asp.net-mvc-4 – 在MVC4中使用DotNetOpenAuth获取Twitter访问密
我正在使用MVC4创建一个应用程序,该应用程序将使用Twitter授权用户,并允许他们从应用程序发送推文.我可以使用MVC4中的BuiltInOAuthClient.Twitter在没有问题的情况下对用户进行身份验证.
http://www.asp.net/web-pages/tutorials/security/enabling-login-from-external-sites-in-an-aspnet-web-pages-site
我有访问令牌和oauth_verifier,但我也需要从Twitter获取acess_secret. https://dev.twitter.com/docs/auth/implementing-sign-twitter 我缺少的是如何将oauth_verifier传递回Twitter以使用OAuthWebSecurity获取访问密码. 再次,我可以使用Twitter登录确定,但我需要能够使用Twitter作为用户.我之前使用TweetSharp库完成了这项工作,但我正在尝试在此项目中使用DotNetOpenAuth. 更新: 我可以使用第一个链接中描述的“内置”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",AccessTokenEndpoint = new MessageReceivingEndpoint( "https://api.twitter.com/oauth/access_token",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,但它仅在内部属性上.通过投射,您可以访问公共财产.我不能说我喜欢这样做,但后来我也不明白为什么DotNetOpenAuth Asp.Net团队首先隐藏了这个属性.一定有充分的理由. 然后在AuthConfig中注册此客户端: OAuthWebSecurity.RegisterClient( new TwitterClient( consumerKey: "",consumerSecret: ""),"Twitter",null); 现在,在AccountController上的ExternalLoginCallback方法中,accessSecret在ExtraData字典中可用. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 在Asp.Net中群发邮件HTML-newsletter
- asp.net-core – 如何在使用asp.net 5时更改登录URL
- asp.net-mvc – RedirectToAction重定向查询参数?
- ASP.NET mvc:如何使用今天的日期自动填充日期字段?
- asp.net – 如何在MVC 4中查询当前登录用户的AD信息
- asp.net-mvc – 带有asp.net mvc 4和EntityFramework的View
- 免费构建.NET服务器
- asp.net-mvc – RedirectToAction()与View()和三元运算符?
- asp.net-mvc – ASP.NET MVC&Windsor.Castle:使用与Ht
- msbuild – 如果不指定目标框架,则不支持“发布”目标
- asp.net – 什么原因“内部连接致命错误”
- asp.net-mvc – 不需要必需属性的bool属性
- asp.net – BC30560:’ExtensionAttribute’在名
- ASP.NET C#Active Directory – 查看用户密码到期
- asp.net-mvc – 如何在ASP.NET MVC4 Api控制器中
- asp.net-mvc – 我什么时候应该在asp.net mvc应用
- asp.net – 我应该在我的HttpHandler中设置IsReu
- C#输入的字符串只包含汉字
- 在asp.net中使用cookie mvc c#
- asp.net-mvc-3 – Structuremap,AutoFac或Ninjec