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.
Asked
Active
Viewed 5,379 times
7
-
I've had this link for a while in case i encounter that problem. Not sure if is what you looking for but could give you a hint. http://www.asp.net/identity/overview/migrations/migrating-an-existing-website-from-sql-membership-to-aspnet-identity – DJ. Nov 04 '15 at 21:00
-
Have a look at this answer: [Adding ASP.NET MVC5 Identity Authentication to an existing project](http://stackoverflow.com/questions/31960433/adding-asp-net-mvc5-identity-authentication-to-an-existing-project/31963828#31963828) – Sam FarajpourGhamari Nov 05 '15 at 17:53
-
Did you already have any kind of authentication before? – Matt Feb 27 '19 at 14:30
-
Could you please accept my answer if that solves your problem? Please please please :D – Hao Zhang Mar 27 '20 at 15:18
1 Answers
2
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") { }
}
Add a connection string in your Web.config file
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.

Hao Zhang
- 177
- 6
-
Thanks for this, I was searching the web why the add-migration command didn't generate any of the built-in aspnet identity tables. I forgot to inherit from `IdentityDbContext`! – kipusoep Jan 30 '20 at 15:05