Пример #1
0
 protected void Page_Init(object sender, EventArgs e)
 {
     string url = null;
     WXAccountDAL dal = new WXAccountDAL();
     if (!OpenID.IsNullOrEmpty())
     {
         OAuthUserInfo userInfo = dal.QuerySingleOpenID(OpenID);
         weiXinUserInfo = new WeiXinUserInfo(true, userInfo, null);
     }
     if (ConfigurationManager.AppSettings["IsDebug"].TryParseBool())
     {
         weiXinUserInfo = new WeiXinUserInfo(true, new OAuthUserInfo() { subscribe = 1, openid = "o3QP1jjbjXDBQZgyPsH5V6pwoubY" }, new OAuthAccessTokenResult() { openid = "o3QP1jjbjXDBQZgyPsH5V6pwoubY" });
     }
     try
     {
         if (weiXinUserInfo == null || weiXinUserInfo.UserInfo == null)
         {
             string backUrl = string.Format("{1}/GetUserInfo.aspx?callBack={0}&appID={2}&appSecret={3}",
                 HttpContext.Current.Server.UrlEncode(HttpContext.Current.Request.Url.AbsoluteUri), ConfigurationManager.AppSettings["ClientDeployDomain"],
                 ConfigurationManager.AppSettings["OAuthAppID"], ConfigurationManager.AppSettings["OAuthAppSercet"]);
             url = OAuth.GetAuthorizeUrl(ConfigurationManager.AppSettings["OAuthAppID"], backUrl, "STATE", OAuthScope.snsapi_userinfo);
             //Log.Default.Debug("url:" + url);
             Response.Redirect(url);
         }
     }
     catch (Exception ex)
     {
         Log.Default.Error(ex);
     }
 }
        protected void Page_Load(object sender, EventArgs e)
        {
            string appID = Request["appID"], appSecret = Request["appSecret"], code = Request["code"], callBack = Request["callBack"];
            OAuthAccessTokenResult accessToken = null;
            WXAccountDAL dal = new WXAccountDAL();
            OAuthUserInfo userinfo = null;
            Log.Default.Debug("appID:" + appID);
            Log.Default.Debug("appSecret:" + appSecret);
            Log.Default.Debug("code:" + code);
            Log.Default.Debug("callBack:" + callBack);
            string openid = "";
            try
            {
                accessToken = OAuth.GetAccessToken(appID, appSecret, code);
                Log.Default.Debug("accessToken:" + accessToken.ToJsonString());
                openid = accessToken.openid;
                if (accessToken.scope == "snsapi_userinfo")
                {
                    Snsapi_UserInfo snsapi = OAuth.GetAccessToken(appID, appSecret);
                    userinfo = OAuth.GetUserInfo(snsapi.access_token, openid);
                    OAuthUserInfo tempUI = dal.QuerySingleOpenID(openid);
                    if (userinfo == null || userinfo.subscribe == 0)
                    {
                        try
                        {
                            if (userinfo != null && userinfo.subscribe == 0)
                            {
                                userinfo.nickname = Encoding.GetEncoding("GBK").GetString(Encoding.Default.GetBytes(userinfo.nickname));
                                Session["OpenID"] = openid;
                                userinfo.subscribe = 1;
                                if (tempUI == null)
                                    dal.InsertSQL(userinfo);
                                Response.Redirect("weixin://profile/" + ConfigurationManager.AppSettings["OriginalID"]);
                            }
                            else if (!openid.IsNullOrEmpty())
                            {
                                //Account account = InterfaceControler.GetMyAccount(openid);
                                //userinfo = new OAuthUserInfo();
                                //userinfo.nickname = account.user_realname;
                                //userinfo.headimgurl = account.user_img;
                                //userinfo.openid = openid;
                                //userinfo.subscribe = 1;
                                //if (tempUI == null)
                                //    dal.InsertSQL(userinfo);
                            }
                        }
                        catch (Exception ex)
                        {
                            if (ex.Message == "微信自媒体客户端鉴权失败.")
                            {
                                Response.Redirect(ConfigurationManager.AppSettings["ClientDeployDomain"] + "/BindAccount.aspx");
                            }
                            else
                            {
                                Log.Default.Error(ex);
                            }
                        }
                    }
                    else if (tempUI == null)
                    {
                        dal.InsertSQL(userinfo);
                    }
                    if (tempUI != null && !userinfo.headimgurl.IsNullOrEmpty() && !userinfo.nickname.IsNullOrEmpty())
                    {
                        userinfo.nickname = Encoding.GetEncoding("GBK").GetString(Encoding.Default.GetBytes(userinfo.nickname));
                        tempUI.city = userinfo.city;
                        tempUI.headimgurl = userinfo.headimgurl;
                        tempUI.country = userinfo.country;
                        tempUI.nickname = userinfo.nickname;
                        tempUI.province = userinfo.province;
                        tempUI.sex = userinfo.sex;
                        dal.UpdateSQL(tempUI);
                    }
                    Session["OpenID"] = openid;
                }
            }
            catch (Exception ex)
            {
                Log.Default.Error("获取授权失败!" + ex);
            }
            //CacheHelper.Add(MPOAuth.WEIXIN_USERIDETAIL + "-" + MPOAuth.CurrrentOpenID, new WeiXinUserInfo(true, userinfo, accessToken));

            Response.Redirect(callBack);
        }