Skip to content

Instantly share code, notes, and snippets.

View wellingtonjhn's full-sized avatar

Wellington Nascimento wellingtonjhn

View GitHub Profile
@wellingtonjhn
wellingtonjhn / 2.Post.RefreshToken.Authenticate.cs
Last active July 18, 2018 01:16
Authentication Handler - JWT + RefreshToken
public class Authenticate : Request<Response>
{
public string Email { get; }
public string Password { get; }
public string GrantType { get; }
public string RefreshToken { get; }
public Authenticate(string grantType, string email, string password, string refreshToken)
{
Validate(grantType, email, password, refreshToken);
@wellingtonjhn
wellingtonjhn / 1.Post.RefreshToken.JwtService.cs
Last active July 13, 2018 09:04
JwtService - Create Refresh Token
public class JwtService : IJwtService
{
private readonly JwtSettings _settings;
public JwtService(JwtSettings settings)
{
_settings = settings;
}
public JsonWebToken CreateJsonWebToken(User user)
@wellingtonjhn
wellingtonjhn / 3.Post.Swagger.Startup.cs
Created June 10, 2018 06:58
Swagger XML Documentation
public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerGen(options =>
{
// ... código omitido
var applicationBasePath = PlatformServices.Default.Application.ApplicationBasePath;
var applicationName = PlatformServices.Default.Application.ApplicationName;
var xmlDocumentPath = Path.Combine(applicationBasePath, $"{applicationName}.xml");
@wellingtonjhn
wellingtonjhn / 2.Post.Swagger.Startup.cs
Last active June 10, 2018 06:40
Swagger Auth Config
public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerGen(options =>
{
// ... código omitido
options.AddSecurityDefinition(
"bearer",
new ApiKeyScheme
{
@wellingtonjhn
wellingtonjhn / 1.Post.Swagger.Startup.cs
Last active June 10, 2018 06:35
Swagger Basic Configuration
public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1",
new Info
{
Title = "Demo Jwt",
Version = "v1",
Description = "Projeto de demonstração ASP.Net Core",
@wellingtonjhn
wellingtonjhn / ProfileController.cs
Created May 29, 2018 22:34
Authorization Service Example
[Route("api/[controller]")]
public class ProfileController : Controller
{
private readonly IAuthorizationService _authorizationService;
public CustomersController(IAuthorizationService authorizationService)
{
_authorizationService = authorizationService;
}
@wellingtonjhn
wellingtonjhn / AdministrationController.cs
Created May 29, 2018 22:06
Authorize Attribute with Policy
[Route("api/[controller]"), Authorize(Roles = "Administrator")]
public class AdministrationController : Controller
{
// ... restante do código omitido
[HttpDelete, Route("accounts/{accountId}"), Authorize(Policy = "DeleteUserPolicy")]
public async Task<IActionResult> DeleteAccount(Guid accountId)
{
var command = new RemoveAccount(accountId);
var response = await _mediator.Send(command);
@wellingtonjhn
wellingtonjhn / Startup.cs
Created May 29, 2018 20:18
Configure Policy Requirement Handler
public void ConfigureServices(IServiceCollection services)
{
// ... restante do códito omitido
services.AddAuthorization(options =>
{
options.AddPolicy("DeleteUserPolicy", policy =>
policy.Requirements.Add(new DeleteUserRequirement("CanDeleteUser")));
});
@wellingtonjhn
wellingtonjhn / DeleteUserRequirementHandler.cs
Last active May 29, 2018 18:58
Policy Authorization Requirement
public class DeleteUserRequirementHandler : AuthorizationHandler<DeleteUserRequirement>
{
private const string AdministratorRoleName = "Administrator";
private AuthorizationHandlerContext _context;
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, DeleteUserRequirement requirement)
{
_context = context;
@wellingtonjhn
wellingtonjhn / DeleteUserRequirement.cs
Last active May 29, 2018 18:57
Policy Requirement
public class DeleteUserRequirement : IAuthorizationRequirement
{
public string RequiredPermission { get; }
public DeleteUserRequirement(string requiredPermission)
{
RequiredPermission = requiredPermission;
}
}