public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { using (SecretSantaUserManager userManager = SecretSantaUserManager.GetInstance()) { IdentityUser user = await userManager.FindAsync(context.UserName, context.Password); if (user == null) { context.SetError("error", "Invalid user name or password"); return; } } var identity = new ClaimsIdentity(context.Options.AuthenticationType); identity.AddClaim(new Claim("username", context.UserName)); context.Validated(identity); }
public async Task <HttpResponseMessage> Post([FromBody] UserDto userDto) { if (!ModelState.IsValid) { Request.CreateResponse(HttpStatusCode.BadRequest, ModelState); } using (SecretSantaUserManager userManager = SecretSantaUserManager.GetInstance()) { UserIdentity userIdentity = new UserIdentity { UserName = userDto.UserName, DisplayName = userDto.DisplayName, }; IdentityResult result = await userManager.CreateAsync(userIdentity, userDto.Password); HttpResponseMessage errorResult = GetErrorResult(result); if (errorResult != null) { return(errorResult); } } return(Request.CreateResponse(HttpStatusCode.Created, userDto.DisplayName)); }