我似乎与该身份存在仇恨/爱恋关系。我喜欢它,因为它是大多数应用程序的完整解决方案。但我讨厌它,因为扩展并非易事。我觉得它比应该的更复杂。

我正在尝试向用户模型添加自定义属性和外键,但这似乎是一项非常困难的任务。

我需要添加一个名为Identity的新UserId字段,因为Id是将由数据库自动生成的字符串。然后,我需要为Company模型添加一个外键,为Location模型添加另一个。

我遵循了answer from this other question中的指令,试图添加两个新的外键并能够从我的 Controller 中获取它们的值。

到目前为止,这是我所做的。进行修改后,我的ApplicationUser类如下所示

public class ApplicationUser : IdentityUser
{
    [Key]
    public int MyUserId { get; set; }

    [ForeignKey("Company")]
    public int CompanyId { get; set; }

    [ForeignKey("Location")]
    public int CurrentLocationId { get; set; }

    public virtual Company Company { get; set; }

    public virtual Location Location { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here

        userIdentity.AddClaim(new Claim("MyUserId", this.MyUserId.ToString() ));

        userIdentity.AddClaim(new Claim("CompanyId", this.CompanyId.ToString() ));

        userIdentity.AddClaim(new Claim("CurrentLocationId", this.CurrentLocationId.ToString()));

        return userIdentity;
    }
}

我还创建了一个扩展类,它使我可以像这样从 Controller 获取值
public static class IdentityExtensions
{
    public static int GetComapnyId(this IIdentity identity)
    {
        var claim = ((ClaimsIdentity)identity).FindFirst("ComapnyId");
        // Test for null to avoid issues during local testing
        return (claim != null) ? Int32.Parse(claim.Value) : 0;
    }

    public static int GetCurrentLocationId(this IIdentity identity)
    {
        var claim = ((ClaimsIdentity)identity).FindFirst("CurrentLocationId");
        // Test for null to avoid issues during local testing
        return (claim != null) ? Int32.Parse(claim.Value) : 0;
    }


    public static int GetMyUserId(this IIdentity identity)
    {
        var claim = ((ClaimsIdentity)identity).FindFirst("MyUserId");
        // Test for null to avoid issues during local testing
        return (claim != null) ? Int32.Parse(claim.Value) : 0;
    }

}

但是,当我尝试添加新的迁移时,我遇到了错误“在下面列出”

这是错误

在模型生成过程中检测到一个或多个验证错误:



这是我用来创建InitialCreate迁移的命令
Add-Migration InitialCreate

如何添加我的外键并能够正确地从 Controller 中获取它们?

最佳答案

身份可以将主键(Id字段)更改为int。它有很多样板/空类,但对我们有用。也许这会有所帮助吗?

public class ApplicationUser : IdentityUser<int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    [ForeignKey("Company")]
    public int CompanyId { get; set; }

    [ForeignKey("Location")]
    public int CurrentLocationId { get; set; }

    public virtual Company Company { get; set; }

    public virtual Location Location { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here

        userIdentity.AddClaim(new Claim("CompanyId", this.CompanyId.ToString() ));

        userIdentity.AddClaim(new Claim("CurrentLocationId", this.CurrentLocationId.ToString()));

        return userIdentity;
    }
}

public class ApplicationUserLogin : IdentityUserLogin<int>
{
}

public class ApplicationUserRole : IdentityUserRole<int>
{
    [ForeignKey("RoleId")]
    public virtual ApplicationRole Role { get; set; }

    [ForeignKey("UserId")]
    public virtual ApplicationUser User { get; set; }
}

public class ApplicationRole : IdentityRole<int, UserRole>
{

}

public class ApplicationUserClaim : IdentityUserClaim<int>
{
}

然后我们还需要一个自定义的ApplicationUserStore
public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public ApplicationUserStore(DbContext context)
        : base(context)
    {
    }
}

10-04 12:16
查看更多