gpt4 book ai didi

asp.net-mvc-4 - 在 MVC4 中使用 DotNetOpenAuth 获取 Twitter 访问 key

转载 作者:行者123 更新时间:2023-12-01 07:28:27 25 4
gpt4 key购买 nike

我正在使用 MVC4 创建一个应用程序,该应用程序将授权用户使用 Twitter 并让他们也从应用程序中发布推文。我可以使用 MVC4 中的 BuiltInOAuthClient.Twitter 毫无问题地对用户进行身份验证。 http://www.asp.net/web-pages/tutorials/security/enabling-login-from-external-sites-in-an-aspnet-web-pages-site

我有访问 token 和 oauth_verifier,但我还需要从 Twitter 取回 acess_secret。 https://dev.twitter.com/docs/auth/implementing-sign-twitter

我缺少的是如何将 oauth_verifier 传递回 Twitter 以使用 OAuthWebSecurity 获取访问 key 。

同样,我可以使用 Twitter 进行登录,但我还需要能够以用户身份使用 Twitter。我以前用 TweetSharp 库做过这个,但我试图在这个项目上使用 DotNetOpenAuth。

更新:
我正在使用第一个链接中描述的 OAuthWebSecurity 类来管理身份验证。 AuthConfig 中的 OAuthWebSecurity.RegisterClient 需要一个 DotNetOpenAuth.AspNet.IAuthenticationClient。您不能按照建议将其与 TwitterConsumer 类交换。

我可以使用第一个链接中描述的“内置”DotNetOpenAuth 身份验证部分,或者我可以使用自定义代码进行完全授权,但我正在尝试找到一种方法来做到这两个。

我可以单独做,但随后用户会看到两次 Twitter 对话框(一次登录,一次授权)。我希望有一种方法可以使用已经连接的身份验证部分,该部分使用 OAuthWebSecurity,但也包含授权部分。

最佳答案

几天来,我一直在用这个头撞墙,但我终于有了一些有用的东西。有兴趣知道它是否是一个有效的解决方案!

首先,创建一个新的 OAuthClient:

public class TwitterClient : OAuthClient
{
/// <summary>
/// The description of Twitter's OAuth protocol URIs for use with their "Sign in with Twitter" feature.
/// </summary>
public static readonly ServiceProviderDescription TwitterServiceDescription = new ServiceProviderDescription
{
RequestTokenEndpoint =
new MessageReceivingEndpoint(
"https://api.twitter.com/oauth/request_token",
HttpDeliveryMethods.GetRequest | HttpDeliveryMethods.AuthorizationHeaderRequest),
UserAuthorizationEndpoint =
new MessageReceivingEndpoint(
"https://api.twitter.com/oauth/authenticate",
HttpDeliveryMethods.GetRequest | HttpDeliveryMethods.AuthorizationHeaderRequest),
AccessTokenEndpoint =
new MessageReceivingEndpoint(
"https://api.twitter.com/oauth/access_token",
HttpDeliveryMethods.GetRequest | HttpDeliveryMethods.AuthorizationHeaderRequest),
TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() },
};

public TwitterClient(string consumerKey, string consumerSecret) :
base("twitter", TwitterServiceDescription, consumerKey, consumerSecret) { }

/// Check if authentication succeeded after user is redirected back from the service provider.
/// The response token returned from service provider authentication result.
protected override AuthenticationResult VerifyAuthenticationCore(AuthorizedTokenResponse 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<string, string>()
{
{"accesstoken", accessToken},
{"accesssecret", accessSecret}
};
return new AuthenticationResult(
isSuccessful: true,
provider: ProviderName,
providerUserId: userId,
userName: userName,
extraData: extraData);
}
}

重要的部分是将响应转换为 ITokenSecretContainingMessage。似乎响应一直都有 TokenSecret,但它只是在内部属性上。通过转换它,您可以访问公共(public)属性(property)。我不能说我喜欢这样做,但是我也不明白为什么 DotNetOpenAuth Asp.Net 团队首先隐藏了该属性。一定有充分的理由。

然后在 AuthConfig 中注册这个客户端:
OAuthWebSecurity.RegisterClient( new TwitterClient(
consumerKey: "",
consumerSecret: ""), "Twitter", null);

现在,在 AccountController 的 ExternalLoginCallback 方法中,extraData 字典中提供了 accessSecret。

关于asp.net-mvc-4 - 在 MVC4 中使用 DotNetOpenAuth 获取 Twitter 访问 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12198734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com