Skip to content

Instantly share code, notes, and snippets.

View wellingtonjhn's full-sized avatar

Wellington Nascimento wellingtonjhn

View GitHub Profile
@wellingtonjhn
wellingtonjhn / Anonymous-actions.cs
Created May 17, 2018 16:41
Allow Anonymous Actions
[HttpPost, AllowAnonymous, Route("register")]
public async Task<IActionResult> CreateUser([FromBody] CreateUser command)
{
// ... conteúdo ocultado
}
[HttpPost, AllowAnonymous, Route("login")]
public async Task<IActionResult> Authenticate([FromBody] AuthenticateUser command)
{
// ... conteúdo ocultado
@wellingtonjhn
wellingtonjhn / Startup.cs
Created April 30, 2018 22:49
Use Authentication
public class Startup
{
// ... restante do arquivo ocultado
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// .. restante do método ocultado
app.UseAuthentication();
app.UseMvc();
@wellingtonjhn
wellingtonjhn / Startup.cs
Created April 30, 2018 01:08
HttpcontextAccessor DI
public void ConfigureServices(IServiceCollection services)
{
// ... restante do método ocultado
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<AuthenticatedUser>();
}
@wellingtonjhn
wellingtonjhn / AuthenticatedUser.cs
Created April 29, 2018 23:08
Authenticated User - IHttpContextAccessor
public class AuthenticatedUser
{
private readonly IHttpContextAccessor _accessor;
public AuthenticatedUser(IHttpContextAccessor accessor)
{
_accessor = accessor;
}
public string Email => _accessor.HttpContext.User.Identity.Name;
@wellingtonjhn
wellingtonjhn / Startup-configure-authorize-filter.cs
Created April 29, 2018 21:52
Configure Authorize Filter ASP.Net MVC
public void ConfigureServices(IServiceCollection services)
{
// ... restante do método ocultado
services.AddMvc(config =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
@wellingtonjhn
wellingtonjhn / AdministrationController.cs
Last active May 28, 2018 02:04
AdministrationController - AuthorizeAttribute
[Route("api/[controller]"), Authorize(Roles = "Administrator")]
public class AdministrationController : Controller
{
[HttpGet, Route("accounts")]
public async Task<IActionResult> ListUsers()
{
// ... conteúdo omitido
}
}
@wellingtonjhn
wellingtonjhn / AddJwtAuthorization.cs
Created April 29, 2018 08:47
AddJwtAuthorization Method
private static void AddJwtAuthorization(IServiceCollection services)
{
var jwtSettings = services.BuildServiceProvider().GetRequiredService<JwtSettings>();
services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(jwtBearerOptions =>
{
jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters
{
@wellingtonjhn
wellingtonjhn / AuthController.cs
Created April 29, 2018 08:24
AuthController - MediatR
[Route("api/[controller]")]
public class AuthController : Controller
{
private readonly IMediator _mediator;
public AuthController(IMediator mediator)
{
_mediator = mediator;
}
@wellingtonjhn
wellingtonjhn / AuthenticateUserHandler.cs
Created April 29, 2018 08:05
AuthenticateUserHandler - MediatR
public class AuthenticateUserHandler : IRequestHandler<AuthenticateUser, Response>
{
private readonly IJwtService _jwtService;
private readonly IUserRepository _repository;
public AuthenticateUserHandler(IJwtService jwtService, IUserRepository repository)
{
_jwtService = jwtService;
_repository = repository;
}
public class JwtService : IJwtService
{
private readonly JwtSettings _settings;
public JwtService(JwtSettings settings)
{
_settings = settings;
}
public object CreateJwtToken(User user)