9

基于阿里云服务实现短信验证码功能 - 码农阿亮

 1 year ago
source link: https://www.cnblogs.com/wml-it/p/17613232.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

  阿里云短信服务是一项基于云计算和大数据技术的企业级短信平台服务。它能够为企业和开发者提供高可用、高性能、高稳定性的短信发送服务,可以快速地将各类业务通知、验证码、营销推广等信息发送给用户。在我们经常登录一些系统或者APP时候,经常会遇到其他登录登录方式——短信验证码登录。这也是我前一段时间优化一个系统时新增加的一个登录功能,mark记录一下。

一、准备工作:

登录阿里云官网注册用户:

https://dysms.console.aliyun.com/

登录后完成个人或者企业认证:

说明:供个人学习测试,建议个人认证就可以。认证完成后,会赠送100条免费测试短信,用完后需要续费购买。

img

创建AccessKey :

Step1:点击右上角头像=>AccessKey 管理

img

Step2:点击创建,填写内容后创建,此处我已经创建

img

Step3:创建后,查看复制出AccessKeySecret和AccessKeyId,供后续程序调用API使用

img

创建签名 :

说明:短信发送时,必须要模板和签名搭配使用,必须创建。创建消息模板时候需要关联签名,所以要先创建签名,等待签名审核通过,再创建消息模板。

img

img

创建消息模板:

说明:如上所述,需要关联已经审核通过的签名。

img
img

完成创建详情:

img

二、创建项目:

项目结构:

说明:方便测试,创建Winform窗体程序。项目结构目录如下:

img

说明:通过Nuget引用阿里云SDK包

Aliyun-net-sdk-dysmsapi   -----api包Aliyun-net-sdk-core   -----安装核心SDK

三、核心代码:

img

点击触发事件代码:

private void btSendMsg_Click(object sender, EventArgs e)
        {
            string phone = "13004089962";//手机号码,多个用,分割
            string code = "888888";//验证码
            string msg = "{\"code\":\"" + code + "\"}";//拼接验证码Json格式,参数名称和消息模板中保持一致
            string templateCode = "SMS_461895332";//消息模板编码
            var  ret  =  AliMsgService.SendUserSms(phone, msg, templateCode);
         }

调用阿里云API服务业务短端代码:

  public class AliMsgService
    {
​
​
        //产品名称:云通信短信API产品,开发者无需替换
        const String product = "Dysmsapi";
        //产品域名,开发者无需替换
        const String domain = "dysmsapi.aliyuncs.com";
​
        // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
        private static String accessKeyId = "";//AccessKey管理中查看
        private static String accessKeySecret = "";//AccessKey管理中查看
​
        /// <summary>
        /// 发送消息通知
        /// </summary>
        /// <param name="phone">推送手机号</param>
        /// <param name="msg">JSON参数</param>
        /// <param name="TemplateCode">短信模板</param>
        /// <returns></returns>
        public static smsrespon SendUserSms(string phone, string msg, string TemplateCode)
        {
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, accessKeySecret);
            DefaultAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest();
            request.Method = MethodType.POST;
            request.Domain = domain;
            request.Version = "2017-05-25";//请求版本
            request.Action = "SendSms";
            request.AddQueryParameters("PhoneNumbers", phone);//必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,
            request.AddQueryParameters("SignName", "阿亮学习发送");//必填:短信签名-可在短信控制台中找到
            request.AddQueryParameters("TemplateCode", TemplateCode);//必填:短信模板-可在短信控制台中找到,发送国际/港澳台消息时,请使用国际/港澳台短信模版
            request.AddQueryParameters("TemplateParam", msg);//"{\"code\":\"" + code + "\"}"
            try
            {
                CommonResponse response = client.GetCommonResponse(request);
                return JsonConvert.DeserializeObject<smsrespon>(response.Data);
            }
            catch (ServerException e)
            {
                return new smsrespon { Message = "阿里云发送验证码异常", Code = "no" };
            }
            catch (ClientException e)
            {
                return new smsrespon { Message = "阿里云发送验证码失败", Code = "no" };
            }
        }
​
        public class smsrespon
        {
            public string Message { get; set; }
            public string RequestId { get; set; }
            public string BizId { get; set; }
            public string Code { get; set; }
        }
    }

四、测试验证:

测试收到验证码截图:

img

阿里云后台数据实时数据:

img
img
img

源码链接地址:

Gitee完整实例地址:

https://gitee.com/mingliang_it/AliyunSendMsgTest.git

本文来自博客园,作者:码农阿亮,转载请注明原文链接:https://www.cnblogs.com/wml-it/p/17613232.html


技术的发展日新月异,随着时间推移,无法保证本博客所有内容的正确性。如有误导,请大家见谅,欢迎评论区指正!
开源库链接,欢迎点亮:
GitHub:https://github.com/ITMingliang
Gitee:https://gitee.com/mingliang_it
GitLab:https://gitlab.com/ITMingliang
【编程内功修炼交流群】:                       【个人公众号】:
t_220731064016_wx.jpg             o_230807062355_gongzhonghao.jpg

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK