1
votes

This Twitter Login page (using LinqToTwitter) works as expected until the user has Logged in, then logged out, and then logs in again in the same session.

At that point the statement (bottom of code snippet)

_auth.BeginAuthorization(returnURI);

appears to execute but the Redirect does not take place.

Anybody have this experience or have a suggestion, I'm stumped.

TIA for any ideas!

Code:

 protected void Page_Load(object sender, EventArgs e)
    {

        _credentials = new SessionStateCredentials();
        if (_credentials.ConsumerKey == null || _credentials.ConsumerSecret == null)
        {
            _credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
            _credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
        }
        _auth = new WebAuthorizer
        {
            Credentials = _credentials,
            PerformRedirect = authUrl => Response.Redirect(authUrl)
        };

        if (!IsPostBack)
        {

            if (Request.QueryString["act"] != null)
            {
                string act = Request.QueryString["act"].ToString();
                switch (act)
                {
                    case "login":
                        _auth.CompleteAuthorization(Request.Url);

                        // we do record tokens in SQL here but code removed
                        // for this exercise

                        //log us in for FormsAuthentication
                              FormsAuthentication.SetAuthCookie(_auth.Credentials.ScreenName, false);

                        string ret = Request.QueryString["ret"];
                        Response.Redirect(HttpUtility.UrlDecode(ret));
                        break;
                    case "logout":
                        FormsAuthentication.SignOut();
                        _credentials = null;
                        _auth = null;
                        Response.Redirect("~/");
                        break;
                }
            }
            else
            {
                if (_auth != null)
                {
                    _return = Request.QueryString["ret"];
                    string OAuthReturnUrl = String.Format("http://mydomain.com/login?act=login&ret={0}&ts={1}", HttpUtility.UrlEncode(_return), DateTime.Now);
                    Uri returnURI = new Uri(OAuthReturnUrl);

                    /******************************************
                     on second login in same browser session this executes
                     and return URI is correct but no 
                     redirect takes place. ???
                     ******************************************/
                    _auth.BeginAuthorization(returnURI);
                }
            }
        }
    }
1

1 Answers

2
votes

My bad... of course, SessionCredentials use Session State so:

 if (_auth != null)
                {
                    _return = Request.QueryString["ret"];
                    string OAuthReturnUrl = String.Format("http://myDomain.com/login?act=login&ret={0}&ts={1}", HttpUtility.UrlEncode(_return), DateTime.Now);
                    Uri returnURI = new Uri(OAuthReturnUrl);
                    _credentials.AccessToken = null;
                    _credentials.OAuthToken = null;
                    _credentials.ScreenName = "";
                    _auth.Credentials = _credentials;
                    _auth.BeginAuthorization(returnURI);
                }

resets the previously obtained tokens and allows the redirect to take place.