腾讯云短信服务使用记录与.NET Core C#代码分享
来源:互联网 发布:淘宝网假货多吗 编辑:程序博客网 时间:2024/06/05 14:09
1、即使是相同的短信签名与短信正文模板,也需要针对“国内文本短信”与“海外文本短信”分别申请。开始不知道,以为只要申请一次,给国外手机发短信时给api传对应的国家码就行,后来才发现需要分别申请。
2、短信服务web api响应“手机号内容频率限制”错误。这是由于在30秒内向同一手机号多次发送了相同内容的短信,这是腾讯云短信服务的默认限制——“相同内容短信对同一个手机号,30秒内发送短信条数不超过1条”,可以通过“应用配置”的“短信频率配置”修改这个限制。
3、腾讯云短信服务没有提供 .NET Core 的 SDK,我们自己实现的代码如下:
public class TencentCloudSmsService : ISmsService
{
private static readonly HttpClient _httpClient =
new HttpClient { BaseAddress = new Uri("https://yun.tim.qq.com") };
private readonly string _appId;
private readonly string _appKey;
private const string SIGNATURE = "...";
private const int DOMESTIC_TEMPLATE_ID = 1234;
private const int OVERSEA_TEMPLATE_ID = 5678;
private readonly ILogger _logger;
public TencentCloudSmsService(IConfiguration conf,
ILoggerFactory loggerFactory)
{
_appId = conf["tencentCloudSms:appId"];
if (string.IsNullOrEmpty(_appId))
throw new ArgumentException($"{nameof(_appId)} must have a value");
_appKey = conf["tencentCloudSms:appKey"];
if (string.IsNullOrEmpty(_appKey))
throw new ArgumentException($"{nameof(_appKey)} must have a value");
_logger = loggerFactory.CreateLogger<TencentCloudSmsService>();
}
public async Task<bool> SendCode(string countryCode, long mobile, int code)
{
var random = GetRandom();
var timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
var data = new
{
tel = new { nationcode = countryCode.Replace("+", ""), mobile = mobile.ToString() },
sign = SIGNATURE,
tpl_id = countryCode == "+86" ? DOMESTIC_TEMPLATE_ID : OVERSEA_TEMPLATE_ID ,
@params = new[] { code.ToString() },
sig = ComputeSignature(mobile, random, timestamp),
time = timestamp,
extend = "",
ext = ""
};
var url = $"/v5/tlssmssvr/sendsms?sdkappid={_appId}&random={random}";
_logger.LogDebug("Post to " + _httpClient.BaseAddress + url);
var response = await _httpClient.PostAsJsonAsync<dynamic>(url, data);
_logger.LogDebug("Post data:\n" + JsonConvert.SerializeObject(data));
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadAsAsync<dynamic>();
if(result.result != 0)
{
_logger.LogError($"Failed to send message to {countryCode}-{mobile}: {result.errmsg}");
return false;
}
return true;
}
private string ComputeSignature(long mobile, int random, long timestamp)
{
var input = $"appkey={_appKey}&random={random}&time={timestamp}&mobile={mobile}";
var hasBytes = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(input));
return string.Join("", hasBytes.Select(b => b.ToString("x2")));
}
private int GetRandom()
{
return new Random().Next(100000, 999999);
}
}
原文地址:http://www.cnblogs.com/dudu/p/7782376.html
.NET社区新闻,深度好文,欢迎访问公众号文章汇总 http://www.csharpkit.com
- 腾讯云短信服务使用记录与.NET Core C#代码分享
- 腾讯视频分享代码
- .NET Core快速入门教程 5、使用VS Code进行C#代码调试的技巧
- iOS使用系统服务-短信与邮件
- 使用ContentResolver读取通话记录与短信记录
- .net core注入服务
- 来腾讯云开发者实验室 学习.NET Core 2.0
- 腾讯陈军:腾讯云平台与技术实践分享
- Asp.net core中Migration工具使用的交流分享
- 使用Web服务将C#代码转换为VB.NET代码
- 解决.NET Core中MailKit无法使用阿里云邮件推送服务的问题
- 腾讯云SDK 与 腾讯Bugly集成冲突记录
- C# .Net短信验证
- .NET Core下使用gRpc公开服务(SSL/TLS)
- .NET Core下使用gRpc公开服务(SSL/TLS)
- 使用阿里云的短信服务发送短信验证码
- 【代码笔记】iOS-短信分享
- Core C# and .NET
- PWM调制原理及仿真
- 字幕字体滚动插件——scroxt.js
- 打印素数、输出乘法口诀、判断闰年
- 【实验手册】使用Visual Studio Code 开发.NET Core应用程序
- 使用Identity Server 4建立Authorization Server (2)
- 腾讯云短信服务使用记录与.NET Core C#代码分享
- Vc6.0链接Mysql数据库的几点问题
- 对于spring的理解
- 欢迎使用CSDN-markdown编辑器
- eclipse中安装freemarker插件及ftl使用freemarker编辑器--在线安装方法
- VTK学习(六)纹理映射
- 智能象棋机器人设计之机械臂——相关设计参考
- STL一时爽(全家火葬场)
- git mv $ git rm