首页 > 微信 > 详细

微信通过encryptedData,iv,Code获取用户信息

时间:2021-06-17 09:29:35      阅读:236      评论:0      收藏:0      [点我收藏+]
   #region 通过encryptedate,iv,code获取用户信息返回登录状态
        /// <summary>
        /// 判断用户是否存在-如果不存在保存用户信息,如果存在泽不需要更新
        /// </summary>
        /// <param name="encryptedData"></param>
        /// <param name="iv"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetWxinfo(string encryptedData, string iv, string Code)
        {

            string Appid = "wx4feb6bfe43b18822";
            string Secret = "012857c1e1fcb98496b8c5ba9d44f544";
            string grant_type = "authorization_code";//默认值

            //向微信服务端 使用登录凭证 code 获取 session_key 和 openid   
            string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + Appid + "&secret=" + Secret + "&js_code=" + Code + "&grant_type=" + grant_type;
            string type = "utf-8";
            
            string obj = Toolkits.GetUrltoHtml(url, type);//获取微信服务器返回字符串  
            //将字符串转换为json格式  







调用方法下方
 #region 获取链接返回数据,调用url,获取链接返回数据
        /// <summary>  
        /// 获取链接返回数据  
        /// </summary>  
        /// <param name="Url">链接</param>  
        /// <param name="type">请求类型</param>  
        /// <returns></returns>  
        public static string GetUrltoHtml(string Url, string type)
        {
            try
            {
                System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
                System.Net.WebResponse wResp = wReq.GetResponse();
                System.IO.Stream respStream = wResp.GetResponseStream();
                using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.GetEncoding(type)))
                {
                    return reader.ReadToEnd();
                }
            }
            catch (System.Exception ex)
            {
                return ex.Message;
            }
        }
        #endregion




        #region 微信小程序用户数据解密  

        public static string AesKey;
        public static string AesIV;

        /// <summary>  
        /// AES解密  
        /// </summary>  
        /// <param name="inputdata">输入的数据encryptedData</param>  
        /// <param name="AesKey">key</param>  
        /// <param name="AesIV">向量128</param>  
        /// <returns name="result">解密后的字符串</returns>  
        public static string AESDecrypt(string inputdata)
        {
            try
            {
                AesIV = AesIV.Replace(" ", "+");
                AesKey = AesKey.Replace(" ", "+");
                inputdata = inputdata.Replace(" ", "+");
                byte[] encryptedData = Convert.FromBase64String(inputdata);

                RijndaelManaged rijndaelCipher = new RijndaelManaged();
                rijndaelCipher.Key = Convert.FromBase64String(AesKey); // 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(encryptedData, 0, encryptedData.Length);
                string result = Encoding.UTF8.GetString(plainText);

                return result;
            }
            catch (Exception)
            {
                return null;

            }
        }
        #endregion

 


            JObject jsonlist = (JObject)JsonConvert.DeserializeObject(obj);
            WxModel wxres = new WxModel();
            try
            {
                //微信服务器验证成功  
                wxres.openid = jsonlist["openid"].ToString();
                wxres.session_key = jsonlist["session_key"].ToString();
            }
            catch (Exception)
            {
                //微信服务器验证失败  
                wxres.errcode = jsonlist["errcode"].ToString();
                wxres.errmsg = jsonlist["errmsg"].ToString();
            }
            //判断返回的oepnID知否存在
            if (string.IsNullOrEmpty(wxres.openid)) 
            {
                //用户数据解密  
                Toolkits.AesIV = iv;
                Toolkits.AesKey = wxres.session_key;
                var result = Toolkits.AESDecrypt(encryptedData);
                //存储用户数据  
                JObject _usrInfo = (JObject)JsonConvert.DeserializeObject(result);
                //userInfo userInfo = new userInfo();
                //userInfo.openId = _usrInfo["openId"].ToString();
            }
                

 

微信通过encryptedData,iv,Code获取用户信息

原文:https://www.cnblogs.com/SDdemon/p/14891654.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!