.Net对接阿里云短信平台

Posted 王景林的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.Net对接阿里云短信平台相关的知识,希望对你有一定的参考价值。

一、在对接阿里云短信平台之前需完成阿里云短信平台短信签名和短信模板的配置,在此不做过多说明,建议采用企业账号的身份申请短信签名和短信模板更容易审核通过一些
       阿里云短信平台地址 https://www.aliyun.com/product/sms?spm=5176.19720258.J_2686872250.7.7b812c4aBro5hF

二、完成以上配置之后在项目Nuget包中安装:AlibabaCloud.SDK.Dysmsapi20170525

三、主体代码

        public void SendSms(string PhoneNumbers)
        {
            var verifyCode = GetVerifyCode();
            JObject jObject = new JObject();
            jObject.Add("code", verifyCode);
            AlibabaCloud.SDK.Dysmsapi20170525.Client client = CreateClient("accessKeyId", "accessKeySecret");
            AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest sendSmsRequest = new AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest
            {
                PhoneNumbers = PhoneNumbers,
                SignName = "SignName",
                TemplateCode = "TemplateCode",
                TemplateParam = jObject.ToString()
            };
            SendSmsResponse sendSmsResponse = client.SendSms(sendSmsRequest);
            if (sendSmsResponse.Body.Code == "OK" && sendSmsResponse.Body.Message == "OK")
            {
                textEdit1.Text = sendSmsResponse.Body.BizId;
                SetCacheTimeSpan(sendSmsResponse.Body.BizId, verifyCode, 20);
            }
            else if("isv.BUSINESS_LIMIT_CONTROL".Equals(sendSmsResponse.Body.Code))
            {
                throw new Exception("获取验证码过于频繁");
            }
        }


        public static AlibabaCloud.SDK.Dysmsapi20170525.Client CreateClient(string accessKeyId, string accessKeySecret)
        {
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
                AccessKeyId = accessKeyId,
                AccessKeySecret = accessKeySecret,
            };
            config.Endpoint = "dysmsapi.aliyuncs.com";
            return new AlibabaCloud.SDK.Dysmsapi20170525.Client(config);
        }


        /// <summary>
        /// 生成6位数随机验证码
        /// </summary>
        /// <returns></returns>
        private static int GetVerifyCode()
        {
            Random random = new Random();
            return random.Next(100000, 999999);
        }

        /// <summary>
        /// 设置缓存相对过期时间
        /// </summary>
        /// <param name="cacheKey">key</param>
        /// <param name="objValue">缓存对象</param>
        /// <param name="timeSpan">过期时间(秒)</param>
        public static void SetCacheTimeSpan(string cacheKey, object objValue, long timeSpan)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(cacheKey, objValue, null, DateTime.MaxValue, TimeSpan.FromSeconds(timeSpan));
        }


        /// <summary>
        /// 获取Cache的value
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        public object GetCacheValue(string cacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            return objCache.Get(cacheKey);
        }

四、调用

            SendSms(PhoneNumbers);

 

以上是关于.Net对接阿里云短信平台的主要内容,如果未能解决你的问题,请参考以下文章

低代码对接腾讯云-阿里云短信平台

低代码对接腾讯云-阿里云短信平台

低代码对接腾讯云-阿里云短信平台

crmeb标准版和PRO安装阿里云短信开发对接使用

对接阿里云短信接口

NE35E MQTT协议对接阿里云