public async Task <IActionResult> PasswordAuth(PasswordAuthAddressModel model) { var app = (await _apiService.AppInfoAsync(model.AppId)).App; if (app == null) { return(NotFound()); } var mail = await _dbContext .UserEmails .Include(t => t.Owner) .SingleOrDefaultAsync(t => t.EmailAddress == model.Email); if (mail == null) { ModelState.AddModelError(string.Empty, "Unknown user email."); return(ResultGenerator.GetInvalidModelStateErrorResponse(ModelState)); } var user = mail.Owner; var result = await _signInManager.PasswordSignInAsync(user, model.Password, isPersistent : false, lockoutOnFailure : true); if (result.Succeeded) { OAuthPack pack = null; if (await user.HasAuthorizedApp(_dbContext, model.AppId)) { pack = await user.GeneratePack(_dbContext, model.AppId); } else { await user.GrantTargetApp(_dbContext, model.AppId); pack = await user.GeneratePack(_dbContext, model.AppId); } return(Json(new AiurValue <int>(pack.Code) { Code = ErrorType.Success, Message = "Auth success." })); } else if (result.RequiresTwoFactor) { throw new NotImplementedException(); } else if (result.IsLockedOut) { throw new NotImplementedException(); } else { return(this.Protocal(ErrorType.Unauthorized, "Wrong password!")); } }
private async Task <OAuthPack> GeneratePack(GatewayUser user, string appId) { var pack = new OAuthPack { Code = Math.Abs(Guid.NewGuid().GetHashCode()), UserId = user.Id, ApplyAppId = appId }; await _dbContext.OAuthPack.AddAsync(pack); await _dbContext.SaveChangesAsync(); return(pack); }