微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 在asp.net mvc Twitter OAuth2登录后获取oauth凭据

在测试 build-in MVC 5 OAuth2/OpenID providers之后,我能够创建一个网站,允许我使用我的Twitter凭据进行身份验证.

我现在遇到的问题是,我还想在用户成功通过身份验证后,在网址中存储令牌(oauth_token& oauth_verifier)Twitter帖子.我需要这些令牌,所以我可以允许用户直接从我的网站发布详细信息到他们的Twitter帐户.

在Startup.Auth.cs中设置了Twitterauthenticationoptions(见下文)后,我确实发现我所关注的令牌可以在上下文中找到(((context.Response.Context).Request).QueryString)但是解析这似乎是一个难看的解决方案.

var tw = new Twitterauthenticationoptions {
       ConsumerKey = "SecretKey",ConsumerSecret = "SecretSecret",SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie,Provider = new TwitterauthenticationProvider() {
            OnAuthenticated = (context) => {
                context.Identity.AddClaim(new System.Security.Claims.Claim("urn:twitter:access_token",context.Accesstoken,XmlSchemaString,"Twitter"));
                return Task.Fromresult(0);
                        }
            }

  };

  app.UseTwitterauthentication(tw);

如何优雅地实施?对于Facebook我找到了一个实际检索其他信息的解决方案,这感觉类似……

get-more-information-from-social-providers-used-in-the-vs-2013-project-templates

解决方法

您可以使用Query而不是QueryString,然后使用Get方法查询字符串中检索值.
context.Response.Context.Request.Query.Get("oauth_verifier");
context.Response.Context.Request.Query.Get("oauth_token"); or context.Accesstoken

需要注意的其他方法是你不需要oauth_verifer来发布数据. Look here at required headers.我建议你使用libraries listended here之一与Twitter互动.

原文地址:https://www.jb51.cc/csharp/99726.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐