Reputation: 1325
I have already started my dotnet core mvc project without identity on Mac with CLI and now I want to add this feature. The only option I have known till now is to create a new project by
dotnet new mvc --auth
Is there a better way to add identity to an existing project? I hope there is a 'dotnet new' command.
Upvotes: 16
Views: 33273
Reputation: 1325
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
Upvotes: 16
Reputation: 15252
I was following this MS tutorial but had some problems so I liste them here and solutions how I fixed them.
I had problems with connection string. This is how my connection string looked on the end in appsettings.json
since my MS SQL server was working on specific port:
"AuthDbContextConnection": "Data Source=xxx.xxx.xxx.xxx,10008;Initial Catalog=dbName;Persist Security Info=True;User ID=sqluser;Password=sqlpassword"
xxx.xxx.xxx.xxx
is here instead of IP address.
I wasn't able to install Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore
so I went on packet page here and installed latest version for my project (.NET Core 3.x) which is this. So I've installed package with :
Install-Package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore -Version 3.1.10
After that I run
Add-Migration CreateIdentitySchema -Context AuthDbContext
It didn't work without context parameter. This created 2 migrations which I ran separately:
Update-Database 20201217102436_InitialCreate -Context AuthDbContext
and than
Update-Database 20201218083037_CreateIdentitySchema -Context AuthDbContext
And that worked. Tables were created in MS SQL server successfully.
Upvotes: 1
Reputation: 1315
According to learn.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();
}
}
Upvotes: 24
Reputation: 1368
You can manage this through the NuGet Package Manager:
Tools -> NuGet Package Manager -> Console
$
Install-Package Microsoft.AspNet.Identity.Core
Upvotes: 5