public async Task SendAsyncTestAsync() { if (string.IsNullOrEmpty(_settings.AccessKeySecret)) { return; } var sendTo = "17751283521"; var sender = new AliyunSmsSender(_settings); var arg = new SmsBody { Parameters = new Dictionary <string, string> { { "code", new Random().Next(0, 1000000).ToString("D6") } } }; var result = await sender.SendAsync(arg, sendTo); Assert.IsTrue(result.Ok); }
private async Task <Result> SendCodeAsync(string mobileNumberOrEmailAddress, string?overrideMessageTemplateWithCodeArg0 = null, string?overrideSmsTemplateAlias = null, string?overrideSmsSignName = null) { if (mobileNumberOrEmailAddress == null) { throw new ArgumentNullException(nameof(mobileNumberOrEmailAddress)); } var isEmail = mobileNumberOrEmailAddress.IsEmail(); var isMobile = mobileNumberOrEmailAddress.IsMobileNumber(); if (!isEmail && !isMobile) { return(new Failure($"无法送达 '{mobileNumberOrEmailAddress}'")); } var justSentWithinMinute = await _twoFactorDb.TwoFactorCodes .AsNoTracking() .Where(x => x.SentTo == mobileNumberOrEmailAddress) .Where(x => x.CreatedTime > DateTime.Now.AddMinutes(-1)) .AnyAsync(); if (justSentWithinMinute) { return(new Failure("过于频繁,请1分钟后再试")); } var code = new TwoFactorCode { UserId = _me.Id, AnonymousId = _me.AnonymousId, Code = new Random().Next(0, 1000000).ToString("D6"), SentTo = mobileNumberOrEmailAddress }; _twoFactorDb.TwoFactorCodes.Add(code); await _twoFactorDb.Normalize().SaveChangesAsync(); if (isEmail) { if (_mailSender == null) { throw new Exception($"Required to inject service {nameof(IMailSender)}"); } var wrappedSignName = overrideSmsSignName == null ? null : $"【{overrideSmsSignName}】 "; var content = string.Format(overrideMessageTemplateWithCodeArg0 ?? "{wrappedSignName}验证码: {0}", code.Code); await _mailSender.SendAsync($"{wrappedSignName}动态验证码", content, mobileNumberOrEmailAddress); } else if (isMobile) { if (_smsSender == null) { throw new Exception($"Required to inject service {nameof(IMailSender)}"); } var shortMessage = new SmsBody { SignName = overrideSmsSignName, Template = overrideMessageTemplateWithCodeArg0, TemplateAlias = overrideSmsTemplateAlias, Parameters = new Dictionary <string, string> { ["code"] = code.Code } }; await _smsSender.SendAsync(shortMessage, mobileNumberOrEmailAddress); } return(new Success()); }