public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { var allowedOrigin = context.OwinContext.Get <string>("as:clientAllowedOrigin"); if (allowedOrigin == null) { allowedOrigin = "*"; } context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin }); using (IdentityRepository _repo = new IdentityRepository()) { IdentityUser user = await _repo.FindUser(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "O usuário ou senha estão incorretos."); return; } //if (!user.EmailConfirmed) { // context.SetError("invalid_grant", "O email do usuário não foi confirmado"); // return; //} } var identity = new ClaimsIdentity(context.Options.AuthenticationType); identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName)); identity.AddClaim(new Claim(ClaimTypes.Role, "user")); identity.AddClaim(new Claim("sub", context.UserName)); var props = new AuthenticationProperties(new Dictionary <string, string> { { "as:client_id", (context.ClientId == null) ? string.Empty : context.ClientId }, { "userName", context.UserName } }); var ticket = new AuthenticationTicket(identity, props); context.Validated(ticket); }
// responsible to validate the username and password sent to the authorization server’s token endpoint // If the credentials are valid we’ll create “ClaimsIdentity” class and pass the authentication type to it, in our case “bearer token” public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); // validate user using (IdentityRepository identityRepo = new IdentityRepository()) { var user = identityRepo.FindUser(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "The user name or password is incorrect."); return; } } var identity = new System.Security.Claims.ClaimsIdentity(context.Options.AuthenticationType); identity.AddClaim(new System.Security.Claims.Claim("sub", context.UserName)); identity.AddClaim(new System.Security.Claims.Claim("role", "user")); // token generation happens here context.Validated(identity); }