Problem with SessionStateCredentials

Aug 26, 2013 at 3:31 PM
Hi,

I am implementing LinQToTwitter in webforms and using C#.

I am using SessionStateCredentials, to that I can use same credential across the pages. It is authorizing perfectly through the WebAuthorizer.

My problem is in the second page? How can I reuse the authentication in another page?

My code is here in the second page:
 auth1 = new WebAuthorizer();
 credentials = new SessionStateCredentials();
 var twitterCtx = new TwitterContext(auth1);

var followers = (from follower in twitterCtx.SocialGraph where follower.Type == SocialGraphType.Followers && follower.ScreenName == credentials.ScreenName select follower).SingleOrDefault();
it is throwing error:
System.ArgumentNullException: consumerKey
Parameter name: You must provide a consumerKey.

Con anybody help me if i am doing anything wrong?

Thanks
Amit
Coordinator
Aug 26, 2013 at 3:33 PM
Hi Amit,

It sounds like you're losing session state. Check to make sure you have it set to StateServer or SQL Server.

@JoeMayo
Aug 26, 2013 at 4:07 PM
Hi Joe,

Thanks for your quick response.

This time I am unable to to go to the second page. Also I have changed the session mode to Sql Server. Now it is throwing the auth1 object of WebAuthorizer as null on CompleteAuthorization.

The function to authenticate:
private bool AuthenticateTweet()
    {
        try
        {
                credentials = new SessionStateCredentials();

                if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
                {
                    credentials.ConsumerKey = Preferences.ConsumerKey;
                    credentials.ConsumerSecret = Preferences.ConsumerKeySecret;
                }

                HttpContext.Current.Session["oauth_token"] = Preferences.ConsumerKey;
                HttpContext.Current.Session["oauth_verifier"] = Preferences.ConsumerKey;

                try
                {
                    auth1 = new WebAuthorizer
                    {
                        Credentials = credentials,
                        PerformRedirect = authUrl => Response.Redirect(authUrl, false)
                    };

                    if (!auth1.IsAuthorized)
                    {
                        Uri specialUri = new Uri(Request.Url.ToString());

                        auth1.BeginAuthorization(specialUri);
                        //Label1.Text = "You are not authorized from twitter.";
                    }
                }
                catch (Exception e) { }
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
The function call & CompleteAuthentication on page load:
         if (!Page.IsPostBack)
         {
                 if (String.IsNullOrEmpty(Request["oauth_token"]))
                    {
                        AuthenticateTweet();
                    }
         }


                    if (!Page.IsPostBack)
                    {
                        if (auth1.CompleteAuthorization(Request.Url))
                        {
                            captureUserInfo();
                        }
                    }
The auth1 is null in the line: auth1.CompleteAuthorization(Request.Url).
But earlier it was working perfectly.
Aug 26, 2013 at 4:37 PM
Can anyone please help me out on this?

If anybody implemented this in C#, can you please provide me a sample code, how you have handled the authentication and how you have used that on another page?

Thanks