如何在ASP.NET MVC中使用自定义属性扩展Identity?

Jun*_*ior 5 c# asp.net-mvc identity entity-framework asp.net-mvc-5

我似乎与身份有仇恨/爱情关系.我喜欢它,因为它是大多数应用程序的完整解决方案.但我讨厌它,因为延伸并不是一件容易的事.我觉得它比它应该更加复杂.

我试图将自定义属性和外键添加到我的用户模型,但这似乎是一项非常困难的任务.

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

我按照另一个问题答案中的说明试图添加两个新的外键,并能够从我的控制器中获取它们的值.

这是我到目前为止所做的.我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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我还创建了一个扩展类,允许我从控制器中获取值

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

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

这是错误

在模型生成期间检测到一个或多个验证错误:

ApplicationUser_Claims_Source_ApplicationUser_Claims_Target ::参照约束的从属角色中的所有属性的类型必须与主体角色中的相应属性类型相同.实体'IdentityUserClaim'上的属性'MyUserId'的类型与参照约束'ApplicationUser_Claims'中实体'ApplicationUser'上的属性'MyUserId'的类型不匹配.ApplicationUser_Logins_Source_ApplicationUser_Logins_Target ::参照约束的从属角色中的所有属性的类型必须与主体角色中的相应属性类型相同.实体'IdentityUserLogin'上的属性'MyUserId'的类型与引用约束'ApplicationUser_Logins'中实体'ApplicationUser'上的属性'MyUserId'的类型不匹配.ApplicationUser_Roles_Source_ApplicationUser_Roles_Target ::参照约束的从属角色中的所有属性的类型必须与主体角色中的相应属性类型相同.实体'IdentityUserRole'上的属性'MyUserId'的类型与参照约束'ApplicationUser_Roles'中实体'ApplicationUser'上的属性'MyUserId'的类型不匹配.

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

Add-Migration InitialCreate
Run Code Online (Sandbox Code Playgroud)

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

小智 0

Identity 有办法将主键(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>
{
}
Run Code Online (Sandbox Code Playgroud)

ApplicationUserStore然后我们还需要一个定制

public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public ApplicationUserStore(DbContext context)
        : base(context)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)