【第三方登陆】之微信第三方登陆

微信太会赚钱了,要用这个开放平台的登陆连接,收了300元大洋啊。虽然公司出钱,可是别人大公司赚钱分分钟到手啊。api

 

https://open.weixin.qq.com/connect/qrconnect?appid=appid&redirect_uri=redirect_uri&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect微信

 

首先,页面的连接,把这个连接套在A标签下,打开就是微信自动生成的一个二维码。 PS:记得把APPID换掉,还有返回地址也换掉。session

还有,APPID必定要用开放平台下:管理中心——》网站应用——》你建立的应用下的APPIDapp

 

被虐了很久,用的错(公众平台的)APPID。告知我,Scope参数错误或没有Scope权限。微信公众平台

        网站微信第三方登陆里面,用的是 微信开放平台  中申请的 APPID 和 APPSECRET。网站

 

2015年8月25日11:06:13url

        有点不明白的是,微信公众平台和微信开放平台获取的两个用户OPENID的值,居然不同。这让人有点费解了?spa

 

       回调地址下的代码以下:code

 

 public partial class WeiXinLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            WeiXinLoginAPI();
        }

        public const string APPID = "";
        public const string APPSECRET = "";

        public void WeiXinLoginAPI()
        {
            string errorCode = "微信登陆过时,请从新登陆!";
            var code = Request.QueryString["Code"];
            try
            {
                if (!string.IsNullOrEmpty(code))
                {
                   
                    var client = new System.Net.WebClient();
                    client.Encoding = System.Text.Encoding.UTF8;
                    var url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", APPID, APPSECRET, code);
                    var data = Tools.GetPage(url);
                    Log.Debug("第一个接口", data);
                    var serializer = new JavaScriptSerializer();
                    var obj = serializer.Deserialize<Dictionary<string, string>>(data);
                    string accessToken;
                    if (!obj.TryGetValue("access_token", out accessToken))
                        return;

                    var opentid = obj["openid"];
                    url = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", accessToken, opentid);
                    data = Tools.GetPage(url);
                    Log.Debug("第二个接口", data);
                    var userInfo = serializer.Deserialize<Dictionary<string, object>>(data);
                    #region 同步数据到您的平台操做 Begin
                    if (opentid != null && userInfo["nickname"] != null)
                    {
                        try
                        {
                                Users _MTUsers =  GetModel( openid);  //获取用户信息
                  
                                if (_MTUsers == null)
                                {
                                  Insert(_MTUsers);  //若是不存在,建立
                                }
                                else
                                {
                                    _MTUsers.LastLogin = DateTime.Now;
                                    _MTUsers.LastUpdated = DateTime.Now;
                                    Instance.MTUsers.Business.Update(_MTUsers);  //存在则修改
                                }
                                Session["CurrentUser"] = _MTUsers;               //将当前登陆的用户放入session中
                        }
                        catch (Exception ex)
                        {
                            Response.Write("异常信息:" + ex.ToString());
                        }

                    }
                    else
                    {
                        errorCode = "微信登陆身份过时";
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                Log.Debug("【异常】", errorCode + ";异常信息:" + ex.Message.ToString());
            }
          }
 }
相关文章
相关标签/搜索