using System; using System.Collections.Specialized; using System.Net; class Program { static void Main(string[] args) { string accessKeyId = "<your-access-key-id>"; string accessKeySecret = "<your-access-key-secret>"; string phoneNumber = "<target-phone-number>"; string signName = "<your-signature>"; string templateCode = "<your-template-code>"; string templateParam = "{\"code\":\"123456\"}"; using (WebClient client = new WebClient()) { NameValueCollection values = new NameValueCollection(); values.Add("AccessKeyId", accessKeyId); values.Add("Action", "SendSms"); values.Add("Format", "JSON"); values.Add("PhoneNumbers", phoneNumber); values.Add("RegionId", "cn-hangzhou"); values.Add("SignName", signName); values.Add("SignatureMethod", "HMAC-SHA1"); values.Add("SignatureNonce", Guid.NewGuid().ToString()); values.Add("SignatureVersion", "1.0"); values.Add("TemplateCode", templateCode); values.Add("TemplateParam", templateParam); values.Add("Timestamp", DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ")); values.Add("Version", "2017-05-25"); string sortedQueryString = GetSortedQueryString(values); string stringToSign = "GET&%2F&" + Uri.EscapeDataString(sortedQueryString); string signature = ComputeSignature(stringToSign, accessKeySecret + "&"); string url = "http://dysmsapi.aliyuncs.com/?" + sortedQueryString + "&Signature=" + Uri.EscapeDataString(signature); string response = client.DownloadString(url); Console.WriteLine("SMS sent, response: " + response); } } static string GetSortedQueryString(NameValueCollection values) { var sortedValues = values.AllKeys.Select(key => key + "=" + Uri.EscapeDataString(values[key])); return string.Join("&", sortedValues.OrderBy(s => s)); } static string ComputeSignature(string stringToSign, string accessKeySecret) { using (var hmac = new System.Security.Cryptography.HMACSHA1(Encoding.UTF8.GetBytes(accessKeySecret))) { var hashBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)); return Convert.ToBase64String(hashBytes); } } }