There are a couple different ways to set the credentials: via Load or explicitly. I'm going to pseudo code it to save time, but you'll need to ensure you type the code in properly in case I make a typo.
Here's an example that uses the Credentials.Load method. It assumes that you've stored your credential string as a comma-separated list in your database:
var credentials = new InMemoryCredentials();
string credentialString = GetCredentialStringFromDB();
And then you would load the credentials into your authorizer. Here's a WebAuthorizer used in ASP.NET WebForms applications:
auth = new WebAuthorizer
Credentials = credentials,
PerformRedirect = authUrl => Response.Redirect(authUrl)
var ctx = new TwitterContext(auth);
In the code above, GetCredentialsStringFromDB() is a method that you would write to read a previously saved credential string. The credential string is a comma-separated list, consisting of:
So, now you're probably curious about how that string got into your DB in the first place. You get this string after the user's first successful authentication, which happens after auth.CompleteAuthorization() executes successfully, like this:
string successCredentialString = auth.Credentials.ToString();
In the code above, I used the ToString() method of the Credentials class (from the auth.Credentials property) to obtain the credential string that is in the comma-separated list format above. The SaveCredentialsInDBForUser() method is a method that
you would write to associate the credential string with that user. How you obtain the identity of the current user is up to you.
After the first time that the user authenticates, save their credentials. On subsequent LINQ to Twitter operations on behalf of that user, grab their credentials from the DB, initialize Credentials/Authorizer/TwitterContext, and make the query without
the OAuth dance. Twitter never changes a user's credentials.
Here's the second method of initializing credentials:
var credentials = new InMemoryCredentials
ConsumerKey = <ConsumerKey>,
ConsumerSecret = <ConsumerSecret>,
OAuthToken = <OauthToken>,
AccessToken = <AccessToken>
In the example above, I'm explicitly setting the credentials by setting the properties on the Credentials instance. Once you have a Credentials instance, you can use it just like the previous example. The only difference is that the previous
example uses Load and this example sets properties explicitly. Additionally, to initially obtain credentials, you can read the properties from the Authorizer instance after a successful call to CompleteAuthorization. i.e.
var oauthToken = auth.Credentials.OAuthToken;
SaveTokensToDBForUser(consumerKey, consumerSecret, oauthToken, accessToken);
The SaveTokensToDBForUser() is a method that you would write to save information for the user.
Note: Since ConsumerKey and ConsumerSecret belong to your application, all you really need to do is save the associated OAuthToken and AccessToken in the user's DB record.