Keep an User's Authentication in C# Application Memory

Jun 13, 2012 at 11: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()
        {
            InitializeComponent();
        }

        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();
                    maMsgBox.ShowDialog();
                    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: ");
                    RadMessageBox.Show(pinConfirmation);
                    return pinConfirmation;
                }
            };
            authentification.Authorize();
            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,

Max.

Coordinator
Jun 13, 2012 at 2: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.

@JoeMayo


Jun 14, 2012 at 5:52 AM

Hi,

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,

Max.