public async Task <IHttpActionResult> Register(UserModel userModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = await repository.FindUser(userModel.UserName, userModel.Password); if (user != null) { return(BadRequest()); } IdentityResult result = await repository.RegisterUser(userModel); var errorResult = GetErrorResult(result); if (errorResult != null) { return(errorResult); } return(Ok()); }
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); using (AuthorizationRepository _repo = new AuthorizationRepository()) { IdentityUser user = await _repo.FindUser(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "The user name or password is incorrect."); return; } } var identity = new ClaimsIdentity(context.Options.AuthenticationType); identity.AddClaim(new Claim("sub", context.UserName)); identity.AddClaim(new Claim("role", "user")); context.Validated(identity); }