阿里云发送短信注册验证码功能实现,及相关代码

有图有真相

阿里云发送短信注册验证码功能实现,及相关代码_第1张图片阿里云发送短信注册验证码功能实现,及相关代码_第2张图片

首先是引入官方 下载查看aliyun关键的dll,

生成六位二维码,存到cookie里,发送给客户,客户提交时,检验和cookie里是否一致

  using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Core.Exceptions;
using System.Collections;
//using Aliyun.Acs.Core.Http;

public void Button2_Click()
        {
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", "LTA******GHL", "RQihj******bcHP");
            DefaultAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest();
            request.Method = Aliyun.Acs.Core.Http.MethodType.POST;
            request.Domain = "dysmsapi.aliyuncs.com";
            request.Version = "2017-05-25";
            request.Action = "SendSms";
            // request.Protocol = ProtocolType.HTTP;
            string mobile = Request["BrainTextBox7"];
            request.AddQueryParameters("PhoneNumbers", mobile);
            request.AddQueryParameters("SignName", "叨叨分工");
            request.AddQueryParameters("TemplateCode", "SMS_122130310");
            string code = createCode();
            request.AddQueryParameters("TemplateParam", "{\"code\":\"" + code + "\"}");
            try
            {
                CommonResponse response = client.GetCommonResponse(request);
                Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
                HttpCookie ccode = new HttpCookie("code", code);
                ccode.Expires = DateTime.Now.AddMinutes(2);
                Response.Cookies.Add(ccode);
                Response.Write("ok");
            }
            catch (ServerException e2)
            {
                Response.Write("er");
                Console.WriteLine();
            }

        }

前端按钮处理 

   function onValid() {         
            var mobile = $("#BrainTextBox7").val().trim();        
            if (!isMobile(mobile)) {
                top.$.messager.info('提示', '请填写正确的手机号!');
            }
            else {
                //发送后,将这个按钮变为另外一个按钮,显示倒计时,
                if (!flag) {
                    return false;
                   
                }
                $.ajax({
                    type: "POST",
                    dataType: "text",
                    //cache:true,
                    data: {
                        BrainTextBox7: mobile,
                    },
                    url: "/Service/EasyUiService.ashx?Method=Button2_Click",
                    success: function (json) {
                        if (json == "ok") {                           
                            showTimerout();
                            flag = !flag;
                        }
                        else {
                            top.$.messager.info('提示', '发送验证码失败!');
                        }
                        
                    },
                    error: function () {
                        top.$.messager.info('提示', '发送验证码错误!');
                    }
                });
                           
            }
            return false;
        }
       

        function showTimerout() {          
            var sum=120;
            var timerr=setInterval(function () {          
                $("#Button2").val(sum + "秒");
                sum--;
            }, 1000);
            setTimeout(function () {
                window.clearTimeout(timerr);
                $("#Button2").val("发送验证码");
                flag = true;
            }, 121000);

           
        }

 

你可能感兴趣的:(发送短信)