public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { using (UserManager <IdentityUser> userManager = _userManagerFactory()) { IdentityUser user = await userManager.FindAsync(context.UserName, context.Password); WVCUserManager wvcUserManager = new WVCUserManager(); IdentityManager identityManager = new IdentityManager(); wvc_user wvcUser = null; IdentityUserRole userRole = null; IdentityRole role = null; if (user == null) { context.SetError("invalid_grant", "The user name or password is incorrect."); return; } else { userRole = user.Roles.FirstOrDefault(); if (userRole == null) { context.SetError("invalid_grant", "The user is inactive (no rules assigned). Contact administrator."); return; } role = identityManager.GetRoleById(userRole.RoleId); // check wvc user active; wvcUser = wvcUserManager.FindUser(user.Id); if (wvcUser == null) { context.SetError("invalid_grant", "The user is inactive. Contact administrator."); return; } } // Add claims ClaimsIdentity oAuthIdentity = await userManager.CreateIdentityAsync(user, context.Options.AuthenticationType); oAuthIdentity.AddClaim(new Claim(Authentication.IDKey, wvcUser.id.ToString())); oAuthIdentity.AddClaim(new Claim(Authentication.RoleKey, role.Name)); ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user, CookieAuthenticationDefaults.AuthenticationType); AuthenticationProperties properties = CreateProperties(user, role, wvcUser); AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties); context.Validated(ticket); context.Request.Context.Authentication.SignIn(cookiesIdentity); } }
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { using (UserManager<IdentityUser> userManager = _userManagerFactory()) { IdentityUser user = await userManager.FindAsync(context.UserName, context.Password); WVCUserManager wvcUserManager = new WVCUserManager(); IdentityManager identityManager = new IdentityManager(); wvc_user wvcUser = null; IdentityUserRole userRole = null; IdentityRole role = null; if (user == null) { context.SetError("invalid_grant", "The user name or password is incorrect."); return; } else { userRole = user.Roles.FirstOrDefault(); if (userRole == null) { context.SetError("invalid_grant", "The user is inactive (no rules assigned). Contact administrator."); return; } role = identityManager.GetRoleById(userRole.RoleId); // check wvc user active; wvcUser = wvcUserManager.FindUser(user.Id); if (wvcUser == null) { context.SetError("invalid_grant", "The user is inactive. Contact administrator."); return; } } // Add claims ClaimsIdentity oAuthIdentity = await userManager.CreateIdentityAsync(user, context.Options.AuthenticationType); oAuthIdentity.AddClaim(new Claim(Authentication.IDKey, wvcUser.id.ToString())); oAuthIdentity.AddClaim(new Claim(Authentication.RoleKey, role.Name)); ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user, CookieAuthenticationDefaults.AuthenticationType); AuthenticationProperties properties = CreateProperties(user, role, wvcUser); AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties); context.Validated(ticket); context.Request.Context.Authentication.SignIn(cookiesIdentity); } }