zoukankan      html  css  js  c++  java
  • 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);
            }
    Slowly I find myself
  • 相关阅读:
    固定sql语句传参批量查询数据库脚本
    超多行数据纵向编辑
    takes 3 positional arguments but 4 were given错误
    使用PMD进行代码审查(转)
    WebADI应用到Office 2016 64-bit
    SVN 提交代码时强制加入注释内容
    DOCKER初体验
    "make_path" is not exported by the File::Path modul
    perl 调用shell脚本
    scp 上传文件到多个服务器节点
  • 原文地址:https://www.cnblogs.com/SDdemon/p/14891654.html
Copyright © 2011-2022 走看看