public WeChatJsConfiguration GenerateWeChatJsConfigurationWithSignature(string jsTicket, string url)
        {
            var jsConfig = new WeChatJsConfiguration { 
                 NonceString = NonceString(),
                 Timestamp = TimeStamp()
            };

            var dataList = new Dictionary<string, string> {
                {"noncestr", jsConfig.NonceString},
                {"timestamp", jsConfig.Timestamp.ToString() },
                {"jsapi_ticket", jsTicket},
                {"url", url}
            }.ToList();

            dataList.Sort(ParameterKeyComparison);
            var queryString = dataList.Aggregate(string.Empty, (query, item) => string.Concat(query, "&", item.Key, "=", item.Value)).TrimStart('&');

            using (var sha1 = new SHA1CryptoServiceProvider())
            {
                var hashed = sha1.ComputeHash(Encoding.Default.GetBytes(queryString));
                jsConfig.Signature = HexStringFromBytes(hashed);
                return jsConfig;
            }
        }
Example #2
0
        public WeChatJsConfiguration GenerateWeChatJsConfigurationWithSignature(string jsTicket, string url)
        {
            var jsConfig = new WeChatJsConfiguration {
                NonceString = NonceString(),
                Timestamp   = TimeStamp()
            };

            var dataList = new Dictionary <string, string> {
                { "noncestr", jsConfig.NonceString },
                { "timestamp", jsConfig.Timestamp.ToString() },
                { "jsapi_ticket", jsTicket },
                { "url", url }
            }.ToList();

            dataList.Sort(ParameterKeyComparison);
            var queryString = dataList.Aggregate(string.Empty, (query, item) => string.Concat(query, "&", item.Key, "=", item.Value)).TrimStart('&');

            using (var sha1 = SHA1.Create())
            {
                var hashed = sha1.ComputeHash(Encoding.GetEncoding(0).GetBytes(queryString));
                jsConfig.Signature = HexStringFromBytes(hashed);
                return(jsConfig);
            }
        }