.net core2.2,生成WebApi或者MVC項目後,Identityhtml
1.增長ApplicationUser.cs文件,內容以下cookie
public class ApplicationUser : IdentityUser<int> { } public class ApplicationRole : IdentityRole<int> { }
2.修改ApplicationDbContext.cs文件,內容以下ide
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) {
} }
3.修改Startup.cs,內容以下this
public void ConfigureServices(IServiceCollection services) { services.Configure<CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer( Configuration.GetConnectionString("DriverBase"))); services.AddDefaultIdentity<ApplicationUser>() .AddDefaultUI(UIFramework.Bootstrap4) .AddEntityFrameworkStores<ApplicationDbContext>(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); }
4.在PM運行Add-Migration CreateIdentitySchemaspa
PM> Add-Migration CreateIdentitySchema
Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 2.2.1-servicing-10028 initialized 'ApplicationDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
To undo this action, use Remove-Migration..net
5.在PM運行Update-Databasecode
PM> Update-Databasehtm
6.打開Views\Shared\_LoginPartial.cshtml文件,找到以下內容blog
@using Microsoft.AspNetCore.Identity @inject SignInManager<IdentityUser> SignInManager @inject UserManager<IdentityUser> UserManager
修改成:ci
@using Microsoft.AspNetCore.Identity @inject SignInManager<ApplicationUser> SignInManager @inject UserManager<ApplicationUser> UserManager