ASP.NET 之 UserRoleIdentity
Add User_Role_Identity function on existing project
1.1 appsettings.json
"ConnectionStrings": {
"DefaultConnection":"Server=WIN-R68LSH0BM46\\SQLEXPRESS;Database=MvcMusicStore-UserRole;Trusted_Connection=True;MultipleActiveResultSets=true",
"MvcMusicDatabase": "Server=WIN-R68LSH0BM46\\SQLEXPRESS;Database=MvcMusicStore;Trusted_Connection=True;MultipleActiveResultSets=true"
}
1.2 Startup.cs
notice: ApplicationDbContext -- DefaultConnection
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer( Configuration.GetConnectionString("DefaultConnection")));
Add configuration
services.AddDefaultIdentity<IdentityUser>() .AddRoles<IdentityRole>() //...added to provide RoleManager support .AddEntityFrameworkStores<ApplicationDbContext>();
1.3 Tool - NuGet Manager - console
=> generate database `MvcMusicStore-UserRole`
update-Database -context ApplicationDbContext
1.4 Add Identity
Result:
2. Adding Models/controllers/Views
github: https://github.com/pear2020/IdentityClass/tree/main/IdentityClass