Keep an User's Authentication in C# Application Memory

Jun 13, 2012 at 10:28 AM

Hi Joe,

I created a Twitter Application in C# using LiNQtoTwitter and I've used an PinAuthorizer authentification system. My C# WinForm Application (RadForm exactly) works great and I can connect any twitter account to it, but when I close the application, then I re-open it, and want to connect with the same account, it's asking me a new PinAuthorizer again..

Here is my problem.. I'm looking for a solution that can memorize the Authorization Access to an account which was already logged in to the application one time before. 


Please check the code below :

public partial class TwitterConnection : Telerik.WinControls.UI.RadForm

        IOAuthCredentials Credentials;
        private string pinConfirmation = "";
        private PinAuthorizer auth;

        public TwitterConnection()

        public void connectionAccount()
             var authentification = new PinAuthorizer
                Credentials = new InMemoryCredentials
                    ConsumerKey = "myConsumerKey",
                    ConsumerSecret = "myConsumerSecret"
                UseCompression = true,
                GoToTwitterAuthorization = pageLink => Process.Start(pageLink),
                GetPin = () =>
                    // this executes after user authorizes, which begins with the call to auth.Authorize() below.
                    AskPin maMsgBox = new AskPin();
                    if (maMsgBox.getOk())
                        pinConfirmation = maMsgBox.getPin();
                    //Console.WriteLine("\nAfter you authorize this application, Twitter will give you a 7-digit PIN Number.\n");
                    //Console.Write("Enter the PIN number here: ");
                    return pinConfirmation;
            this.auth = authentification;
            this.DialogResult = DialogResult.OK; 

 Have you any idea to keep the authorization access of an already logged in user ?


Thanks in advance,


Jun 13, 2012 at 1:29 PM

Hi Max,

To avoid re-authorizing, you'll need to keep track of who the user is, save their credentials, and reload their credentials any time you instantiate a TwitterContext. Here's a quick overview of what you can do:

1. If you're using Windows Forms, and you have a login for your app, you can use that.  Alternatively, you can get their Windows login via System.Security.Principal.WindowsIdentity.GetCurrent().Name.

2. Whenever you instantiate TwitterContext, check your database to see if you have credentials for the logged-in user and load them.  This would be the credentials that you load via InMemoryCredentials to the Credentials property of the PinAuthorizer instance.  There are 4 of them: OAuthConsumerKey, OAuthConsumerSecret, OAuthToken, and OAuthTokenSecret.

3. If you don't have credentials, then take the user through the authorization process.

4. After the authorization process, grab all 4 credentials from the Credentials property, of the AuthorizedClient property, in your TwitterContext instance.  i.e. twitterCtx.AuthorizedClient.Credentials.  Credentials have ToString and Load convenience methods to help.  Get all 4 credentials.

5. Save the credentials, associated with the logged in user's account, to your database.  This will allow you to obtain credentials and avoid the authorization process at Step #2 above.


Jun 14, 2012 at 4:52 AM


Thank you for your fast reply ! I'll check your solution then I'll let you know if I got some other problems or if all is ok.


Best Regards,