01 November 2012 CI Team

 

I’ve already blogged about the authentication with Twitter. ASP.NET MVC 4 offers the DotNetOpenAuth NuGet package which makes the pure authentication process a lot easier.

ASP.NET MVC 4 – Membership & co

imageIf you are going to create an ordinary MVC 4 WebApp you will find an “AccountController” and several classes in “AccountModels.cs”.

Save Twitter Login in RavenDB, MongoDB or what ever

I have to admit that I was very disappointed with the whole Membership System some years ago and because of that I didn’t plan to invest more time into it. Indeed they offer SimpleMambership & co ut to me it doesn’t seem “simple” and because I prefer to stay in charge of the information’s I sense this Membership System as a kind of paternalism.

Besides I refuse to save the Login-files into an SQL server but in RavenDB or my own formats.

Solution: use DotNetOpenAuth by yourself

With ASP.NET MVC the DotNetOpenAuth NuGet package will be installed automatically and it offers several providers. It’s possible to contact this straight without the WebSecurity or Membership stuff from ASP.NET.

public class TwitterLoginController : Controller
    {
        // Callback after Twitter Login
        public ActionResult Callback()
        {
            DotNetOpenAuth.AspNet.Clients.TwitterClient client = new TwitterClient(ConfigurationManager.AppSettings["twitterConsumerKey"], ConfigurationManager.AppSettings["twitterConsumerSecret"]);

            var result = client.VerifyAuthentication(this.HttpContext);

            return RedirectToAction("Index", "Home");
        }

        // Point Login URL to this Action
        public ActionResult Login()
        {
            DotNetOpenAuth.AspNet.Clients.TwitterClient client = new TwitterClient(ConfigurationManager.AppSettings["twitterConsumerKey"], ConfigurationManager.AppSettings["twitterConsumerSecret"]);

            UrlHelper helper = new UrlHelper(this.ControllerContext.RequestContext);
            var result = helper.Action("Callback", "TwitterLogin", null, "http");

            client.RequestAuthentication(this.HttpContext, new Uri(result));

            return new EmptyResult();
        }
    }

I’ve mentioned how to find the Twitter Keys here. Besides there are several helps on the Twitter Dev Site.

After the successful login you will receive this “result”:

image

Publish on Twitter with the TwitterClient?

Keep in mind: the only task of the TwitterClient in the DotNetOpenAuth project is the authentication. If you want to get the authentication and post Tweets you should take a look on the DotNetOpenAuth ApplicationBlock package on NuGet. Keyword is “TwitterConsumer” and the implementation of own Stores for the Keys.

[Source Code on GitHub]