.Net asp.net MVC 实现短信验证 您所在的位置:网站首页 手机短信验证码怎么实现 .Net asp.net MVC 实现短信验证

.Net asp.net MVC 实现短信验证

2024-07-11 00:42| 来源: 网络整理| 查看: 265

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

关于使用MVC进行短信验证,作者也都写好啦,有需要的小伙伴可以去作者主页瞅瞅~

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

创建微信公众号可以去百度上搜如何创建,创建的要是个人公众号

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

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

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

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

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

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

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

4.等待签名管理和正文模板审核通过后(必须)

然后导入TencentCloud文件放到项目中 

TencentCloud 点此下载  提取码:2580

然后找到里面的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.前端代码:

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 = "";//在腾讯云官网中搜索短信后找到应用列表,找到“SDKAPPID”,点击打开,就会有一个SDKAPPID,复制填写即可 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(); } } }

最终呈现:



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有