手机短信服务

启用了手机号码验证(EnableUserMobileVerificationopen in new window) 或启用了手机号登录(EnableSmsLoginopen in new window)后, 就需要有手机短信发送功能。
另有 EnableSmsCaptchaopen in new window 配置项指示发送手机短信前是否需要先进行图片验证码验证,建议置为 true。

不同的短信服务商的接口各不相同,因此 QuickAdmin.Net 并未内置相关实现,不过已设计了短信接口以及抽象基类以便用户快速实现自己手机短信服务。
你也可以直接使用我做好的短信发送扩展,添加相关配置即可使用。

实现手机短信服务

QuickAdmin.Net 内置了手机短信接口 ISmsServiceopen in new window 及抽象基类 SmsServiceopen in new window,选定短信服务商后,你可通过继承该抽象基类快速实现自己手机短信服务。

using QuickAdmin.Service.Auth;

public class MySmsService : SmsService
{
  // 重写 SendCode() 即可
  protected override IServiceResult SendCode(int purpose, string mobile, string code)
  {
    // 当前 purpose == 1 表示正在绑定手机号,purpose == 2 表示正在用手机号登录
    // mobile 已被前置代码检查过了,必定是一个有效的手机号码
    ...
    if (failed)
      return ServiceResult.Failed("xxxx"); // 注意错误消息会返回给前端,因此不要返回敏感信息
    return ServiceResult.Ok();
  }
}

...
// 在启动时注册服务(必须在调用 AddQuickAdmin 之后去注册):
builder.Services.AddSingleton(typeof(ISmsService), typeof(MySmsService));
...

基类里的其它方法:
CheckMobileopen in new window:可重写此方法来屏蔽某些手机号码。
BeforeSendopen in new window:基类此方法已实现对发送间隔的控制,一分钟内只能发送一条。
SaveSmsLogopen in new window:用来保存发送日志,你可按需在继承类 SendCode() 里调用。

使用腾讯云短信发送扩展

如果你使用的是腾讯云短信,可直接使用已做好腾讯云短信发送扩展实现发送功能, 点击此处open in new window下载。
下载并解压后,参照上一章节说明将扩展文件夹放置到指定位置,打开 configs\TencentSms.json 输入相关配置即可。

TencentSms.json 配置说明:

{
  "SecretId": "xxx",     // 腾讯云账户密钥对
  "SecretKey": "xxx",    // 腾讯云账户密钥对
  "Region": null,        // 地域信息
  "SmsSdkAppId": "xxx",  // 短信应用 Id
  "SignName": "xxx",     // 短信签名
  "TemplateIds": {
    "1": "xxx",          // 绑定验证码模板 Id,其对应的模板内容类似:"您正在绑定手机号码,验证码为:{1},{2} 分钟内有效,如非本人操作,请忽略本短信。"
    "2": "xxx"           // 登录验证码模板 Id,其对应的模板内容类似:"您正在登录,验证码为:{1},如非本人操作,请忽略本短信。"
  },
  "SaveSmsLog": true,               // 指示是否保存发送日志,若置为 true,将保存到 qsys_smslog 数据表里,对应实体为 QuickAdmin.Entity.SysSmsLog
  "BlockedMobiles": [ "16", "17" ]  // 要屏蔽的号码数组,以这些配置打头的号码都将被屏蔽
}

参考腾讯云短信官方文档open in new window