.net core2.2,生成WebApi或者MVC项目后,Identity
1.增加ApplicationUser.cs文件,内容如下
public class ApplicationUser : IdentityUser<int> { } public class ApplicationRole : IdentityRole<int> { }
2.修改ApplicationDbContext.cs文件,内容如下
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) {
} }
3.修改Startup.cs,内容如下
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 CreateIdentitySchema
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.
5.在PM运行Update-Database
PM> Update-Database
6.打开ViewsShared\_LoginPartial.cshtml文件,找到如下内容
@using Microsoft.AspNetCore.Identity @inject SignInManager<IdentityUser> SignInManager @inject UserManager<IdentityUser> UserManager
修改为:
@using Microsoft.AspNetCore.Identity @inject SignInManager<ApplicationUser> SignInManager @inject UserManager<ApplicationUser> UserManager