#region 獲取微信手機號 /// <summary> /// 獲取微信手機號 /// </summary> /// <param name="aesIv">向量</param> /// <param name="encryptedData">encryptedData</param> /// <param name="code">加密數據</param> /// <returns></returns> [HttpGet] [RespWrapFilter] //框架引用,用於返回Json串 public wechardetails<watermark> GetPhoneNumber(string aesIv, string encryptedData, string code) { try { #region 獲取Session_Key var AppId = ""; //微信小程序AppID var Secret = ""; //微信小程序Secret string serviceAddress = "https://api.weixin.qq.com/sns/jscode2session?appid=" + AppId + "&secret=" + Secret + "&js_code=" + code + "&grant_type=authorization_code"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress); request.Method = "GET"; request.ContentType = "text/html;charset=utf-8"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, System.Text.Encoding.UTF8); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); var obj = new { data = retString, Success = true }; Formatting microsoftDataFormatSettings = default(Formatting); string result = JsonConvert.SerializeObject(obj, microsoftDataFormatSettings); //序列化獲取session_key v_petminiuserdetails item = JsonConvert.DeserializeObject<v_petminiuserdetails>(retString); #endregion #region 獲取微信綁定手機號 //判斷是不是16位 若是不夠補0 //text = tests(text); //16進制數據轉換成byte byte[] encryptedDatas = Convert.FromBase64String(encryptedData); // strToToHexByte(text); RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Key = Convert.FromBase64String(item.session_key); // Encoding.UTF8.GetBytes(AesKey); rijndaelCipher.IV = Convert.FromBase64String(aesIv);// Encoding.UTF8.GetBytes(AesIV); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; ICryptoTransform transform = rijndaelCipher.CreateDecryptor(); byte[] plainText = transform.TransformFinalBlock(encryptedDatas, 0, encryptedDatas.Length); string results = Encoding.Default.GetString(plainText); //序列化獲取手機號碼 wechardetails<watermark> getPhone = JsonConvert.DeserializeObject<wechardetails<watermark>>(results); getPhone.watermark.openid = item.openid; //獲取用戶OpenID return getPhone; #endregion } catch (Exception ex) { return null; } } #endregion //實體Model //獲取用戶openid、session_key public class v_petminiuserdetails { /// <summary> /// 微信用戶openId /// </summary> public string openid { get; set; } /// <summary> /// session_key /// </summary> public string session_key { get; set; } } //獲取用戶手機號 public class wechardetails<T> { /// <summary> /// 手機號 /// </summary> public string phoneNumber { get; set; } /// <summary> /// 區域手機號 /// </summary> public string purePhoneNumber { get; set; } /// <summary> /// 區碼 /// </summary> public string countryCode { get; set; } public T watermark { get; set; } } public class watermark { /// <summary> /// 時間戳 /// </summary> public string timestamp { get; set; } /// <summary> /// 用戶appid /// </summary> public string appid { get; set; } /// <summary> /// 用戶openid /// </summary> public string openid { get; set; } }