Getting Twitter Access Secret using DotNetOpenAuth in MVC4

后端 未结 4 1879
一生所求
一生所求 2021-01-06 16:22

I\'m creating an app with MVC4 that will authorize users using Twitter and lets them tweet from the app as well. I\'m able to get the user authenticated without a problem us

4条回答
  •  死守一世寂寞
    2021-01-06 16:52

    For a WebForms project template which references Microsoft.AspNet.Membership.OpenAuth in AuthConfig.cs instead of Microsoft.Web.WebPages.OAuth (MVC4 Internet Application) I was able to modify Paul Manzotti's answer to get it to work:

    1. Create a custom twitter client class that derives from DotNetOpenAuth.AspNet.Clients.TwitterClient

      public class CustomTwitterClient : TwitterClient { public CustomTwitterClient(string consumerKey, string consumerSecret) : base(consumerKey, consumerSecret) { }

      protected override AuthenticationResult VerifyAuthenticationCore(AuthorizedTokenResponse response)
      {
          //return base.VerifyAuthenticationCore(response);
          string accessToken = response.AccessToken;
          string accessSecret = (response as ITokenSecretContainingMessage).TokenSecret;
          string userId = response.ExtraData["user_id"];
          string userName = response.ExtraData["screen_name"];
      
          var extraData = new Dictionary()
                          {
                              {"accesstoken", accessToken},
                              {"accesssecret", accessSecret}
                          };
          return new AuthenticationResult(
              isSuccessful: true,
              provider: ProviderName,
              providerUserId: userId,
              userName: userName,
              extraData: extraData);
      }
      

      }

    2. Add the custom client in AuthConfig.cs

      public static void RegisterOpenAuth()
      {
          OpenAuth.AuthenticationClients.Add("Twitter", () => new CustomTwitterClient(
              consumerKey: ConfigurationManager.AppSettings["twitterConsumerKey"], 
              consumerSecret: ConfigurationManager.AppSettings["twitterConsumerSecret"]));
      }
      

    Ta-dow! Now you can haz access secret.

提交回复
热议问题