C# MVC 腾讯云 短信验证

0、前提

注册 微信公众号

https://mp.weixin.qq.com/cgi-bin/registermidpage?action=index&lang=zh_CN

使用 微信公众号 创建 腾讯云 短信签名

https://cloud.tencent.com/search/%E7%9F%AD%E4%BF%A1/1_1

1、下载 TencentCloud 文件

CSDN下载

 迅雷下载          提取码:wmpq

百度网盘下载          提取码:lyio

2、创建MVC项目

3、添加现有项目

4、添加引用

 

5、写代码

前端代码自行发挥

后端如下:

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

//导入命名空间
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Sms.V20190711;
using TencentCloud.Sms.V20190711.Models;



//跳转到页面
public ActionResult dd()
{
    return View();
}

//输入手机号,post提交
[HttpPost]
public ActionResult dd(string sjh)
{
    //yzm 随机生成的验证码
    Random rd = new Random();
    string yzm = rd.Next(100000, 1000000).ToString();
    //time 验证码过期时间 单位:分钟
    int time = 5;
    SendDL(new string[] { "+86"+sjh},yzm,time);
    return Content("true");
}
/// <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 = "";//在腾讯云官网中的“云产品”中找到“访问秘钥”,点击打开,就会有一个APPID,复制填写即可
        req.Sign = "";//您的公众号名字
        req.TemplateParamSet = new String[] { code, Time.ToString() };
        SendSmsResponse resp = client.SendSmsSync(req);


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

    return;
}

​

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值