ASP.NET MVC 使用阿里大于发送验证码短信

现在大部分的商业网站都需要短信验证短信通知。

和客户协商之后,我们还是选择了阿里大于。 阿里大于平台(www.alidayu.com)是阿里巴巴旗下阿里通信的能力开放平台,里面提供很多通信服务及调用包,可以节省很多开发成本直接服用阿里巴巴的基础能力。接下来就是注册阿里大于,如果有淘宝账号可以直接登录,登录以后下载SDK,查看App Key和App Secret,最后把dll文件加载到项目里面。

配置阿里大于的管理有很多个方面,细分为验证码、短信通知、推广短信、语音通知、群发短信等功能,每一项都有独立的签名、模板等,均需要独立申请,好在审核的时间不长,一般几分钟可以通过,最长不超过两小时。

App Key和App Secret一般我们存在Web.config里面,如:

<appSettings>
   ......

    <add key="alidayuAppKey" value="12345678"/>
    <add key="alidayuAppSecret" value="*****"/>
    <add key="alidayuURL" value="http://gw.api.taobao.com/router/rest"/>
  </appSettings>

一个项目下来,需要用到的App Key,或者其它需要保存的信息也许有很多,我们开发一个类对它进行管理,取名为Forwebconfig。

using System;
using System.Configuration;
using System.Globalization;

namespace SevenTons.Models
{
    public class Forwebconfig
    {
        public static string alidayuAppKey
        {
            get
            {
                return Setting<string>("alidayuAppKey");
            }
        }

        public static string alidayuAppSecret
        {
            get
            {
                return Setting<string>("alidayuAppSecret");
            }
        }

        public static string alidayuURL
        {
            get
            {
                return Setting<string>("alidayuURL");
            }
        }

        private static T Setting<T>(string name)
        {
            string value = ConfigurationManager.AppSettings[name];

            if (value == null)
            {
                throw new Exception(String.Format("Could not find setting '{0}',", name));
            }

            return (T)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture);
        }
    }
}

这样一来,读取就非常简单了:

ViewBag.alidayuAppKey = Forwebconfig.alidayuAppKey;
ViewBag.alidayuAppSecret = Forwebconfig.alidayuAppSecret;
ViewBag.alidayuURL = Forwebconfig.alidayuURL;

下面我们简单的开发一个发送随机代码的短信界面:

@using (Html.BeginForm()) { 
@Html.TextBox("phone")
<input type="submit" name="Submit" value="发送短信" />
   }

然后用控制器取到这个Form的电话号码,发送随机数给手机。代码如下:

[HttpPost]
        public ActionResult totest(FormCollection form)
        {
            string alidayuAppKey = Forwebconfig.alidayuAppKey;
            string alidayuAppSecret = Forwebconfig.alidayuAppSecret;
            string alidayuURL = Forwebconfig.alidayuURL;
            Random R = new Random();
            int i = R.Next(1000, 9999);
            string num = i.ToString();
            string value = Convert.ToString(form["phone"]);
            string code = "{\"code\":\"" + num + "\"}";
            ITopClient client = new DefaultTopClient(alidayuURL, alidayuAppKey, alidayuAppSecret);
            AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
            req.SmsType = "normal";
            req.SmsFreeSignName = "Alexzeng.net";
            
            req.SmsParam = code;
            req.RecNum = "18924053286";
            req.SmsTemplateCode = "SMS_43275152";
            AlibabaAliqinFcSmsNumSendResponse response = client.Execute(req);
            if (response.IsError) { ViewBag.err = "短信发送失败!代码:" + response.ErrCode + ",错误原因" + response.ErrMsg; };

            return View();
        }

《ASP.NET MVC 使用阿里大于发送验证码短信》 手机收到的验证码

谢谢大家。转帖的时候请把凉风有兴或者AlexZeng.net进行署名。本文版权声明:自由转载-非商用-以非衍生-保持署名(创意共享3.0许可证

    原文作者:凉风有兴
    原文地址: https://www.jianshu.com/p/33449a4121d7
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞