public async Task<IActionResult> BindNewEmail(BindNewEmailAddressModel model) { var accessToken = await _dbContext .AccessToken .SingleOrDefaultAsync(t => t.Value == model.AccessToken); var app = await _developerApiService.AppInfoAsync(accessToken.ApplyAppId); var user = await _userManager.FindByIdAsync(model.OpenId); var emailexists = await _dbContext.UserEmails.SingleOrDefaultAsync(t => t.EmailAddress == model.NewEmail); if (emailexists != null) { return this.Protocal(ErrorType.NotEnoughResources, $"An user has already bind email: {model.NewEmail}!"); } if (!_dbContext.LocalAppGrant.Exists(t => t.AppID == accessToken.ApplyAppId && t.APIUserId == user.Id)) { return Json(new AiurProtocal { Code = ErrorType.Unauthorized, Message = "This user did not grant your app!" }); } if (!app.App.ConfirmEmail) { return this.Protocal(ErrorType.Unauthorized, "You app is not allowed to bind new email!"); } var mail = new UserEmail { OwnerId = user.Id, EmailAddress = model.NewEmail, Validated = false }; _dbContext.UserEmails.Add(mail); await _dbContext.SaveChangesAsync(); return this.Protocal(ErrorType.Success, "Successfully set"); }
public async Task<IActionResult> BindNewEmail(BindNewEmailAddressModel model) { var user = await _grantChecker.EnsureGranted(model.AccessToken, model.OpenId, t => t.ConfirmEmail); var emailexists = await _dbContext.UserEmails.AnyAsync(t => t.EmailAddress.ToLower() == model.NewEmail.ToLower()); if (emailexists) { return this.Protocol(ErrorType.NotEnoughResources, $"An user has already bind email: {model.NewEmail}!"); } var mail = new UserEmail { OwnerId = user.Id, EmailAddress = model.NewEmail.ToLower(), Validated = false }; _dbContext.UserEmails.Add(mail); await _dbContext.SaveChangesAsync(); return this.Protocol(ErrorType.Success, "Successfully set"); }