Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP.NET MVC中使用自定义属性扩展标识?_C#_Asp.net Mvc_Entity Framework_Asp.net Mvc 5_Identity - Fatal编程技术网

C# 如何在ASP.NET MVC中使用自定义属性扩展标识?

C# 如何在ASP.NET MVC中使用自定义属性扩展标识?,c#,asp.net-mvc,entity-framework,asp.net-mvc-5,identity,C#,Asp.net Mvc,Entity Framework,Asp.net Mvc 5,Identity,我似乎对这个身份有一种恨/爱的关系。我喜欢它,因为它是大多数应用程序的完整解决方案。但我讨厌它,因为扩展不是一项容易的任务。我觉得它比应该的更完整 我正在尝试向我的用户模型添加自定义属性和外键,但这似乎是一项非常困难的任务 我需要添加一个新的Identity字段,名为UserId,因为Id是由数据库自动生成的字符串。然后,我需要在公司模型中添加一个外键,在位置模型中添加另一个外键 我按照中的说明尝试添加两个新外键,并能够从控制器获取它们的值 以下是我迄今为止所做的工作。修改后的Applicati

我似乎对这个身份有一种恨/爱的关系。我喜欢它,因为它是大多数应用程序的完整解决方案。但我讨厌它,因为扩展不是一项容易的任务。我觉得它比应该的更完整

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

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

我按照中的说明尝试添加两个新外键,并能够从控制器获取它们的值

以下是我迄今为止所做的工作。修改后的
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;
    }
}
但我在尝试添加新迁移时遇到了错误“如下所列”

Add-Migration InitialCreate
这里是错误

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

ApplicationUser_声明\u源\u ApplicationUser_声明\u目标:: 引用的从属角色中所有属性的类型 约束必须与中相应的特性类型相同 主要作用。实体上属性“MyUserId”的类型 “IdentityUserClaim”与上的属性“MyUserId”的类型不匹配 引用约束中的实体“ApplicationUser” “应用程序用户声明”。 ApplicationUser\u登录\u源\u ApplicationUser\u登录\u目标:: 引用的从属角色中所有属性的类型 约束必须与中相应的特性类型相同 主要作用。实体上属性“MyUserId”的类型 “IdentityUserLogin”与上的属性“MyUserId”的类型不匹配 引用约束中的实体“ApplicationUser” “应用程序用户登录”。 ApplicationUser\u Roles\u Source\u ApplicationUser\u Roles\u Target::类型 引用约束的从属角色中的所有属性的 必须与主体中的相应属性类型相同 角色实体“IdentityUserRole”上的属性“MyUserId”的类型无效 与中实体“ApplicationUser”的属性“MyUserId”的类型不匹配 引用约束“ApplicationUser_角色”

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

Add-Migration InitialCreate

如何添加外键并正确地从控制器获取外键?

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

您是如何定义身份声明的?我真的认为最好是将身份和其他实体分别保存在不同的上下文中。该标识有一个
用户ID
,您可以稍后使用它从主上下文加载您的用户。@GertArnold我没有对我的
IdentityUserClaim
@AdrianIftode进行任何更改。我没有跟踪您。我在这里所要做的就是在我的应用程序中为其他模型添加外键,我所建议的是采用不同的方法。在自己的上下文中使用标识,并在自己的上下文中使用模型。您始终可以使用该扩展方法访问UserId,并像往常一样加载用户+公司和位置。
public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public ApplicationUserStore(DbContext context)
        : base(context)
    {
    }
}