Use Twitter Login in ASP.NET MVC 4 without membership & co.

image1611_thumb.png

 

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]

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

About the author

Written by Code Inside Team

Learn more about our team.

Comment on this post

Recent Posts

  • Json-Online-Tools: Viewer & Json2Csharp generator

      Wherever APIs are mentioned the JSON format I not far away. Since I’m using two tools regularly I would like to introduce you to them. JSON Viewer If you only see the JSON-Text you are usually not able to see the structure. With the help of JSON Viewer you can have an easy overview: ...

  • Windows Phone SDK & „System“-Icons

      Although the Metro Design focuses a lot on Typography Icons are still quite important. If you install Windows Phone SDK you will receive 36 Icons. You can find them here: C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v8.0\Icons Unfortunately many Icons you might know from the common applications are not integrated. Pedro Lamas extracted 99 additional Icons ...

  • image1830-570x194.png
    How can I figure out if my ADFS 2.0 works?

      I was working with ADFS 2.0 (“Active Directory Federation Services”) for a while when this simple question crossed my mind: How can I figure out if the connection between ADFS and AD “works”? Here is a simple test… What is ADFS? If you need some “position of trusts” beneath the AD-boarders you choose an ...

  • Subdomain vs. Subdirectory

      Better blog.mydomain.com or mydomain.com/blog? Good question! If got asked this question again via Twitter on the weekend so therefore I decide to share my experiences:   Choose a subdomain, if…. - You plan to offer “different services” which are “logical separated” on one domain - You are able to influence the subdomains without much ...

  • image1792-450x194.png
    Guide to Claim-Based Identity with the Access Control Service

    Microsoft published a free PDF about Claim-based Identity, Access Control Service and how to connect it to the remaining Microsoft world (Sharepoint, ADFS, Azure): Download-Link or MSDN Link I found the announcement today on the blog of Vittorio Bertocci. More information’s? If you want to get deeper into the subject you should risk a look ...

Support us