C#控制器微信通过encryptedData,iv,Code获取用户信息

        #region 通过encryptedate,iv,code获取用户信息返回登录状态
        /// <summary>
        /// 通过encryptedate,iv,code获取用户信息返回登录状态
        /// </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)
        {
            AjaxResult res;
            string Appid = "wx4feb6bfe43b14444";
            string Secret = "012857c1e1fcb98496b2342244f547";
            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格式  


            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();
                return Error(jsonlist["errcode"].ToString() + wxres.errmsg.ToString());
            }
            //判断返回的oepnID知否存在
            if (!string.IsNullOrEmpty(wxres.openid))
            {

                //用户数据解密  
                Toolkits.AesIV = iv;
                Toolkits.AesKey = wxres.session_key;
                var result1 = Toolkits.AesDecode(encryptedData, wxres.session_key, iv);
//存储用户数据  
                //JObject _usrInfo = (JObject)JsonConvert.DeserializeObject(result);

                //用户实体
                Base_User theData = new Base_User();
                var query = _UserBusiness.GetWxTheData(wxres.openid);
                if (query == null)
                {
                  
                }
                return Success(Code);

                //userInfo userInfo = new userInfo();
                //userInfo.openId = _usrInfo["openId"].ToString();
            }
            else 
            {
                return Error("返回失败了。获取openID失败");
            }

        }

        #endregion

以上是MVC调用的接口的方法

下方是调用的方法,

        #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

        /// 

        /// Aes解密
        /// 

        /// 需要解密的字符串
        /// 密钥,长度不够时空格补齐,超过时从左截取
        /// 偏移量,长度不够时空格补齐,超过时从左截取
        /// 秘钥长度,16 24 32
        /// 解密模式
        /// 填充方式
        /// 
        public static string AesDecode(string str, string key, string iv, int keyLenth = 16, CipherMode aesMode = CipherMode.CBC, PaddingMode aesPadding = PaddingMode.PKCS7)
        {
            if (!new List<int> { 16, 24, 32 }.Contains(keyLenth))
            {
                return null;//密钥的长度,16位密钥 = 128位,24位密钥 = 192位,32位密钥 = 256位。
            }
            var oldBytes = Convert.FromBase64String(str);
            var bKey = new Byte[keyLenth];
            Array.Copy(Convert.FromBase64String(key.PadRight(keyLenth)), bKey, keyLenth);
            var bIv = new Byte[16];
            Array.Copy(Convert.FromBase64String(iv.PadRight(16)), bIv, 16);

            var rijalg = new RijndaelManaged
            {
                Mode = aesMode,
                Padding = aesPadding,
                Key = bKey,
                IV = bIv,
            };
            var decryptor = rijalg.CreateDecryptor(rijalg.Key, rijalg.IV);
            var rtByte = decryptor.TransformFinalBlock(oldBytes, 0, oldBytes.Length);
            return Encoding.UTF8.GetString(rtByte);
        }

 

posted @ 2021-06-16 23:45  SDdemon  阅读(598)  评论(0编辑  收藏  举报