diff options
Diffstat (limited to 'samples/DotNetOpenAuth.ApplicationBlock/TwitterConsumer.cs')
-rw-r--r-- | samples/DotNetOpenAuth.ApplicationBlock/TwitterConsumer.cs | 73 |
1 files changed, 43 insertions, 30 deletions
diff --git a/samples/DotNetOpenAuth.ApplicationBlock/TwitterConsumer.cs b/samples/DotNetOpenAuth.ApplicationBlock/TwitterConsumer.cs index 120061b..0ebb1db 100644 --- a/samples/DotNetOpenAuth.ApplicationBlock/TwitterConsumer.cs +++ b/samples/DotNetOpenAuth.ApplicationBlock/TwitterConsumer.cs @@ -60,29 +60,38 @@ namespace DotNetOpenAuth.ApplicationBlock { private static readonly MessageReceivingEndpoint VerifyCredentialsEndpoint = new MessageReceivingEndpoint("http://api.twitter.com/1/account/verify_credentials.xml", HttpDeliveryMethods.GetRequest | HttpDeliveryMethods.AuthorizationHeaderRequest); - private static InMemoryTokenManager ShortTermUserSessionTokenManager { - get { - var store = HttpContext.Current.Session; - var tokenManager = (InMemoryTokenManager)store["TwitterShortTermUserSessionTokenManager"]; - if (tokenManager == null) { - string consumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"]; - string consumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"]; - if (IsTwitterConsumerConfigured) { - tokenManager = new InMemoryTokenManager(consumerKey, consumerSecret); - store["TwitterShortTermUserSessionTokenManager"] = tokenManager; - } else { - throw new InvalidOperationException("No Twitter OAuth consumer key and secret could be found in web.config AppSettings."); - } - } - - return tokenManager; - } - } - + /// <summary> + /// The consumer used for the Sign in to Twitter feature. + /// </summary> private static WebConsumer signInConsumer; + /// <summary> + /// The lock acquired to initialize the <see cref="signInConsumer"/> field. + /// </summary> private static object signInConsumerInitLock = new object(); + /// <summary> + /// Initializes static members of the <see cref="TwitterConsumer"/> class. + /// </summary> + static TwitterConsumer() { + // Twitter can't handle the Expect 100 Continue HTTP header. + ServicePointManager.FindServicePoint(GetFavoritesEndpoint.Location).Expect100Continue = false; + } + + /// <summary> + /// Gets a value indicating whether the Twitter consumer key and secret are set in the web.config file. + /// </summary> + public static bool IsTwitterConsumerConfigured { + get { + return !string.IsNullOrEmpty(ConfigurationManager.AppSettings["twitterConsumerKey"]) && + !string.IsNullOrEmpty(ConfigurationManager.AppSettings["twitterConsumerSecret"]); + } + } + + /// <summary> + /// Gets the consumer to use for the Sign in to Twitter feature. + /// </summary> + /// <value>The twitter sign in.</value> private static WebConsumer TwitterSignIn { get { if (signInConsumer == null) { @@ -97,18 +106,22 @@ namespace DotNetOpenAuth.ApplicationBlock { } } - /// <summary> - /// Initializes static members of the <see cref="TwitterConsumer"/> class. - /// </summary> - static TwitterConsumer() { - // Twitter can't handle the Expect 100 Continue HTTP header. - ServicePointManager.FindServicePoint(GetFavoritesEndpoint.Location).Expect100Continue = false; - } - - public static bool IsTwitterConsumerConfigured { + private static InMemoryTokenManager ShortTermUserSessionTokenManager { get { - return !string.IsNullOrEmpty(ConfigurationManager.AppSettings["twitterConsumerKey"]) && - !string.IsNullOrEmpty(ConfigurationManager.AppSettings["twitterConsumerSecret"]); + var store = HttpContext.Current.Session; + var tokenManager = (InMemoryTokenManager)store["TwitterShortTermUserSessionTokenManager"]; + if (tokenManager == null) { + string consumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"]; + string consumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"]; + if (IsTwitterConsumerConfigured) { + tokenManager = new InMemoryTokenManager(consumerKey, consumerSecret); + store["TwitterShortTermUserSessionTokenManager"] = tokenManager; + } else { + throw new InvalidOperationException("No Twitter OAuth consumer key and secret could be found in web.config AppSettings."); + } + } + + return tokenManager; } } |