Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add asp.net identity to existing web api project?

I have 2 projects in my solution. First one is simple mvc project and the other one is web api. There was no pre-written code in web api. I put all logics myself. Now I want to add asp.net identity in the web api project. How can I do that? Thanks.

like image 936
Diamond Stone Avatar asked Nov 04 '15 12:11

Diamond Stone


People also ask

How do I add a Web API to an existing project?

In order to add a Web API Controller, you will need to Right Click the Controllers folder in the Solution Explorer and select on Add and then New Item. Now from the Add New Item window, choose the API Controller – Empty option as shown below. Then give it a suitable name and click Add.

What is ASP.NET identity in Web API?

ASP.NET Core Identity: Is an API that supports user interface (UI) login functionality. Manages users, passwords, profile data, roles, claims, tokens, email confirmation, and more.


1 Answers

In your web api project, you can do this: 1. Create a DbContext class that looks like this:

public class DataContext : IdentityDbContext<IdentityUser>
{
    public DataContext() : base("ConnectionStringLocal") { }
}
  1. Add a connection string in your Web.config file

  2. In Package manager console, do Enable-Migrations, Add-Migration IdentityUpdate, Update-Database. This will create a database that has asp.net identity built in.

Please let me know if you have additional questions.

like image 170
Hao Zhang Avatar answered Oct 20 '22 20:10

Hao Zhang