栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

.Net 实现短信验证

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

.Net 实现短信验证

关于asp.net实现输入手机号发送验证码,并得到验证码。我将详细的在下面说明,每一步都很重要,谨慎去做,不然没过审核的话得等好久才能通过。如果是做项目或者个人研究需要那么就学我这种方法就足够了。

1.首先,必须先申请一个微信公众号(这是第一步,必须要先申请完成,不然后续无法操作)

申请步骤:1.搜索WX公众平台。2.进行注册公众号的申请就可以了。

2.第一步完成后接下来去百度搜索腾讯云,进入官网

登录后搜索短信,点击免费试用,如图:

进去后找到国内短信---签名管理--点击创建签名后进行资料填写,如图:

根据里面的提示填写好资料。在证明上传那一栏上传自己的公众号信息,公众号信息在你创建好了的公众号里面找,找到设置于开发----公众号设置然后截图保存一下进行上传就可以了,上传图片如下:

 3.签名管理资料提交完成后等待审核通过即可,接下来找到正文模板管理----创建正文模板即可

注:这一步需要等到签名管理审核通过后才能申请不然不会通过的,所以等签名管理申请成功后再创建正文模板,依然按照里面提示内容提交资料即可。

4.等待签名管理和正文模板审核通过后(必须),然后导入TencentCloud文件放到项目中 :

然后找到里面的Send.cs文件打开:

打开后按照注释填写自己的相关内容即可

 /// 
        /// 发送短信
        /// 
        /// 发送手机
        /// 验证码
        /// 有效时间
        public static void SendDL(string[] PhoneNumber, string code, int Time)
        {
            try
            {
                Credential cred = new Credential
                {
                    SecretId = "", //在腾讯云官网中的“云产品”中找到“访问秘钥”,点击打开,就看得到相关ID和Key,复制填写即可
                    SecretKey = ""
                };
                ClientProfile clientProfile = new ClientProfile();
                HttpProfile httpProfile = new HttpProfile();
                httpProfile.Endpoint = ("sms.tencentcloudapi.com");
                clientProfile.HttpProfile = httpProfile;
                SmsClient client = new SmsClient(cred, "", clientProfile);
                SendSmsRequest req = new SendSmsRequest();
                req.PhoneNumberSet = PhoneNumber;
                req.TemplateID = "";//创建正文模板ID
                req.SmsSdkAppid = "";//在腾讯云官网中的短信里面找到应用管理里面的应用列表复制里面的SDKAPPid
                req.Sign = "";//您的签名管理的签名内容的名字
                req.TemplateParamSet = new String[] { code, Time.ToString() };
                SendSmsResponse resp = client.SendSmsSync(req);


            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            return;
        }

 最后在你的点击按钮事件调用Send方法即可

全部代码如下:

1.前端代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PhoneYZM.aspx.cs" Inherits="WebSurface.PhoneYZM" %>






    


    

2.后端代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Cr.V20180321.Models;
using TencentCloud.Sms.V20190711;
using TencentCloud.Sms.V20190711.Models;
using System.Text;

namespace WebSurface
{
    public partial class PhoneYZM : System.Web.UI.Page
    {
        public static string codes;//验证码
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int code1 = 6;
            string str = TextBox1.Text;
            var codes = PhoneTool.CreateRandomCode(code1);
            SendDL(new string[] { "+86" + str }, codes, 10);
            Label1.Text = codes;
           
        }
        protected void SendCodeBtn_Command(object sender, CommandEventArgs e)
        {
            int codeCount = 6;
            string str = TextBox1.Text;
            codes = PhoneTool.CreateRandomCode(codeCount);
            SendDL(new string[] { "+86" + str }, codes, 10);
            Button1.Text = "重新获取验证码";
            Label1.Text = codes;
        }
        /// 
        /// 生成随机验证码
        /// 
        /// 验证码位数
        /// 
        public static string CreateRandomCode(int codeCount)
        {
            StringBuilder randomCode = new StringBuilder();
            Random rand = new Random();
            for (int i = 0; i < codeCount; i++)
            {
                randomCode.Append(rand.Next(10));
            }
            return randomCode.ToString();
        }
        public static void SendDL(string[] PhoneNumber, string code, int Time)
        {
            try
            {
                Credential cred = new Credential
                {
                    SecretId = "", //在腾讯云官网中的“云产品”中找到“访问秘钥”,点击打开,就看得到相关ID和Key,复制填写即可
                    SecretKey = ""
                };
                ClientProfile clientProfile = new ClientProfile();
                HttpProfile httpProfile = new HttpProfile();
                httpProfile.Endpoint = ("sms.tencentcloudapi.com");
                clientProfile.HttpProfile = httpProfile;
                SmsClient client = new SmsClient(cred, "", clientProfile);
                SendSmsRequest req = new SendSmsRequest();
                req.PhoneNumberSet = PhoneNumber;
                req.TemplateID = "";//创建正文模板ID
                req.SmsSdkAppid = "";//在腾讯云官网中的“云产品”中找到“访问秘钥”,点击打开,就会有一个APPID,复制填写即可
                req.Sign = "";//您的公众号名字
                req.TemplateParamSet = new String[] { code, Time.ToString() };
                SendSmsResponse resp = client.SendSmsSync(req);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            return;
        }
        /// 
        /// 提交按钮事件,如果文本框等于获取到的手机验证码即成功否则失败
        /// 
        /// 
        /// 
        protected void Button2_Click(object sender, EventArgs e)
        {
            if (TextBox2.Text==Label1.Text)
            {
                Label1.Text = "成功";
            }
            else
            {
                Label1.Text = "失败";

            }
        }
    }
}

3.PhoneTool.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace WebSurface
{
    public class PhoneTool
    {
        public static string CreateRandomCode(int codeCount)
        {
            StringBuilder randomCode = new StringBuilder();
            Random rand = new Random();
            for (int i = 0; i < codeCount; i++)
            {
                randomCode.Append(rand.Next(10));
            }
            return randomCode.ToString();
        }
    }
}

最终呈现:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/910703.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号