How add ASP.NET Core identity to existing Core mvc project?
You can manage this through the NuGet Package Manager:
Tools -> NuGet Package Manager -> Console
$
Install-Package Microsoft.AspNet.Identity.Core
According to docs.microsoft.com you can scaffold identity into an existing MVC project with aspnet-codegenerator.
1) If you have not previously installed the ASP.NET Core scaffolder, install it now:
dotnet tool install -g dotnet-aspnet-codegenerator
2) Add a package reference to Microsoft.VisualStudio.Web.CodeGeneration.Design to the project (*.csproj) file. Run the following command in the project directory:
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet restore
3) Run the following command to list the Identity scaffolder options:
dotnet aspnet-codegenerator identity -h
4) In the project folder, run the Identity scaffolder with the options you want. For example, to setup identity with the default UI and the minimum number of files, run the following command:
dotnet aspnet-codegenerator identity --useDefaultUI
5) The generated Identity database code requires Entity Framework Core Migrations. Create a migration and update the database. For example, run the following commands:
dotnet ef migrations add CreateIdentitySchema
dotnet ef database update
6) Call UseAuthentication after UseStaticFiles:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthentication(); // <-- add this line
app.UseMvcWithDefaultRoute();
}
}
You need to add this NuGet package via CLI in VS Code:
dotnet add package Microsoft.AspNetCore.Identity
And if you want the standard UI pages you can install this package which contains everything embedded:
dotnet add package Microsoft.AspNetCore.Identity.UI