Getting Started With Twitter Authentication in MVC 5

Introduction

This article explains Twitter Authentication for logging into your ASP.NET Web Application developed in MVC Project Templates. I will use Visual Studio 2013 Preview and MVC 5 in my ASP.NET Web Application. As I had described in my earlier article there are various login options with which you can login your MVC Application. Here, I am describing for you how to authenticate your MVC Application with Twitter.

In that context, you will see in the following image that there are three login options available in my application with which you can login:

TwitterLogin-in-MVC.jpg

As you can see, the following are the login options available in my MVC Application and I am using Twitter Authentication in my application:

  • Facebook
  • Google
  • Twitter

Twitter Authentication for MVC Application

So, let's start to authenticate the MVC Application with Twitter using the following sections:

  • Twitter App Creation
  • MVC App Creation

Twitter App Creation

Register yourself as a App Developer in Twitter.

DevTwitter.jpg

Let's start to create an Twitter app with the following procedure.

Step 1: At first, create an app in Twitter as a developer by https://dev.twitter.com/apps

SignIn.jpg

Step 2: After login, create an app by clicking on "Create a New Application".

CreateNewApp-in-Twitter.jpg

Step 3: Enter your unique App name and other necessary details.

Note: You need to enter the CallBack URL and choose Read and Write options to authorize . If you do not have any domain then you can enter an URL like http://www.website.com/ 

Step 4: After accepting the license agreement, click on "Create your Twitter Application".

CreateNewApp2-in-Twitter.jpg

Step 5: Copy your Consumer Key and Consumer Secret values from the next window.

Details.jpg

That's it. Now, move to the next session.

MVC App Creation

Let's start to create an MVC app with the following procedure.

Step 1: Create an ASP.NET MVC Application.

MvcTemplate.jpg

Step 2: In your Solution Explorer, open the "Startup.Auth.cs" file .

Authentication.jpg

Step 3: As you can see in the following figure, my Twitter authentication is disabled:

Key.jpg

Please un-comment it and paste your Consumer Key value and Consumer Secret value in the file as I mentioned below:

Secret.jpg

Save the changes.

Step 4: Debug your application and move to the Login link.

Login-in-mvc.jpg

Step 5: Click on the Twitter Login option.

TwitterLogin-in-MVC.jpg

Step 6: After clicking the "Twitter" link, it'll automatically move to authenticate your app from Twitter.

AuthorizxeApp.jpg

Step 7: Click on "Authorize App" and it'll redirect you back to your application.

Redirecting.jpg

Step 8: Register yourself to associate with a Twitter account.

Register.jpg

Step 9: Now, you will see your User Name in your MVC Application.

HelloNimit.jpg

Step 10: Log yourself out.

LogOff.jpg

Summary

So far this article will help you to login your MVC Application by Twitter. In this article I have provided all necessary information related to Twitter. So just apply it in your MVC Application.

Thanks for reading my article.


Similar Articles