.net服务端获取微信小程序用户信息

时间:2024-02-25 16:49:28
 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:

 

 

二次更新:

编译的时候错误提示:https://api.weixin.qq.com/sns/jscode2session?appid=wx*********&secret=****37387043***&js_code=logincode&grant_type=authorization_code,这个请求地址不在安全域名列表中,官方给出的回答是为了安全起见,此接口请求只允许在服务端执行,调整后的代码如下:

 

 服务端请求微信api获取session_key和openid代码:

 1  public void RequestWxApi(string logincode)
 2         {
 3             string url = "https://api.weixin.qq.com/sns/jscode2session?appid=wx**********&secret=789******3e&js_code=" + logincode + "&grant_type=authorization_code";
 4             HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
 5             request.Method = "Post";
 6 
 7             request.ContentType = "application/json";
 8             byte[] databody = System.Text.Encoding.UTF8.GetBytes("");//发送主体
 9             using (Stream stream = request.GetRequestStream()) { stream.Write(databody, 0, databody.Length); }
10 
11             //获取结果
12             StreamReader srdPreview = new StreamReader(request.GetResponse().GetResponseStream());
13             string result = srdPreview.ReadToEnd();
14             srdPreview.Close();
15 
16             //Library.Log.WriteLogInfo("RequestWxApi", new Exception(result));
17 
18             CallBackData(true, "", result);
19         }