【发布时间】:2018-09-22 03:36:09
【问题描述】:
尝试在 ASP.NET Core 2.0 中实现 Identity。解决这个问题有很多问题。
Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("Ctrack6_Custom"))
);
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
etc...
ApplicationUser.cs 使用 Guid 作为密钥。也在Role等中设置
// Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUser : IdentityUser<Guid>
{
[MaxLength(50)]
public string FirstName { get; set; }
[MaxLength(50)]
public string LastName { get; set; }
[MaxLength(5)]
public string OrgCode { get; set; }
public ApplicationUser() : base()
{
}
}
ApplicationDbContext.cs 在该文件中的类定义中引发错误。 ApplicationDbContext 抛出此错误:
类型“App.Identity.ApplicationUser”不能用作泛型类型或方法“IdentityDbContext”中的类型参数“TUser”。没有从“App.Identity.ApplicationUser”到“Microsoft.AspNetCore.Identity.IdentityUser”的隐式引用转换。
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
builder.Entity<blah>()
.HasKey(c => new { fields, for, key });
}
public DbSet<etc> Etcs {get; set; }
}
【问题讨论】:
标签: c# asp.net-identity asp.net-core-2.0