NullReferenceException on BeginAuthorizationAsync

Apr 16, 2014 at 9:40 AM
Hi, When I try to begin authorization, a NullReferenceException occurs on the method BeginAuthorizationAsync. I followed the example MVC code. I tried both with SessionStateCredentialStore and InMemoryCredentialStore but the result is the same.
The solution is a MVC 4 solution upgraded to MVC 5
public async Task<ActionResult> BeginAsync()
        {
            //var auth = new MvcSignInAuthorizer
            //var auth = new MvcAuthorizer
            //{
                 
            //    CredentialStore = new SessionStateCredentialStore
            //    {
            //        ConsumerKey = "<my key>",
            //        ConsumerSecret = "<my secret>"
            //    }
            //};

            var auth = new MvcSignInAuthorizer
            {
                CredentialStore = new InMemoryCredentialStore()
                {
                    ConsumerKey = "<my key>",
                    ConsumerSecret = "<my secret>"
                }
            };

            string twitterCallbackUrl = Request.Url.ToString().Replace("Begin", "Complete");
            return await auth.BeginAuthorizationAsync(new Uri(twitterCallbackUrl));
            
      
        }

public async Task<ActionResult> CompleteAsync(string oauth_token, string oauth_verifier)
        {
            //var auth = new MvcAuthorizer
            //{
            //    CredentialStore = new SessionStateCredentialStore()
            //};

            var auth = new MvcSignInAuthorizer
            {
                CredentialStore = new InMemoryCredentialStore()
                {
                    ConsumerKey = "<my key>",
                    ConsumerSecret = "<my secret>",
                    OAuthToken = oauth_token
                }
            };

            await auth.CompleteAuthorizeAsync(Request.Url);

            // This is how you access credentials after authorization.
            // The oauthToken and oauthTokenSecret do not expire.
            // You can use the userID to associate the credentials with the user.
            // You can save credentials any way you want - database, 
            //   isolated storage, etc. - it's up to you.
            // You can retrieve and load all 4 credentials on subsequent 
            //   queries to avoid the need to re-authorize.
            // When you've loaded all 4 credentials, LINQ to Twitter will let 
            //   you make queries without re-authorizing.
            //
            var credentials = auth.CredentialStore;
            string oauthToken = credentials.OAuthToken;
            string oauthTokenSecret = credentials.OAuthTokenSecret;
            string screenName = credentials.ScreenName;
            ulong userID = credentials.UserID;

            return RedirectToAction("Manage", "Account");
        }
Any idea? Thanks in advance
Coordinator
Apr 16, 2014 at 4:26 PM
Hi,

It looks like you should use MvcAuthorizer instead of MvcSignInAuthorizer. Here's some documentation that might help:

https://linqtotwitter.codeplex.com/wikipage?title=Implementing%20OAuth%20for%20ASP.NET%20MVC&referringTitle=Learning%20to%20use%20OAuth

There's also a sample in the downloadable source code in the Linq2TwitterDemos_MVC project.

@JoeMayo
Apr 16, 2014 at 5:42 PM
I solved after performing the following 2 actions:

1) I upgraded Microsoft Http Client libraries to 2.2.20
2) I added <sessionState mode="InProc" /> under <system.web> in the web.config

Hope this will help others :)
Thanks
Coordinator
Apr 16, 2014 at 6:21 PM
InProc will cause problems because IIS recycles occasionally and you'll loose Session state. State Server and SQL are more reliable options.

@JoeMayo