This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class JwtService : IJwtService | |
| { | |
| private readonly JwtSettings _settings; | |
| public JwtService(JwtSettings settings) | |
| { | |
| _settings = settings; | |
| } | |
| public JsonWebToken CreateJsonWebToken(User user) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public void ConfigureServices(IServiceCollection services) | |
| { | |
| services.AddSwaggerGen(options => | |
| { | |
| // ... código omitido | |
| options.AddSecurityDefinition( | |
| "bearer", | |
| new ApiKeyScheme | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [Route("api/[controller]")] | |
| public class ProfileController : Controller | |
| { | |
| private readonly IAuthorizationService _authorizationService; | |
| public CustomersController(IAuthorizationService authorizationService) | |
| { | |
| _authorizationService = authorizationService; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public void ConfigureServices(IServiceCollection services) | |
| { | |
| // ... restante do códito omitido | |
| services.AddAuthorization(options => | |
| { | |
| options.AddPolicy("DeleteUserPolicy", policy => | |
| policy.Requirements.Add(new DeleteUserRequirement("CanDeleteUser"))); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class DeleteUserRequirementHandler : AuthorizationHandler<DeleteUserRequirement> | |
| { | |
| private const string AdministratorRoleName = "Administrator"; | |
| private AuthorizationHandlerContext _context; | |
| protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, DeleteUserRequirement requirement) | |
| { | |
| _context = context; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class DeleteUserRequirement : IAuthorizationRequirement | |
| { | |
| public string RequiredPermission { get; } | |
| public DeleteUserRequirement(string requiredPermission) | |
| { | |
| RequiredPermission = requiredPermission; | |
| } | |
| } |