コード例 #1
0
        /// <summary>
        /// Handles the Google return.
        /// </summary>
        private void HandleGoogleReturn()
        {
            var googleAuth = new Google();

            if (YafContext.Current.Get <HttpRequestBase>().QueryString.GetFirstOrDefault(paramName: "code") != null)
            {
                var authorizationCode = YafContext.Current.Get <HttpRequestBase>().QueryString.GetFirstOrDefault(paramName: "code");
                var accessTokens      = googleAuth.GetAccessToken(authorizationCode: authorizationCode, request: this.Request);

                if (accessTokens.AccessToken == null)
                {
                    this.Response.Write(
                        s: string.Format(
                            format: "{2} alert('{0}');window.location.href = '{1}'; {3}",
                            YafContext.Current.Get <ILocalization>().GetText(text: "AUTH_NO_ACCESS_TOKEN"),
                            YafBuildLink.GetLink(page: ForumPages.login).Replace(oldValue: "auth.aspx", newValue: "default.aspx"),
                            ScriptBeginTag,
                            ScriptEndTag));

                    return;
                }

                if (YafContext.Current.Get <HttpRequestBase>().QueryString.GetFirstOrDefaultAs <bool>(paramName: "connectCurrent"))
                {
                    string message;

                    try
                    {
                        googleAuth.ConnectUser(request: this.Request, parameters: accessTokens.AccessToken, message: out message);
                    }
                    catch (Exception ex)
                    {
                        YafContext.Current.Get <ILogger>().Error(ex: ex, format: "Error while trying to connect the google user");

                        message = ex.Message;
                    }

                    if (message.IsSet())
                    {
                        this.Response.Write(
                            s: string.Format(
                                format: "{2} alert('{0}');window.location.href = '{1}'; {3}",
                                message,
                                YafBuildLink.GetLink(page: ForumPages.forum).Replace(oldValue: "auth.aspx", newValue: "default.aspx"),
                                ScriptBeginTag,
                                ScriptEndTag));
                    }
                    else
                    {
                        YafBuildLink.Redirect(page: ForumPages.forum);
                    }
                }
                else
                {
                    string message;

                    try
                    {
                        googleAuth.LoginOrCreateUser(request: this.Request, parameters: accessTokens.AccessToken, message: out message);
                    }
                    catch (Exception ex)
                    {
                        YafContext.Current.Get <ILogger>()
                        .Error(ex: ex, format: "Error while trying to login or register the google user");

                        message = ex.Message;
                    }

                    this.Response.Clear();

                    if (message.IsSet())
                    {
                        this.Response.Write(
                            s: string.Format(
                                format: "{2} alert('{0}');window.location.href = '{1}';window.close(); {3}",
                                message,
                                YafBuildLink.GetLink(page: ForumPages.login).Replace(oldValue: "auth.aspx", newValue: "default.aspx"),
                                ScriptBeginTag,
                                ScriptEndTag));
                    }
                    else
                    {
                        this.Response.Write(
                            s: string.Format(
                                format: "{1} window.location.href = '{0}';window.close(); {2}",
                                arg0: YafBuildLink.GetLink(page: ForumPages.forum).Replace(oldValue: "auth.aspx", newValue: "default.aspx"),
                                arg1: ScriptBeginTag,
                                arg2: ScriptEndTag));
                    }
                }
            }
            else if (YafContext.Current.Get <HttpRequestBase>().QueryString.GetFirstOrDefault(paramName: "error") != null)
            {
                // Return to login page if user cancels social login
                this.Response.Redirect(url: YafBuildLink.GetLink(page: ForumPages.login, fullUrl: true));
            }
            else
            {
                // Authorize first
                this.Response.Redirect(url: googleAuth.GetAuthorizeUrl(request: this.Request), endResponse: true);
            }
        }
コード例 #2
0
        /// <summary>
        /// Handles the Google return.
        /// </summary>
        private void HandleGoogleReturn()
        {
            var googleAuth = new Google();

            if (YafContext.Current.Get <HttpRequestBase>().QueryString.GetFirstOrDefault("code") != null)
            {
                var authorizationCode = YafContext.Current.Get <HttpRequestBase>().QueryString.GetFirstOrDefault("code");
                var accessTokens      = googleAuth.GetAccessToken(authorizationCode, this.Request);

                if (accessTokens.AccessToken == null)
                {
                    this.Response.Write(
                        "{2} alert('{0}');window.location.href = '{1}'; {3}".FormatWith(
                            YafContext.Current.Get <ILocalization>().GetText("AUTH_NO_ACCESS_TOKEN"),
                            YafBuildLink.GetLink(ForumPages.login).Replace("auth.aspx", "default.aspx"),
                            SCRIPTBEGINTAG,
                            SCRIPTENDTAG));

                    return;
                }

                if (YafContext.Current.Get <HttpRequestBase>().QueryString.GetFirstOrDefaultAs <bool>("connectCurrent"))
                {
                    string message;

                    try
                    {
                        googleAuth.ConnectUser(this.Request, accessTokens.AccessToken, out message);
                    }
                    catch (Exception ex)
                    {
                        YafContext.Current.Get <ILogger>().Error(ex, "Error while trying to connect the google user");

                        message = ex.Message;
                    }

                    if (message.IsSet())
                    {
                        this.Response.Write(
                            "{2} alert('{0}');window.location.href = '{1}'; {3}".FormatWith(
                                message,
                                YafBuildLink.GetLink(ForumPages.forum).Replace("auth.aspx", "default.aspx"),
                                SCRIPTBEGINTAG,
                                SCRIPTENDTAG));
                    }
                    else
                    {
                        YafBuildLink.Redirect(ForumPages.forum);
                    }
                }
                else
                {
                    string message;

                    try
                    {
                        googleAuth.LoginOrCreateUser(this.Request, accessTokens.AccessToken, out message);
                    }
                    catch (Exception ex)
                    {
                        YafContext.Current.Get <ILogger>()
                        .Error(ex, "Error while trying to login or register the google user");

                        message = ex.Message;
                    }

                    this.Response.Clear();

                    if (message.IsSet())
                    {
                        this.Response.Write(
                            "{2} alert('{0}');window.location.href = '{1}';window.close(); {3}".FormatWith(
                                message,
                                YafBuildLink.GetLink(ForumPages.login).Replace("auth.aspx", "default.aspx"),
                                SCRIPTBEGINTAG,
                                SCRIPTENDTAG));
                    }
                    else
                    {
                        this.Response.Write(
                            "{1} window.location.href = '{0}';window.close(); {2}".FormatWith(
                                YafBuildLink.GetLink(ForumPages.forum).Replace("auth.aspx", "default.aspx"),
                                SCRIPTBEGINTAG,
                                SCRIPTENDTAG));
                    }
                }
            }
            else if (YafContext.Current.Get <HttpRequestBase>().QueryString.GetFirstOrDefault("error") != null)
            {
                // Return to login page if user cancels social login
                this.Response.Redirect(YafBuildLink.GetLink(ForumPages.login, true));
            }
            else
            {
                // Authorize first
                this.Response.Redirect(googleAuth.GetAuthorizeUrl(this.Request), true);
            }
        }