public async Task<IdentityUser> RegisterUserIfNotFound(User userModel) { if (await _userManager.FindByNameAsync(userModel.UserName) == null) { await RegisterUserAsync(userModel); } return await FindUserAsync(userModel); }
private async Task<IdentityResult> RegisterUserAsync(User userModel) { var user = new IdentityUser { UserName = userModel.UserName }; var result = await _userManager.CreateAsync(user, userModel.Password); return result; }
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); var userModel = new User { UserName = context.UserName, Password = context.Password }; var user = await _authRepository.RegisterUserIfNotFound(userModel); if (user == null) { context.SetError("invalid_grant", "The user name or password is incorrect."); context.Rejected(); return; } var identity = new ClaimsIdentity(context.Options.AuthenticationType); identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id)); context.Validated(identity); }
public async Task<IdentityUser> FindUserAsync(User userModel) { var user = await _userManager.FindAsync(userModel.UserName, userModel.Password); return user; }