This project has moved and is read-only. For the latest updates, please go here.

running the authorization synchronously in WinRt

Jan 25, 2013 at 2:36 AM
Edited Jan 25, 2013 at 2:38 AM

I am basically trying to run my authorization process for my WinRt app synchronously, but everything I've tried appears to deadlock the app after authorization on the MainPage, preventing navigation to the HomePage.  Can't seem to figure it out.  Here is my code without modifications, which works perfectly fine asynchronously below.  How can I make this work synchronously:




public async void InitializeTwitterAsnc()

            WinRtAuthorizer authorizer = null;

            authorizer = new WinRtAuthorizer()
                Credentials = new LocalDataCredentials()
                    ConsumerKey = "blah379",
                    ConsumerSecret = "blah123"
                UseCompression = true,
                Callback = new Uri("")

                await authorizer.AuthorizeAsync();

            // set the twitter credential fields
            await Task.Run
                (() =>
                         twitterName = authorizer.Credentials.ScreenName;
                         twitterId = authorizer.Credentials.UserId;
                         accessToken = authorizer.Credentials.AccessToken;
                         oAuthToken = authorizer.Credentials.OAuthToken;

                         this.TwitterContext = new TwitterContext(authorizer);


code that calls the method from MainPage.xaml.cs:


private void StartTwitterLogin(object sender, RoutedEventArgs e)
            // start the twitter authorization
            TwitterAuth twitAuth = new TwitterAuth();

            this.Frame.Navigate(typeof (HomePage));
Jan 25, 2013 at 5:52 AM


The WinRtAuthorizer is based on WebAuthenticationBroker, which is async. So, it isn't possible to perform a synchronous authentication with it.

The only synchronous option available is PinAuthorizer with InMemoryCredentials. Of course, you could write your own IOAuthCredentials type to replace InMemoryCredentials. Don't use any of the WinRtCredentials types with PinAuthorizer because they're async, which will cause a race condition that will break the authorization process.

I'm curious what is your use case for synchronous authorization in Windows 8?


Jan 25, 2013 at 6:27 AM

Well, I simply just don't want to navigate to the HomePage until the user has finished authorizing.  As it is now, the app navigates to the HomePage in the background while the user is still in the process of authorizing.  Initially I was thinking I could just do some sort of Wait() method, but that may or may not be likely.  Any ideas?



Jan 25, 2013 at 8:48 PM


I see what's happening because as soon as you await in InitializeTwitterAsync(), control returns to the caller and you navigate before everything is done. Instead, do the navigation after the await on authorizer.AuthorizeAsync() inside of the InitializeTwitterLAsync() method.


Jan 26, 2013 at 12:27 AM


Yeah that solution was in the back of my mind, but being that I am new to working with async, I wasn't sure if there was a better way of resolving this issue with async or not.  Thanks for your time and and for a great product in linqtotwitter.