Reset credentials in WinRtAuthorizer and NullReferenceException with InMemoryCredentials

Mar 15, 2013 at 11:24 PM
Edited Mar 15, 2013 at 11:34 PM
Hi Joe,

first of all thank you for implementing OAuth authorization of public streams. I will update my app as soon as possible to include this update.

When I try to use InMemoryCredentials with a WinRTAuthorizer I get a NullReferenceException in LoadCredentials due to the assumption that credentials either implements IAsyncOAuthCredentials or IWinRtSettingsCredentials and there is no null check on the last one.
See line:
(Credentials as IWinRtSettingsCredentials).LoadCredentialsFromSettings();

What I was actually trying to achieve is being able to clear the stored credential data and relogin a user after that user was successfully logged in before. For that I used a new WinRtAuthorizer with new LocalDataCredentials where AccessToken and OAuthToken are empty again. If the user cancels login he is still being logged in based on the old data stored by the LocalDataCredentials. If I try to clear the LocalDataCredentials this will result in an empty OAuthToken and OAuthSecret being stored and the LocalDataCredentials won't work at all (even though I provide the actual values for OAuthToken and OAuthSecret).
What am I missing?

kind regards,

Roland Smeenk
Mar 16, 2013 at 3:43 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Mar 16, 2013 at 3:52 AM
Hi Roland,

I copied this to a work-item because I believe their should be a better solution too. In the meantime, you can still use LocalDataCredentials with WinRtAuthorizer. The key to clearing this is that the credentials are being stored in a file named "Linq2TwitterCredentials.txt" by default in LocalStorage. The following example shows one way to delete that file, which will clear credentials and allow you to re-authenticate the user.
            string fileName = "Linq2TwitterCredentials.txt";
            var files = await ApplicationData.Current.LocalFolder.GetFilesAsync();
            if (files.Any(storFile => storFile.Name == fileName))
                var file = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
                await file.DeleteAsync();

            var auth = new WinRtAuthorizer
                Credentials = new LocalDataCredentials(fileName)
                    ConsumerKey = "...",
                    ConsumerSecret = "..."
                UseCompression = true,
                Callback = new Uri("")
Notice also that LocalDataCredentials has a constructor overload that lets you specify your own file name. This is a convenience to let you manage file names by whatever conventions you need - just keep track of the file name to ensure you're creating/deleting the right one.

Mar 16, 2013 at 5:21 AM
I just checked in a modification to the source code that makes it easier to clear credentials. Here's a quick example of how it works:
            var credentials = new LocalDataCredentials(fileName);
            await credentials.ClearAsync();
Mar 16, 2013 at 6:42 PM
Hi Joe,

thanks for the changes. This works good enough for me.