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

Saving TwitterContext to My.Settings

Mar 17, 2013 at 6:45 PM
Edited Mar 17, 2013 at 6:45 PM
Hi, I can authenticate a user and use the TwitterContext to do what I need to do with Twitter, but how can I keep a copy of the TwitterContext for after the application closes and I reopen it.

I save the TwitterContext to a Setting called PersistingContext in My.Settings which is Typed as a Linqtotwitter.TwitterContext and it saves and can be retrieved from settings during execution, but when I reopen the application is does not persist and I am met by a Nullreferenceexception because it is now empty. All of my other application settings do persist however and the setting scope is set to User.

How can I keep a copy of the TwitterContext in order for a user to stay signed in between application runs without the user having to authenticate each time they wish to use the application?

Any advice would be greatly appreciated.
Mar 17, 2013 at 8:49 PM

Rather than saving the TwitterContext, you might want to consider only saving credentials. Each authorizer has a Credentials property of type IOAuthCredentials. LINQ to Twitter includes many IOAuthCredentials derived types to store credentials in different ways. They range from InMemoryCredentials, which is useful for demos, to SessionStateCredentials for Web Apps, or even WinRtCredentials to support Local, Temporary, or Remote data stores for Windows 8 Apps. By implementing IOAuthCredentials, you have full control over how user secrets persist. You can find one of the existing IOAuthCredentials types in the source code to use directly or create your own using the current ones as examples.

Mar 18, 2013 at 12:11 AM
Works absolutely perfectly, many thanks for your help.

For future reference what I did was store the credentials as settings, which included all of the access tokens etc that are gained upon completing the auth procedure. Once the application was run again it would retrieve these credentials and reinsert them into a PinAuthorizer, which was Authorized and then a new TwitterContext is created from the PinAuthorizer and hey presto.