參考文章 :微信JS-SDK 權限簽名算法 C#版
這篇文章講解的的比較詳細,而且算法准確,但是這篇文章有幾個錯誤的地方需要注意;
url必須動態生成
url不能寫死,否則就算結果和官方檢測的一致,也只會是無效的
1 string url = Request.Url.ToString();
noncestr必須動態生成
noncestr也是動態獲取的,不能寫死
1 ///<summary> 2 ///生成隨機字符串 3 ///</summary> 4 ///<param name="length">目標字符串的長度</param> 5 ///<param name="useNum">是否包含數字,1=包含,默認為包含</param> 6 ///<param name="useLow">是否包含小寫字母,1=包含,默認為包含</param> 7 ///<param name="useUpp">是否包含大寫字母,1=包含,默認為包含</param> 8 ///<param name="useSpe">是否包含特殊字符,1=包含,默認為不包含</param> 9 ///<param name="custom">要包含的自定義字符,直接輸入要包含的字符列表</param> 10 ///<returns>指定長度的隨機字符串</returns> 11 public static string GetRandomString(int length, bool useNum, bool useLow, bool useUpp, bool useSpe, string custom) 12 { 13 byte[] b = new byte[4]; 14 new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b); 15 Random r = new Random(BitConverter.ToInt32(b, 0)); 16 string s = null, str = custom; 17 if (useNum == true) { str += "0123456789"; } 18 if (useLow == true) { str += "abcdefghijklmnopqrstuvwxyz"; } 19 if (useUpp == true) { str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; } 20 if (useSpe == true) { str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; } 21 for (int i = 0; i < length; i++) 22 { 23 s += str.Substring(r.Next(0, str.Length - 1), 1); 24 } 25 return s; 26 }
官方給取的例子長度為16,含大小寫和數字,沒有特殊字符串
即
var noncestr = GetRandomString(16, true, true, true, false,"");
