public async Task<IActionResult> SendConfirmationEmail(SendConfirmationEmailAddressModel model)//User Id { var user = await _grantChecker.EnsureGranted(model.AccessToken, model.OpenId, t => t.ConfirmEmail); var useremail = await _dbContext.UserEmails.SingleOrDefaultAsync(t => t.EmailAddress == model.Email.ToLower()); if (useremail == null) { return this.Protocol(ErrorType.NotFound, $"Can not find your email:{model.Email}"); } if (useremail.OwnerId != user.Id) { return this.Protocol(ErrorType.Unauthorized, $"The account you tried to authorize is not an account with id: {model.OpenId}"); } if (useremail.Validated) { return this.Protocol(ErrorType.HasDoneAlready, $"The email: {model.Email} was already validated!"); } // limit the sending frenquency to 3 minutes. if (DateTime.UtcNow > useremail.LastSendTime + new TimeSpan(0, 1, 0)) { var token = Guid.NewGuid().ToString("N"); useremail.ValidateToken = token; useremail.LastSendTime = DateTime.UtcNow; await _dbContext.SaveChangesAsync(); try { await _emailSender.SendConfirmation(user.Id, useremail.EmailAddress, token); } catch (SmtpException e) { return this.Protocol(ErrorType.InvalidInput, e.Message); } return this.Protocol(ErrorType.Success, "Successfully sent the validation email."); } return this.Protocol(ErrorType.RequireAttention, "We have just sent you an Email in an minute."); }
public async Task <IActionResult> SendConfirmationEmail(SendConfirmationEmailAddressModel model)//User Id { var accessToken = await _dbContext .AccessToken .SingleOrDefaultAsync(t => t.Value == model.AccessToken); var app = await _developerApiService.AppInfoAsync(accessToken.ApplyAppId); var user = await _userManager.FindByIdAsync(model.Id); var useremail = await _dbContext.UserEmails.SingleOrDefaultAsync(t => t.EmailAddress == model.Email.ToLower()); if (useremail == null) { return(this.Protocal(ErrorType.NotFound, $"Can not find your email:{model.Email}")); } if (useremail.OwnerId != user.Id) { return(this.Protocal(ErrorType.Unauthorized, $"The account you tried to authorize is not an account with id: {model.Id}")); } if (useremail.Validated) { return(this.Protocal(ErrorType.HasDoneAlready, $"The email :{model.Email} was already validated!")); } 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 send confirmation email!")); } //limit the sending frenquency to 3 minutes. if (DateTime.Now > useremail.LastSendTime + new TimeSpan(0, 3, 0)) { var token = StringOperation.RandomString(30); useremail.ValidateToken = token; useremail.LastSendTime = DateTime.Now; await _dbContext.SaveChangesAsync(); var callbackUrl = new AiurUrl(_serviceLocation.API, "User", nameof(EmailConfirm), new { userId = user.Id, code = token }); await _emailSender.SendEmail(useremail.EmailAddress, $"{Values.ProjectName} Account Email Confirmation", $"Please confirm your email by clicking <a href='{callbackUrl}'>here</a>"); } return(this.Protocal(ErrorType.Success, "Successfully sent the validation email.")); }
public async Task <IActionResult> SendConfirmationEmail(SendConfirmationEmailAddressModel model)//User Id { var user = await _grantChecker.EnsureGranted(model.AccessToken, model.OpenId, t => t.ConfirmEmail); var userEmail = await _dbContext.UserEmails.SingleOrDefaultAsync(t => t.EmailAddress == model.Email.ToLower()); if (userEmail == null) { return(this.Protocol(ErrorType.NotFound, $"Can not find your email:{model.Email}")); } if (userEmail.OwnerId != user.Id) { return(this.Protocol(ErrorType.Unauthorized, $"The account you tried to authorize is not an account with id: {model.OpenId}")); } if (userEmail.Validated) { return(this.Protocol(ErrorType.HasDoneAlready, $"The email: {model.Email} was already validated!")); } var byProvider = _authProviders.FirstOrDefault(t => user.Email.ToLower().Contains($"@from.{t.GetName().ToLower()}")); if (byProvider != null) { return(this.Protocol(ErrorType.HasDoneAlready, $"We could not get your email from your auth provider: {byProvider.GetName()} because you set your email private. Please manually link your email at: {_serviceLocation.Account}!")); } // limit the sending frenquency to 3 minutes. if (DateTime.UtcNow > userEmail.LastSendTime + new TimeSpan(0, 1, 0)) { var token = Guid.NewGuid().ToString("N"); userEmail.ValidateToken = token; userEmail.LastSendTime = DateTime.UtcNow; await _dbContext.SaveChangesAsync(); try { _cannonService.FireAsync <ConfirmationEmailSender>(async(sender) => { await sender.SendConfirmation(user.Id, userEmail.EmailAddress, token); }); } catch (SmtpException e) { return(this.Protocol(ErrorType.InvalidInput, e.Message)); } return(this.Protocol(ErrorType.Success, "Successfully sent the validation email.")); } return(this.Protocol(ErrorType.RequireAttention, "We have just sent you an Email in an minute.")); }