.net服務端獲取微信小程式使用者資訊
阿新 • • 發佈:2018-12-01
1
public void RequestWxUserInfo(string code, string iv, string encryptedData, string rawData, string signature) 2 { 3 string result = AES_decrypt(encryptedData, code, iv); 4 Dictionary<string, object> requestparmsPost = FormatRequestData(result); 5 Dictionary<string, object> watermark = FormatRequestData(requestparmsPost["watermark"]); 6 string timestamp = watermark["timestamp"] == null ? "" : watermark["timestamp"].ToString(); 7 string appid = watermark["appid"] == null ? "" : watermark["appid"].ToString(); 8 9 wxAccounts u = Newtonsoft.Json.JsonConvert.DeserializeObject<wxAccounts>(result);10 u.appid = appid; 11 u.iv = iv; 12 13 string log = "\r\n result:" + result; 14 log += "\r\n code:" + code; 15 log += "\r\n encryptedData:" + encryptedData; 16 log += "\r\n result:" + result; 17 log += "\r\n timestamp:" + timestamp; 18 log += "\r\n User_appid:" + u.appid; 19 log += "\r\n User_avatarUrl:" + u.avatarUrl; 20 log += "\r\n User_city:" + u.city; 21 log += "\r\n User_country:" + u.country; 22 log += "\r\n User_gender:" + u.gender; 23 log += "\r\n User_iv:" + u.iv; 24 log += "\r\n User_nickName:" + u.nickName; 25 log += "\r\n User_openId:" + u.openId; 26 log += "\r\n User_province:" + u.province; 27 log += "\r\n User_unionId:" + u.unionId; 28 log += "\r\n rawData:" + rawData; 29 log += "\r\n signature:" + signature; 30 31 Proj.Library.Log.WriteLogInfo("微信小程式", new Exception(log)); 32 33 } 34 35 /// <summary> 36 /// encryptedData 解密 37 /// </summary> 38 /// <param name="encryptedDataStr"></param> 39 /// <param name="key"></param> 40 /// <param name="iv"></param> 41 /// <returns></returns> 42 public string AES_decrypt(string encryptedDataStr, string key, string iv) 43 { 44 RijndaelManaged rijalg = new RijndaelManaged(); 45 //----------------- 46 //設定 cipher 格式 AES-128-CBC 47 48 rijalg.KeySize = 128; 49 50 rijalg.Padding = PaddingMode.PKCS7; 51 rijalg.Mode = CipherMode.CBC; 52 53 rijalg.Key = Convert.FromBase64String(key); 54 rijalg.IV = Convert.FromBase64String(iv); 55 56 57 byte[] encryptedData = Convert.FromBase64String(encryptedDataStr); 58 //解密 59 ICryptoTransform decryptor = rijalg.CreateDecryptor(rijalg.Key, rijalg.IV); 60 61 string result = ""; 62 63 using (MemoryStream msDecrypt = new MemoryStream(encryptedData)) 64 { 65 using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) 66 { 67 using (StreamReader srDecrypt = new StreamReader(csDecrypt)) 68 { 69 70 result += srDecrypt.ReadToEnd(); 71 } 72 } 73 } 74 75 return result; 76 } 77 78 protected Dictionary<string, object> FormatRequestData(object data) 79 { 80 return Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(data.ToString()); 81 }
附圖小程式app.js: