/// <summary> /// 获取 验证码 /// </summary> /// <param name="input">验证码Input</param> public async Task GetValidateCode(ValidateCodeInput input) { if (input.CodeType == CodeType.注册) { if (UserRepo.CheckExists(p => p.UserName == input.CodeKey)) { throw new UserFriendlyException("手机号已注册,不能获取注册验证码."); } } if (input.CodeType == CodeType.找回密码 || input.CodeType == CodeType.动态登录) { if (!UserRepo.CheckExists(p => p.UserName == input.CodeKey)) { throw new UserFriendlyException("该用户未注册,不能获取验证码."); } } if (input.ValidateType == ValidateType.手机) { await SendValidateCode(input, code => { var smsContent = "您本次的验证码为" + code + ",工作人员不会向您索要此验证码,请勿向任何人泄露。[不同]"; //Sms.Send(phoneNo, 1, smsContent); }); } else { await SendValidateCode(input, code => { string subject = "验证码"; string body = "您本次的验证码为" + code + ",工作人员不会向您索要此验证码,请勿向任何人泄露。[不同]"; EmailSender.Send(input.CodeKey, subject, body); }); } }
/// <summary> /// 发送验证码 /// </summary> /// <param name="input">验证码Input</param> /// <returns></returns> private async Task SendValidateCode(ValidateCodeInput input, Action <string> sendAction) { string validateCode = new ValidateCoder().GetCode(6, ValidateCodeType.Number); var codeEntity = input.MapTo <ValidateCode>(); codeEntity.Code = validateCode; await ValidateCodeRepo.InsertAsync(codeEntity); sendAction(validateCode); }
/// <summary> /// 验证 验证码 /// </summary> /// <param name="input">input</param> /// <returns></returns> public async Task ValidateCode(ValidateCodeInput input) { //有效期30分钟 DateTime ValidityPeriod = DateTime.Now.AddMinutes(-30); var code = await ValidateCodeRepo.FirstOrDefaultAsync( p => p.CodeKey == input.CodeKey && p.CodeType == input.CodeType && p.ValidateType == input.ValidateType && p.Code == input.Code && p.CreationTime > ValidityPeriod); if (code == null) { throw new UserFriendlyException("验证失败"); } }