微信PC登录的代码

我用的是js显示二维码,因此必须先引用一个js

前面所有的准备工作做好,然后就看代码。

这个就是显示二维码的方法,如果不知道传进来的appid和url是什么,看下面

现在就应该懂了,这两个都可以通过后台传给前台隐藏控件

然后就是二维码的显示存放的地方和appid,url的隐藏控件

这样,你就可以成功的让你的二维码显示出来。

然后就是获取用户信息的方法了。

 protected void Page_Load(object sender, EventArgs e)
        {
                string code = Request.QueryString["code"] ?? "";
                 redirectUrl = "http://" + Request.Url.Host + Request.Url.PathAndQuery;
                  //根据获取的code来获取字典,并将返回值都存在字典中
                            //微信开放平台
                  Dictionary<string, object> dic=GetOpenInfoByCode(code, Config.OpenAppId, Config.OpenAppSecret);
 //根据token,openid获取用户信息
                            Dictionary<string, object> userInfo = GetUserInfo(dic["access_token"].ToString(), dic["openid"].ToString());
                            userName = userInfo["nickname"].ToString();
                            imageUrl = userInfo["headimgurl"].ToString();
                            userName = userInfo["nickname"].ToString();
                            imageUrl = userInfo["headimgurl"].ToString()
}
 /// <summary>
        /// 【微信开放平台】,【微信公众平台】
        /// 根据code获取token、openid、unionid
        /// </summary>
        /// <param name="code">code</param>
        public Dictionary<string, object> GetOpenInfoByCode(string code, string appid, string secret)
        {
            //获得配置信息
            string send_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" +
                               appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
            //发送并接受返回值

            string result = new CRM.Common.DotNetRequest.RequestHelper().HttpGet(send_url);
            new LogHelper().WriteLog("调试-获取token,返回结果:result" + result);

            if (result.Contains("errmsg"))
            {
                new LogHelper().WriteLog(result);
            }
            try
            {
                new LogHelper().WriteLog(result);
                Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(result);
                return dic;
            }
            catch (Exception ex)
            {
                new LogHelper().SaveLog(ex, "异常-获取token,调用参数为:code=" + code + "result" + result);
                return null;
            }
        }
 /// <summary>
        /// 【微信开放平台】,【微信公众平台】
        /// 根据token、openid获取用户详细信息
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="openid"></param>
        /// <returns></returns>
        public Dictionary<string, object> GetUserInfo(string access_token, string openid)
        {
            string send_url = "https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openid;
            string result = new CRM.Common.DotNetRequest.RequestHelper().HttpGet(send_url);
            new LogHelper().WriteLog("调试-获取用户信息,返回结果:result:" + result);
            if (result.Contains("errmsg"))
            {
                new LogHelper().WriteLog(result);
            }
            try
            {
                new LogHelper().WriteLog(result);
                Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(result);
                return dic;
            }
            catch (Exception ex)
            {
                new LogHelper().SaveLog(ex, "异常-获取获取用户信息,调用参数为:access_token:" + access_token + "openid:" + openid);
                return null;
            }
        }
View Code

 将name和url传到前台

 

posted @ 2017-05-22 16:32  大白菜不怕猪拱  阅读(417)  评论(0编辑  收藏  举报