public async Task <IActionResult> SendVerificationCode([FromBody] SendVerificationCodeRequest sendVerificationCodeRequest) { if (sendVerificationCodeRequest == null) { return(BadRequest()); } var verificationCodeCacheKey = VerificationCodeCacheKey(sendVerificationCodeRequest.EmailAddress); string verificationCode = FursvpRandom.CopyableButHardToGuessCode(); MemoryCache.Set(verificationCodeCacheKey, verificationCode, TimeSpan.FromMinutes(60)); // TODO - make this a config variable var email = CreateVerificationEmail(sendVerificationCodeRequest.EmailAddress, verificationCode); await Emailer.SendAsync(email).ConfigureAwait(false); return(Ok()); }
/// <summary> /// 发送验证码短信 /// </summary> /// <param name="cellphone">手机号</param> /// <param name="verifyCode">验证码</param> /// <param name="ip">获取验证码客户端Ip地址</param> /// <param name="expireMinutes">有效时间(分钟),默认5分钟内有效</param> /// <param name="host">发送短信host</param> /// <returns></returns> public static async Task <bool> SendVerificationCodeSmsMessageAsync(string cellphone, string verifyCode, string ip, int expireMinutes = 5, string host = "setting.tuhu.cn") { if (String.IsNullOrWhiteSpace(cellphone) || String.IsNullOrWhiteSpace(verifyCode)) { return(false); } var model = new SendVerificationCodeRequest { Cellphone = cellphone, ChannelType = VerificationCodeChannelTypes.Default, VerificationCode = verifyCode, Expires = expireMinutes, Host = host, UserIp = ip }; try { using (var client = new SmsClient()) { var result = await client.SendVerificationCodeAsync(model); // 记录 elk 日志 Logger.Log(Level.Info, $"SendVerificationCodeSmsMessageAsync 发送验证码短信:手机号:{cellphone},验证码:{verifyCode},过期时间:{expireMinutes.ToString()},响应信息:{JsonConvert.SerializeObject(result)}"); if (result.Success) { return(result.Result > 0); } else { Logger.Log(Level.Error, "SendVerificationCodeSmsMessageAsync 发送验证码短信失败,手机号:{0},异常信息:{1}", cellphone, result.Exception?.InnerException?.Message ?? result.ErrorMessage); return(false); } } } catch (Exception ex) { Logger.Log(Level.Error, "SendVerificationCodeSmsMessageAsync 发送验证码短信异常,手机号:{0},异常信息:{1}", cellphone, ex.InnerException?.Message); return(false); } }