ASP MVC Twitter 授权

发布于 2024-10-22 07:06:37 字数 222 浏览 1 评论 0原文

我正在尝试创建一个允许用户登录并加入组的应用程序。加入组并成功登录后,用户将看到特定于该组的项目列表。

目前我正在尝试使用 Twitter 作为授权方式,但遇到了问题。我整天都在看 Twitterizer 和 TweetSharp,但没有任何运气,问题似乎是找到一些与我正在使用的架构相关的示例:ASP .NET MVC2,我似乎不知道如何实现这个使用控制器等的授权功能。

谢谢,完全新手。 凯夫。

I am attempting to create an application that will allow a user to log in and join a group. On joining a group and successfully logging in the user will be presented with a list of items specific to that group.

Currently I am trying to use Twitter as a means of authorization and I'm having problems. I have been looking at Twitterizer and TweetSharp all day without having any luck, the problem seems to be finding some examples which relate to the architecture I am using: ASP .NET MVC2, I can't seem to figure out how I would implement this authorization functionality using controllers etc.

Thanks, total newbie at this.
Kev.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不再让梦枯萎 2024-10-29 07:06:37

您需要创建一个自定义 MembershipProvider。这是关于如何执行此操作的一个很好的教程:
http://www.asp.net /general/videos/how-do-i-create-a-custom-membership-provider

本教程中的详细信息比您需要的更多,但基本上您需要做的是创建一个继承自 < 的新类code>System.Web.Security.MembershipProvider 并重写 ValidateUser 方法。在该方法中,您将调用我之前的答案中的 Twitter 登录代码,并在成功时返回 true 或 false。您还需要修改 web.config 的 部分,以告诉 ASP.NET 在进行授权时使用您的自定义成员资格提供程序。

You'll want to create a custom MembershipProvider. Here's a good tutorial on how to do that:
http://www.asp.net/general/videos/how-do-i-create-a-custom-membership-provider

There's more detail than you need in the tutorial, but basically what you need to do is create a new class that inherits from System.Web.Security.MembershipProvider and override the ValidateUser method. In that method, you'll call the twitter login code from my previous answer and return true or false on success. You'll also need to modify the <membership/> section of the web.config to tell ASP.NET to use your custom membership provider when doing authorization.

痴骨ら 2024-10-29 07:06:37

我已经在 ASP.NET MVC 中使用 Twitterizer 实现了这一点。

首先,您有一个 Login 操作,它在控制器上获取用户名和密码,用于授权用户并保存令牌:

[HttpPost]
public ActionResult Login(string username, string password) {
    OAuthTokenResponse tokens;
    var consumerKey = ConfigurationManager.AppSettings["TwitterConsumerKey"];
    var consumerSecretKey = ConfigurationManager.AppSettings["TwitterConsumerSecret"];         
    try {

        tokens = XAuthUtility.GetAccessTokens(consumerKey, consumerSecretKey, username, password);
        Session["AccessToken"] = tokens.Token;
        Session["AccessTokenSecret"] = tokens.TokenSecret;
     }
     catch (ArgumentNullException) {
        ViewData["message"] = "Username or password incorrect";
        return View();
     }
     ViewData["message"] = "You are logged in to twitter as " + tokens.ScreenName;
     return View();
}

然后您有一个 PostMessage 操作,它获取消息内容并将消息发布到 twitter:

[HttpPost]
public ActionResult PostMessage(string message ) {
     // you should check to make sure the user is actually logged in by checking the session vars first

     var tokens = new OAuthTokens
                      {
                          AccessToken = Session["AccessToken"],
                          AccessTokenSecret =  Session["AccessTokenSecret"],
                          ConsumerKey = consumerKey,
                          ConsumerSecret = consumerSecretKey
                      };

     var tweetResponse = TwitterStatus.Update(tokens, data.Message);
     if (tweetResponse.Result == RequestResult.Success) {
        ViewData["message"] = "Message posted!";
     } else {
        ViewData["message"] = "Error posting to twitter.";
     }
     return View();
}

此代码未经测试,并且应该添加更多的错误检查,但它应该可以帮助您开始......

I've implemented this with Twitterizer in ASP.NET MVC.

First, you'd have a Login action that takes a username and password on a controller that authorizes the user and saves the tokens:

[HttpPost]
public ActionResult Login(string username, string password) {
    OAuthTokenResponse tokens;
    var consumerKey = ConfigurationManager.AppSettings["TwitterConsumerKey"];
    var consumerSecretKey = ConfigurationManager.AppSettings["TwitterConsumerSecret"];         
    try {

        tokens = XAuthUtility.GetAccessTokens(consumerKey, consumerSecretKey, username, password);
        Session["AccessToken"] = tokens.Token;
        Session["AccessTokenSecret"] = tokens.TokenSecret;
     }
     catch (ArgumentNullException) {
        ViewData["message"] = "Username or password incorrect";
        return View();
     }
     ViewData["message"] = "You are logged in to twitter as " + tokens.ScreenName;
     return View();
}

Then you have a PostMessage action that takes the message content and posts to twitter:

[HttpPost]
public ActionResult PostMessage(string message ) {
     // you should check to make sure the user is actually logged in by checking the session vars first

     var tokens = new OAuthTokens
                      {
                          AccessToken = Session["AccessToken"],
                          AccessTokenSecret =  Session["AccessTokenSecret"],
                          ConsumerKey = consumerKey,
                          ConsumerSecret = consumerSecretKey
                      };

     var tweetResponse = TwitterStatus.Update(tokens, data.Message);
     if (tweetResponse.Result == RequestResult.Success) {
        ViewData["message"] = "Message posted!";
     } else {
        ViewData["message"] = "Error posting to twitter.";
     }
     return View();
}

This code wasn't tested and more error checking should be added, but it should get you started...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文