コード例 #1
0
        protected override void ShowPage()
        {
            if (!DiscuzCloud.GetCloudServiceEnableStatus("connect"))
            {
                AddErrLine("QQ登录功能已关闭");
                return;
            }

            switch (action)
            {
            case "access":
                if (!CheckCallbackSignature(DNTRequest.GetString("con_sig")))
                {
                    AddErrLine("非法请求");
                    return;
                }

                OAuthAccessTokenInfo tokenInfo = DiscuzCloud.GetConnectAccessTokenInfo();
                if (tokenInfo == null)
                {
                    AddErrLine("QQ登录过程中出现异常,请尝试再次登录");
                    return;
                }

                userconnectinfo = DiscuzCloud.GetUserConnectInfo(tokenInfo.Openid);
                if (userconnectinfo == null)
                {
                    userconnectinfo        = new UserConnectInfo();
                    userconnectinfo.OpenId = tokenInfo.Openid;
                    userconnectinfo.Token  = tokenInfo.Token;
                    userconnectinfo.Secret = tokenInfo.Secret;
                    userconnectinfo.AllowVisitQQUserInfo = DNTRequest.GetInt("con_is_user_info", 0);
                    userconnectinfo.AllowPushFeed        = DNTRequest.GetInt("con_is_feed", 0) == 1 ? 3 : 0;
                    userconnectinfo.CallbackInfo         = usedusernames + "&" + birthday + "&" + gender + "&" + email;
                    DiscuzCloud.CreateUserConnectInfo(userconnectinfo);
                }
                else if (userconnectinfo.Uid > 0)
                {
                    if (userid > 0)
                    {
                        SetBackLink("index.aspx");
                        AddErrLine(userconnectinfo.Uid != userid ? "该QQ已经绑定了其他帐号" : "该QQ用户已登录");
                        return;
                    }

                    ShortUserInfo userInfo    = Users.GetShortUserInfo(userconnectinfo.Uid);
                    string        redirectUrl = "";
                    //如果userInfo==null,可能是管理员后台删除了这个帐号,则用户的ConnnectInfo也需要被解绑重置
                    if (userInfo == null)
                    {
                        DiscuzCloud.UnbindUserConnectInfo(userconnectinfo.OpenId);
                        redirectUrl = HttpContext.Current.Request.RawUrl;
                    }
                    else
                    {
                        redirectUrl = forumpath + "index.aspx";
                        //如果云端的token和Secret发生改变,则更新本地保存的token和Secret
                        if (tokenInfo.Token != userconnectinfo.Token || tokenInfo.Secret != userconnectinfo.Secret)
                        {
                            userconnectinfo.Token  = tokenInfo.Token;
                            userconnectinfo.Secret = tokenInfo.Secret;
                            DiscuzCloud.UpdateUserConnectInfo(userconnectinfo);
                        }
                        LoginUser(userInfo);
                    }
                    HttpContext.Current.Response.Redirect(redirectUrl);
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                }
                else
                {
                    string[] callbackInfo = userconnectinfo.CallbackInfo.Split('&');
                    if (callbackInfo.Length == 4)
                    {
                        usedusernames = string.IsNullOrEmpty(usedusernames) ? callbackInfo[0] : usedusernames;
                        birthday      = string.IsNullOrEmpty(birthday) ? callbackInfo[1] : birthday;
                        gender        = gender == 0 ? Utils.StrToInt(callbackInfo[2], 0) : gender;
                        email         = string.IsNullOrEmpty(email) ? callbackInfo[3] : email;
                    }
                }
                UserBindConnectLog userBindLog = DiscuzCloud.GetUserConnectBindLog(userconnectinfo.OpenId);
                isbindoverflow = userBindLog != null && cloudconfig.Maxuserbindcount > 0 && userBindLog.BindCount >= cloudconfig.Maxuserbindcount;

                allowreg      = config.Regstatus != 0 && cloudconfig.Allowconnectregister == 1 && !isbindoverflow;
                connectswitch = allowreg && userid < 0 ? 1 : 2;

                #region convert used username list
                byte[] bt = Convert.FromBase64String(usedusernames);
                usedusernames = System.Text.Encoding.Default.GetString(bt);

                #endregion
                avatarurl = string.Format("http://avatar.connect.discuz.qq.com/{0}/{1}", DiscuzCloudConfigs.GetConfig().Connectappid, userconnectinfo.OpenId);
                openid    = userconnectinfo.OpenId;
                break;

            case "bind":
                if (ispost)
                {
                    if (DNTRequest.GetString("bind_type") == "new")
                    {
                        RegisterAndBind();
                    }
                    else
                    {
                        if (userid < 0)
                        {
                            BindForumExistedUser();
                        }
                        else
                        {
                            BindLoginedUser();
                        }
                    }
                }
                break;

            case "unbind":
                if (userid < 1)
                {
                    AddErrLine("未登录用户无法进行该操作");
                    return;
                }
                userconnectinfo = DiscuzCloud.GetUserConnectInfo(userid);
                if (userconnectinfo == null)
                {
                    AddErrLine("您并没有绑定过QQ,不需要执行该操作");
                    return;
                }
                if (ispost)
                {
                    if (userconnectinfo.IsSetPassword == 0)
                    {
                        string passwd = DNTRequest.GetString("newpasswd");
                        if (string.IsNullOrEmpty(passwd))
                        {
                            AddErrLine("您必须为帐号设置新密码才能解除绑定");
                            return;
                        }
                        if (passwd.Length < 6)
                        {
                            AddErrLine("密码不得少于6个字符");
                            return;
                        }

                        if (passwd != DNTRequest.GetString("confirmpasswd"))
                        {
                            AddErrLine("两次输入的新密码不一致");
                            return;
                        }
                        UserInfo userInfo = Users.GetUserInfo(userid);
                        userInfo.Password = passwd;
                        Users.ResetPassword(userInfo);
                        //同步其他应用密码
                        Sync.UpdatePassword(userInfo.Username, userInfo.Password, "");

                        if (!Utils.StrIsNullOrEmpty(DNTRequest.GetString("changesecques")))
                        {
                            Users.UpdateUserSecques(userid, DNTRequest.GetInt("question", 0), DNTRequest.GetString("answer"));
                        }

                        ForumUtils.WriteCookie("password", ForumUtils.SetCookiePassword(userInfo.Password, config.Passwordkey));
                        OnlineUsers.UpdatePassword(olid, userInfo.Password);
                    }

                    DiscuzCloud.UnbindUserConnectInfo(userconnectinfo.OpenId);
                    UserBindConnectLog bindLog = DiscuzCloud.GetUserConnectBindLog(userconnectinfo.OpenId);
                    if (bindLog != null)
                    {
                        bindLog.Type = 2;
                        DiscuzCloud.UpdateUserConnectBindLog(bindLog);
                    }

                    AddMsgLine("解绑成功");
                    string reurl = Utils.UrlDecode(ForumUtils.GetReUrl());
                    SetUrl(reurl.IndexOf("register.aspx") < 0 ? reurl : forumpath + "index.aspx");
                    SetMetaRefresh();
                }
                break;

            default:
                if (isbindconnect)
                {
                    AddErrLine("用户已登录");
                    return;
                }
                HttpContext.Current.Response.Redirect(DiscuzCloud.GetConnectLoginPageUrl(userid));
                HttpContext.Current.ApplicationInstance.CompleteRequest();
                break;
            }
        }