当前位置:   article > 正文

.net版本的阿里云视频点播获取视频播放凭证的方法加签名算法_alibabacloud.sdk.vod20170321 getvideolistwithoptio

alibabacloud.sdk.vod20170321 getvideolistwithoptions 生成签名凭证
第一步:创建一个一般处理程序,创建一个全局变量url用来接收参数化了之后的URL地址
然后在ProcessRequest(HttpContext context)函数中调用下面的函数signatureURL();
第二步:把这代码拷进去,OK直接去浏览器地址栏请求这个URL就好了
  1. private void signatureURL()
  2. {
  3. string HTTP_METHOD = "GET";
  4. IDictionary<String, String> parameterDic = new Dictionary<string, string>();
  5. // 加入请求公共参数
  6. parameterDic.Add("Version", "2017-03-21");
  7. parameterDic.Add("AccessKeyId", "*******"); //此处请替换成您自己的AccessKeyId
  8. parameterDic.Add("TimeStamp", "2015-05-14T09:03:45Z");//此处将时间戳固定只是测试需要,这样此示例中生成的签名值就不会变,方便您对比验证,可变时间戳的生成需要用下边这句替换
  9. //parameterDic.Add("TimeStamp", DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ"));//2005-11-05 14:23:23Z);
  10. parameterDic.Add("SignatureMethod", "HMAC-SHA1");
  11. parameterDic.Add("SignatureVersion", "1.0");
  12. //parameterDic.Add("SignatureNonce", "12312312312");//此处将唯一随机数固定只是测试需要,这样此示例中生成的签名值就不会变,方便您对比验证
  13. parameterDic.Add("SignatureNonce", System.Guid.NewGuid().ToString());
  14. parameterDic.Add("Format", "XML");
  15. // 加入方法特有参数
  16. parameterDic.Add("Action", "GetVideoPlayAuth");
  17. parameterDic.Add("VideoId", "32651e8b04b04a2caceaec5452bdedce");
  18. // 对参数进行排序
  19. var dicSort = from objDic in parameterDic orderby objDic.Key select objDic;
  20. // 生成stringToSign字符
  21. string SEPARATOR = "&";
  22. string EQUAL = "=";
  23. StringBuilder stringToSign = new StringBuilder();
  24. stringToSign.Append(HTTP_METHOD).Append(SEPARATOR); ;
  25. stringToSign.Append(percentEncode("/")).Append(SEPARATOR);
  26. StringBuilder canonicalizedQueryString = new StringBuilder();
  27. foreach (KeyValuePair<string, string> kvp in dicSort)
  28. {
  29. // 此处需要对key和value进行编码
  30. canonicalizedQueryString.Append(SEPARATOR).Append(percentEncode(kvp.Key)).Append(EQUAL).Append(percentEncode(kvp.Value));
  31. }
  32. // 此处需要对canonicalizedQueryString进行编码
  33. stringToSign.Append(percentEncode(canonicalizedQueryString.ToString().Substring(1)));
  34. string str_ToSign = stringToSign.ToString();
  35. byte[] byt_key = UTF8Encoding.UTF8.GetBytes("dMQfV5GaF3EJhraSN4ZevJlMGu7J7r&");
  36. //byte[] byt_key = UTF8Encoding.UTF8.GetBytes("fdFww0u5zcZhcvi1X+3KJ5bN1VY=&");//请使用您自己的key和secret
  37. //将StringToSign转化为Byte数组
  38. byte[] byt_ToSign = UTF8Encoding.UTF8.GetBytes(str_ToSign);
  39. //设置HMAC SHA1的密钥(Access Key Secret的Byte数组)
  40. HMACSHA1 hmac = new HMACSHA1(byt_key);
  41. //进行哈希运算
  42. byte[] hashValue = hmac.ComputeHash(byt_ToSign);
  43. //进行Base64编码
  44. string signature = Convert.ToBase64String(hashValue);
  45. //对生成的签名进行URL编码(UTF8)
  46. signature = HttpUtility.UrlEncode(signature, Encoding.UTF8);
  47. //构建完整的URL
  48. string signed_url = signature;
  49. // 生成请求URL
  50. StringBuilder requestURL;
  51. requestURL = new StringBuilder("http://vod.cn-shanghai.aliyuncs.com?");
  52. requestURL.Append(percentEncode("Signature")).Append("=").Append(signature);
  53. foreach (var item in dicSort)
  54. {
  55. requestURL.Append("&").Append(percentEncode(item.Key)).Append("=").Append(percentEncode(item.Value));
  56. }
  57. url = requestURL.ToString();
  58. Console.WriteLine(requestURL.ToString());
  59. }

  1. public static string percentEncode(String value)
  2. {
  3. StringBuilder stringBuilder = new StringBuilder();
  4. string text = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
  5. byte[] bytes = Encoding.GetEncoding("UTF-8").GetBytes(value);
  6. foreach (char c in bytes)
  7. {
  8. if (text.IndexOf(c) >= 0)
  9. {
  10. stringBuilder.Append(c);
  11. }
  12. else
  13. {
  14. stringBuilder.Append("%").Append(
  15. string.Format(CultureInfo.InvariantCulture, "{0:X2}", (int)c));
  16. }
  17. }
  18. return stringBuilder.ToString();
  19. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/507622
推荐阅读
相关标签
  

闽ICP备14008679号