/// <summary> /// The grant resource owner credentials. /// </summary> /// <param name="context"> /// The context. /// </param> /// <returns> /// The <see cref="Task"/>. /// </returns> public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { string clientId, roleName = string.Empty; context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); using (var repo = new AuthRepository()) { IdentityUser user = await repo.FindUser(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "The user name or password is incorrect."); return; } clientId = this.GetClientIdFromIdentityUser(user); roleName = this.GetRoleNameFromIdentityUser(user); } var identity = new ClaimsIdentity(context.Options.AuthenticationType); identity.AddClaim(new Claim("username", context.UserName)); identity.AddClaim(new Claim("role", roleName)); identity.AddClaim(new Claim("clientId", clientId)); context.Validated(identity); }
/// <summary> /// Initializes a new instance of the <see cref="AccountController"/> class. /// </summary> public AccountController() { this.repo = new AuthRepository(); }