开发者

ASP MVC Twitter Authorization

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 pre开发者_如何学Pythonsented 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.


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.


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜