/// <summary> /// 发送邮箱验证码 /// </summary> /// <param name="input"></param> /// <returns></returns> public async Task SendEmailVerificationCode(SendEmailVerificationCodeInput input) { await CheckEmailAddressVaild(input); var verificationCode = await _verificationCodeManager.GetOrSetVerificationCodeAsync(input.EmailAddress, input.CodeType); try { await _userEmailer .SendEmailVerificationCodeAsync(input.EmailAddress, verificationCode.Code, input.CodeType); } catch (SmtpCommandException) { throw new UserFriendlyException(L("RecipientNotExisted")); } }
/// <summary> /// 验证手机号码是否有效 /// </summary> /// <param name="input"></param> /// <returns></returns> private async Task CheckEmailAddressVaild(SendEmailVerificationCodeInput input) { if (input.CodeType == VerificationCodeType.Register) { var user = await _userManager.UserStore.Find4PlatformByEmailAsync(input.EmailAddress); if (user != null) { throw new UserFriendlyException(string.Format(L("Identity.DuplicateEmail"), input.EmailAddress)); } } else if (input.CodeType == VerificationCodeType.EmailBinding) { var currentUser = await GetCurrentUserIfLoginAsync(); var user = await _userManager.UserStore.Find4PlatformByEmailAsync(input.EmailAddress); //未登录,不能收取邮箱绑定验证码 if (currentUser == null) { throw new UserFriendlyException(L("Identity.NotLoggedIn")); } if (user != null) { if (user != currentUser) { throw new UserFriendlyException(string.Format(L("Identity.DuplicateEmail"), input.EmailAddress)); } if (user == currentUser) { throw new UserFriendlyException(string.Format(L("Identity.BindingEmailCanNotEqual"), input.EmailAddress)); } } } else if (input.CodeType == VerificationCodeType.EmailUnBinding) { var currentUser = await GetCurrentUserIfLoginAsync(); if (currentUser == null || currentUser.EmailAddress.IsNullOrWhiteSpace()) { throw new UserFriendlyException(string.Format(L("Identity.UnBindingEmail"), input.EmailAddress)); } } }