简体   繁体   English

在ASP.NET Core 2.0中更新身份

[英]Updating Identity in ASP.NET Core 2.0

I'm having an issue trying to replace IdentityUser navigation properties in ASP.NET Core Identity which was a breaking change in 2.0 update. 我在尝试替换ASP.NET Core Identity中的IdentityUser导航属性时遇到问题,这是2.0更新中的重大更改

I looked at Add IdentityUser POCO Navigation Properties , but there was scant explanation on how to actually do this. 我查看了Add IdentityUser POCO导航属性 ,但是对如何实际执行此操作的解释很少。

In ApplicationUser.cs I added the following: 在ApplicationUser.cs中,我添加了以下内容:

public virtual ICollection<IdentityUserRole<string>> Roles
    { get; } = List<IdentityUserRole<string>>();

But when I attempted to use that navigation property it used ApplicationUserId instead of UserId. 但是,当我尝试使用该导航属性时,它使用的是ApplicationUserId而不是UserId。

Any suggestions would be welcome as this isn't intuitive, hopefully the identity team can further elaborate in the documentation. 任何建议都将受到欢迎,因为这不直观,希望身份团队可以在文档中进一步阐述。

In my case I have three derived clases ApplicationUserClaim, ApplicationUserLogin, ApplicationUserRole. 就我而言,我有三个派生的类ApplicationUserClaim,ApplicationUserLogin,ApplicationUserRole。 I do that because I need to add some custom properties. 我这样做是因为我需要添加一些自定义属性。

Derived classes without added properties should look like this. 没有添加属性的派生类应如下所示。

public class ApplicationUserLogin : IdentityUserLogin<string>
{
    public virtual ApplicationUser ApplicationUser { get; set; }
}


public class ApplicationUserRole: IdentityUserRole<string>
{
    public virtual ApplicationUser ApplicationUser { get; set; }
}


public class ApplicationUserClaim: IdentityUserClaim<string>
{
     public virtual ApplicationUser ApplicationUser { get; set; }
}

On your ApplicationUser add the following code: 在您的ApplicationUser上,添加以下代码:

public virtual ICollection<ApplicationUserRole> Roles { get; } = new List<ApplicationUserRole>();

public virtual ICollection<ApplicationUserClaim> Claims { get; } = new List<ApplicationUserClaim>();

public virtual ICollection<ApplicationUserLogin> Logins { get; } = new List<ApplicationUserLogin>();

On your ApplicationDbContext add the following code: 在您的ApplicationDbContext上,添加以下代码:

modelBuilder.Entity<ApplicationUserClaim>().HasOne(pt => pt.ApplicationUser).WithMany(t => t.Claims).HasForeignKey(pt => pt.UserId);

modelBuilder.Entity<ApplicationUserRole>().HasOne(pt => pt.ApplicationUser).WithMany(t => t.Roles).HasForeignKey(pt => pt.UserId);

modelBuilder.Entity<ApplicationUserLogin>().HasOne(pt => pt.ApplicationUser).WithMany(t => t.Logins).HasForeignKey(pt => pt.UserId);

Make a Migration and you are done. 进行迁移,您就完成了。

Adding the following to OnModelCreating in ApplicationDbContext.cs fixed the problem for me which I found from GitHub issue How to include Roles in IdentityUser? 在ApplicationDbContext.cs中的OnModelCreating中添加以下内容为我解决了这个问题,我从GitHub问题中找到了如何在IdentityUser中包括角色?

builder.Entity<ApplicationUser>().HasMany(p => p.Roles)
    .WithOne().HasForeignKey(ur => ur.UserId).IsRequired();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM