签名生成规则如下:
参与签名的字段包括noncestr(随机字符串), 有效的jsapi_ticket, timestamp(时间戳), url(当前网页的URL,不包含#及其后面部分)。
1、对所有待签名参数按照字典序排序,使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串string1(这里需要注意的是所有参数名均为小写字符);
2、对string1作sha1加密,字段名和字段值都采用原始值,不进行URL 转义。
对应的:
class="brush:csharp;gutter:true;">string string1 = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + noncestr+ "×tamp=" + timestamp + "&url=" + url;
和:
string signature = SHA1_Encrypt(string1);
public static string SHA1_Encrypt(string Source_String)
{
    byte[] StrRes = Encoding.Default.GetBytes(Source_String);
    HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
    StrRes = iSHA.ComputeHash(StrRes);
    StringBuilder EnText = new StringBuilder();
     foreach (byte iByte in StrRes)
    {
        EnText.AppendFormat("{0:x2}", iByte);
    }
    return EnText.ToString();
}