public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); var user = _userService.Authenticate(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "Usuário ou senha inválidos"); return; } var identity = new ClaimsIdentity(context.Options.AuthenticationType); identity.AddClaim(new Claim(ClaimTypes.Name, user.Email)); foreach (var item in _userService.GetAllRoles(user.Id)) { identity.AddClaim(new Claim(ClaimTypes.Role, item)); } GenericPrincipal principal = new GenericPrincipal(identity, new string[] {}); Thread.CurrentPrincipal = principal; context.Validated(identity); }
public Task <HttpResponseMessage> Post([FromBody] dynamic body) { HttpResponseMessage response = new HttpResponseMessage(); var command = new RegisterUserCommand( fullName: (string)body.fullName, username: (string)body.username, email: (string)body.email, password: (string)body.password, confirmpass: (string)body.confirmPass ); _service.Register(command); _service.Authenticate("andrebaltieri", "123456"); if (_notification.HasNotifications()) { foreach (var item in _notification.Notify()) { ModelState.AddModelError("", item.Value); } response = Request.CreateResponse(HttpStatusCode.BadRequest, _notification.Notify()); } else { response = Request.CreateResponse(HttpStatusCode.OK, command); } var tsc = new TaskCompletionSource <HttpResponseMessage>(); tsc.SetResult(response); return(tsc.Task); }
public IActionResult Authenticate([FromBody] UserViewModel userViewModel) { var userDTO = _userAppService.Authenticate(userViewModel.Email, userViewModel.Password); if (userDTO == null) { return(NotFound("Usuario não encontrado")); } var user = Mapper.Map <Domain.Entities.User>(userDTO); var token = TokenService.GenerateToken(user); user.Password = string.Empty; return(Ok(new { user.Name, token })); }
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); var user = _userService.Authenticate(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "User or Password is invalid!"); return; } var identity = new ClaimsIdentity(context.Options.AuthenticationType); identity.AddClaim(new Claim(ClaimTypes.Name, user.Email)); identity.AddClaim(new Claim(ClaimTypes.Role, user.IsAdmin ? "admin" : "")); var principal = new GenericPrincipal(identity, new string[] { user.IsAdmin ? "admin" : "" }); Thread.CurrentPrincipal = principal; context.Validated(identity); }