Combine ASP.NET Identity Web API and MVC Best in a Single Web App

By: Jeshal
Updated: December 6, 2014 | Technology:API Integration Services, Asp.Net, Asp.Net MVC
Combine ASP.NET Identity Web API and MVC Best in a Single Web App

ASP.NET Identity:

As the membership story in ASP.NET has evolved over the years, the ASP.NET team has learned a lot from feedback from customers.

The assumption that users will log in by entering a username and password that they have registered in your own application is no longer valid.

The web has become more social. Users are interacting with each other in real time through social channels such as Facebook, Twitter, and other social websites. ASP.NET MVC developers want users to be able to log in with their social identities so that they can have rich experience on their websites.

A modern membership system must enable redirection-based log-ins to authentication providers such as Facebook, Twitter, and others.

As ASP.NET web development evolved, so did the patterns of web development. Unit testing of application code became a core concern for application developers. In 2008 ASP.NET added a new framework based on the Model-View-Controller (MVC) pattern, in part to help .NET developers build unit testable ASP.NET applications. ASP.Net Developers who wanted to unit test their application logic also wanted to be able to do that with the membership system.

Contact Us

Need Help in .NET MVC Development?

Get in touch with our team for a free consultation.
Contact Us

Considering these changes in ASP.NET web application development, ASP.NET Identity was developed with the following goals:

  • One ASP.NET Identity system
  • Ease of plugging in profile data about the user
  • Persistence control
  • Unit test-ability
  • Role provider
  • Claims Based
  • Social Login Providers
  • Windows Azure Active Directory
  • OWIN Integration

Click here for more information about ASP.NET Identity

Now I will show you how to combine Asp.net “Identity” with Web API and ASP Net MVC web application.

You need following simple step for how to use “Identity“.

  1. Create a new Project select Asp.net web application.
  2. Select Web API and change Authentication with (select Individual User Accounts).
  3. Now, your web API project is ready. Let’s run your web API project and click on web API menu you will see how web API access.
  4. Now in my case I want a login and register with an MVC web form. I just add new empty Controller with name “UserManageController”.
  5. Now add the namespace in “UserManageController”.
Copy to Clipboard
  1. Add two token property like.
Copy to Clipboard
  1. Create a constructor for User Mange Controller like.
Copy to Clipboard
  1. Than add new Method “Register” on “UserManageController”
Copy to Clipboard
  1. Right click on the “Register” method and add a new view with “RegisterBindingModel” Modal.
  2. Create a post method for register on UserManageController.

  1. Goto ~/View/Shared/_Layout.cshtml and two links for login and Register.
  2. Run your asp.net web application and click on the register link.
  3. Create Login Method on “UserManageController”.
Copy to Clipboard
  1. Add New login modal on “LoginViewModel” class on =>AccountViewModels on “Modals/AccountViewModels .cs file.
Copy to Clipboard
  1. Add Login View and select is modal “LoginViewModel”.
  2. Than add post method for login like.
Copy to Clipboard
  1. Add redirect method manage method like.
Copy to Clipboard
  1. Now add below code to your “Startup.Auth.cs”.
  2. After successfully building than run your application and login with the registered user above I registered with a Username:jeshal and Password:123456.

Above example, you Understand how asp.net Identity is work now below I will show you how the same thing I will so you using web API it’s already in my project.

Following basic step to understand the same thing with API. how to use ASP.NET Identity and [Authorize] Attribute?

[Authorize] Attribute:

  1. I have already two web API Controller like “ValuesController” another is “AccountController”.
  2. Now “Open your Google Chrome and add extension “APIRestClient”.
  3. Now I will Use Register method using API Call like this “www.yoururl.com/api/Account/register”.
  4. After register, you more Call for Token using “www.yoururl.com/Token” like this.

    When you click on Send then response like below formate.

    You will see here your access token. this token is used for calling your API method.

Copy to Clipboard
  1. Now I will show just when stepping how to test your access_token and how to getting information from your API call.
    in my case, I just got my registration details using “UserInfo” method form API “www.yoururl.com/UserInfo/api/Account/UserInfo”

    Now you have shown here response look like this.

Copy to Clipboard

I hope that this article for setup your asp.net identity authentication is helpful. leave replays if you found any difficulty in the authentication. If you want more solutions and tricks related to Asp.Net MVC, nopCommerce Store, Umbraco cms, Orchard and many more at Here.

Thank you…!!