#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; }
}
C# 微信小程序获取绑定手机号
点赞
收藏