public AuthorizeState Authorize(string returnUrl, bool?verifyResponse = null)
        {
            //Sleep for 15 seconds as a workaround for a twitter bug. :(
            //resolve this issue because it's blocking the entire site
            //Thread.Sleep(new TimeSpan(0, 0, 0, 15));

            MvcAuthorizer.CompleteAuthorization(GenerateCallbackUri());

            if (!MvcAuthorizer.IsAuthorized)
            {
                return(new AuthorizeState(returnUrl, OpenAuthenticationStatus.RequiresRedirect)
                {
                    Result = MvcAuthorizer.BeginAuthorization()
                });
            }

            var parameters = new OAuthAuthenticationParameters(Provider.SystemName)
            {
                ExternalIdentifier        = MvcAuthorizer.OAuthTwitter.OAuthToken,
                ExternalDisplayIdentifier = MvcAuthorizer.ScreenName,
                OAuthToken       = MvcAuthorizer.OAuthTwitter.OAuthToken,
                OAuthAccessToken = MvcAuthorizer.OAuthTwitter.OAuthTokenSecret,
            };

            var result = _authorizer.Authorize(parameters);

            var tempReturnUrl = _httpContext.Request.QueryString["?ReturnUrl"];

            if (!string.IsNullOrEmpty(tempReturnUrl) && string.IsNullOrEmpty(returnUrl))
            {
                returnUrl = tempReturnUrl;
            }

            return(new AuthorizeState(returnUrl, result));
        }
        public AuthorizeState Authorize(string returnUrl, bool?verifyResponse = null)
        {
            MvcAuthorizer.CompleteAuthorization(GenerateCallbackUri());

            if (!MvcAuthorizer.IsAuthorized)
            {
                return(new AuthorizeState(returnUrl, OpenAuthenticationStatus.RequiresRedirect)
                {
                    Result = MvcAuthorizer.BeginAuthorization()
                });
            }

            var parameters = new OAuthAuthenticationParameters(Provider.SystemName)
            {
                ExternalIdentifier        = MvcAuthorizer.OAuthTwitter.OAuthToken,
                ExternalDisplayIdentifier = MvcAuthorizer.ScreenName,
                OAuthToken       = MvcAuthorizer.OAuthTwitter.OAuthToken,
                OAuthAccessToken = MvcAuthorizer.OAuthTwitter.OAuthTokenSecret,
            };

            var result = _authorizer.Authorize(parameters);

            var tempReturnUrl = _httpContext.Request.QueryString["?ReturnUrl"];

            if (!string.IsNullOrEmpty(tempReturnUrl) && string.IsNullOrEmpty(returnUrl))
            {
                returnUrl = tempReturnUrl;
            }

            return(new AuthorizeState(returnUrl, result));
        }
        public ITwitterAuthorizer GetAuthorizer(Customer customer)
        {
            var parameters = new OAuthAuthenticationParameters(Provider.SystemName);
            var identifier = _openAuthenticationService
                             .GetExternalIdentifiersFor(customer)
                             .Where(o => o.ProviderSystemName == parameters.ProviderSystemName)
                             .ToList()
                             .FirstOrDefault();

            if (identifier != null)
            {
                MvcAuthorizer.Credentials.OAuthToken  = identifier.OAuthToken;
                MvcAuthorizer.Credentials.AccessToken = identifier.OAuthAccessToken;

                return(MvcAuthorizer);
            }
            return(null);
        }
        public AuthorizeState Authorize(string returnUrl, bool?verifyResponse)
        {
            AuthorizeState state = null;
            string         error = null;

            try
            {
                if (verifyResponse.Value)
                {
                    string email    = null;
                    string name     = null;
                    var    token    = _httpContext.Request.QueryString?.GetValues("oauth_token")?.FirstOrDefault();
                    var    verifier = _httpContext.Request.QueryString?.GetValues("oauth_verifier")?.FirstOrDefault();

                    var responseData = GetAccessToken(token, verifier);

                    var parameters = new OAuthAuthenticationParameters(TwitterExternalAuthMethod.SystemName)
                    {
                        ExternalIdentifier        = responseData["oauth_token"],
                        ExternalDisplayIdentifier = responseData["screen_name"],
                        OAuthToken       = responseData["oauth_token"],
                        OAuthAccessToken = responseData["oauth_token_secret"]
                    };

                    // Get email address and full name.
                    try
                    {
                        var str = GetAccount(parameters.OAuthToken, parameters.OAuthAccessToken);
                        if (str.HasValue())
                        {
                            var json = JObject.Parse(str);
                            email = json.GetValue("email").ToString();
                            name  = json.GetValue("name").ToString();
                        }
                    }
                    catch (WebException wex)
                    {
                        Logger.Error(GetErrorMessage(wex));
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex.Message);
                    }

                    var splittedName = name.SplitSafe(" ");
                    var claims       = new UserClaims();
                    claims.Name          = new NameClaims();
                    claims.Contact       = new ContactClaims();
                    claims.Contact.Email = email;
                    claims.Name.FullName = name;

                    if (splittedName.Length >= 2)
                    {
                        claims.Name.First = splittedName[0];
                        claims.Name.Last  = splittedName[1];
                    }
                    else if (splittedName.Length >= 1)
                    {
                        claims.Name.Last = splittedName[0];
                    }

                    //$"{claims.Contact.Email.NaIfEmpty()} {claims.Name.FullName.NaIfEmpty()}: {claims.Name.First.NaIfEmpty()} {claims.Name.Last.NaIfEmpty()}".Dump();

                    parameters.AddClaim(claims);

                    var result = _authorizer.Authorize(parameters);

                    state = new AuthorizeState(returnUrl, result);
                }
                else
                {
                    var callbackUrl     = _services.WebHelper.GetStoreLocation() + "Plugins/SmartStore.TwitterAuth/LoginCallback/";
                    var responseData    = GetRequestToken(callbackUrl);
                    var authenticateUrl = string.Concat(TWITTER_AUTHENTICATE_URL, "?oauth_token=", HttpUtility.UrlEncode(responseData["oauth_token"]));

                    state = new AuthorizeState(string.Empty, OpenAuthenticationStatus.RequiresRedirect)
                    {
                        Result = new RedirectResult(authenticateUrl)
                    };
                }
            }
            catch (WebException wex)
            {
                error = GetErrorMessage(wex);
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }

            if (state == null)
            {
                error = error.NullEmpty() ?? _services.Localization.GetResource("Admin.Common.UnknownError");
                state = new AuthorizeState(string.Empty, OpenAuthenticationStatus.Error);
                state.AddError(error);
                Logger.Error(error);
            }

            return(state);
        }