本文基于asp.net实现输入手机号发送验证码,并得到验证码。我将详细的在下面说明,每一步都很重要,谨慎去做,不然没过审核的话得等好久才能通过。如果是做项目或者个人研究需要那么就学我这种方法就足够了。
关于使用MVC进行短信验证,作者也都写好啦,有需要的小伙伴可以去作者主页瞅瞅~
1.首先,必须先申请一个微信公众号(这是第一步,必须要先申请完成,不然后续无法操作)
创建微信公众号可以去百度上搜如何创建,创建的要是个人公众号
申请步骤:1.搜索WX公众平台。2.进行注册公众号的申请就可以了。
2.第一步完成后接下来去百度搜索腾讯云,进入官网
登录后搜索短信,点击免费试用,如图:
进去后找到国内短信---签名管理--点击创建签名后进行资料填写,如图:
根据里面的提示填写好资料。在证明上传那一栏上传自己的公众号信息,公众号信息在你创建好了的公众号里面找,找到设置于开发----公众号设置然后截图保存一下进行上传就可以了,上传图片如下:
3.签名管理资料提交完成后等待审核通过即可,接下来找到正文模板管理----创建正文模板即可
注:这一步需要等到签名管理审核通过后才能申请不然不会通过的,所以等签名管理申请成功后再创建正文模板,依然按照里面提示内容提交资料即可。
4.等待签名管理和正文模板审核通过后(必须)
然后导入TencentCloud文件放到项目中
TencentCloud 点此下载 提取码:2580
然后找到里面的Send.cs文件打开:
打开后按照注释填写自己的相关内容即可
/// <summary>
/// 发送短信
/// </summary>
/// <param name="PhoneNumber">发送手机</param>
/// <param name="code">验证码</param>
/// <param name="Time">有效时间</param>
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" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="发送" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button2" runat="server" Text="提交" OnClick="Button2_Click" style="height: 27px" />
</form>
</body>
</html>
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;
}
/// <summary>
/// 生成随机验证码
/// </summary>
/// <param name="codeCount">验证码位数</param>
/// <returns></returns>
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;
}
/// <summary>
/// 提交按钮事件,如果文本框等于获取到的手机验证码即成功否则失败
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
}
}
最终呈现: