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

第一步:创建一个一般处理程序,创建一个全局变量url用来接收参数化了之后的URL地址
然后在ProcessRequest(HttpContext context)函数中调用下面的函数signatureURL();
第二步:把这代码拷进去,OK直接去浏览器地址栏请求这个URL就好了
private void signatureURL()
        {
            string HTTP_METHOD = "GET";
            IDictionary parameterDic = new Dictionary();

            // 加入请求公共参数
            parameterDic.Add("Version", "2017-03-21");
            parameterDic.Add("AccessKeyId", "*******"); //此处请替换成您自己的AccessKeyId
            parameterDic.Add("TimeStamp", "2015-05-14T09:03:45Z");//此处将时间戳固定只是测试需要,这样此示例中生成的签名值就不会变,方便您对比验证,可变时间戳的生成需要用下边这句替换
            //parameterDic.Add("TimeStamp", DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ"));//2005-11-05 14:23:23Z); 
            parameterDic.Add("SignatureMethod", "HMAC-SHA1");
            parameterDic.Add("SignatureVersion", "1.0");
            //parameterDic.Add("SignatureNonce", "12312312312");//此处将唯一随机数固定只是测试需要,这样此示例中生成的签名值就不会变,方便您对比验证

            parameterDic.Add("SignatureNonce", System.Guid.NewGuid().ToString());
            parameterDic.Add("Format", "XML");


            // 加入方法特有参数
            parameterDic.Add("Action", "GetVideoPlayAuth");
            parameterDic.Add("VideoId", "32651e8b04b04a2caceaec5452bdedce");

            // 对参数进行排序
            var dicSort = from objDic in parameterDic orderby objDic.Key select objDic;

            // 生成stringToSign字符
            string SEPARATOR = "&";
            string EQUAL = "=";
            StringBuilder stringToSign = new StringBuilder();
            stringToSign.Append(HTTP_METHOD).Append(SEPARATOR); ;
            stringToSign.Append(percentEncode("/")).Append(SEPARATOR);

            StringBuilder canonicalizedQueryString = new StringBuilder();
            foreach (KeyValuePair kvp in dicSort)
            {
                // 此处需要对key和value进行编码
                canonicalizedQueryString.Append(SEPARATOR).Append(percentEncode(kvp.Key)).Append(EQUAL).Append(percentEncode(kvp.Value));
            }

            // 此处需要对canonicalizedQueryString进行编码
            stringToSign.Append(percentEncode(canonicalizedQueryString.ToString().Substring(1)));

            string str_ToSign = stringToSign.ToString();

            byte[] byt_key = UTF8Encoding.UTF8.GetBytes("dMQfV5GaF3EJhraSN4ZevJlMGu7J7r&");
            //byte[] byt_key = UTF8Encoding.UTF8.GetBytes("fdFww0u5zcZhcvi1X+3KJ5bN1VY=&");//请使用您自己的key和secret

            //将StringToSign转化为Byte数组
            byte[] byt_ToSign = UTF8Encoding.UTF8.GetBytes(str_ToSign);

            //设置HMAC SHA1的密钥(Access Key Secret的Byte数组)
            HMACSHA1 hmac = new HMACSHA1(byt_key);
            //进行哈希运算
            byte[] hashValue = hmac.ComputeHash(byt_ToSign);
            //进行Base64编码
            string signature = Convert.ToBase64String(hashValue);
            //对生成的签名进行URL编码(UTF8)
            signature = HttpUtility.UrlEncode(signature, Encoding.UTF8);
            //构建完整的URL

            string signed_url = signature;

            // 生成请求URL
            StringBuilder requestURL;
            requestURL = new StringBuilder("http://vod.cn-shanghai.aliyuncs.com?");
            requestURL.Append(percentEncode("Signature")).Append("=").Append(signature);

            foreach (var item in dicSort)
            {
                requestURL.Append("&").Append(percentEncode(item.Key)).Append("=").Append(percentEncode(item.Value));
            }
            url = requestURL.ToString();
            Console.WriteLine(requestURL.ToString());
        } 

public static string percentEncode(String value)
        {
            StringBuilder stringBuilder = new StringBuilder();
            string text = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
            byte[] bytes = Encoding.GetEncoding("UTF-8").GetBytes(value);
            foreach (char c in bytes)
            {
                if (text.IndexOf(c) >= 0)
                {
                    stringBuilder.Append(c);
                }
                else
                {
                    stringBuilder.Append("%").Append(
                        string.Format(CultureInfo.InvariantCulture, "{0:X2}", (int)c));
                }
            }
            return stringBuilder.ToString();
        }

你可能感兴趣的:(阿里云产品运用)