Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.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 Identity中的AspNetUserClaims添加列_C#_Asp.net_Entity Framework_Asp.net Identity - Fatal编程技术网

C# 向ASP.NET Identity中的AspNetUserClaims添加列

C# 向ASP.NET Identity中的AspNetUserClaims添加列,c#,asp.net,entity-framework,asp.net-identity,C#,Asp.net,Entity Framework,Asp.net Identity,我在解决方案中使用了Microsoft.AspNet.Identity.Core 2.2.1。我需要将其与另一个系统集成,该系统将自动添加索赔 为了跟踪哪些索赔是手动添加的,哪些是由外部系统创建的,我希望在我的AspNetUserClaims表上有另一列: ExternalSystem varchar(64) null 鉴于我的解决方案中实际上没有claims类,我如何使用EF迁移(如果必要,也可以不使用EF迁移)实现这一点?您可以创建我们自己的自定义应用程序用户claim类,该类派生自Ide

我在解决方案中使用了
Microsoft.AspNet.Identity.Core 2.2.1
。我需要将其与另一个系统集成,该系统将自动添加索赔

为了跟踪哪些索赔是手动添加的,哪些是由外部系统创建的,我希望在我的
AspNetUserClaims
表上有另一列:

ExternalSystem varchar(64) null

鉴于我的解决方案中实际上没有claims类,我如何使用EF迁移(如果必要,也可以不使用EF迁移)实现这一点?

您可以创建我们自己的自定义应用程序用户claim类,该类派生自IdentityUserClaim类,并添加自定义字段

public class ApplicationUserClaim : IdentityUserClaim
{
    public ApplicationUserClaim() { }

    public ApplicationUserClaim(string userId, string claimType,
        string claimValue, string externalSystem)
    {
        UserId = userId;
        ClaimType = claimType;
        ClaimValue = claimValue;
        ExternalSystem = externalSystem;
    }

    [MaxLength(64)]
    public string ExternalSystem { get; set; }
}
之后,您需要配置ApplicationDbContext和ApplicationUser类,如:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole, string,
        IdentityUserLogin, IdentityUserRole, ApplicationUserClaim>
{
    // ...
}

public class ApplicationUser : IdentityUser<string, IdentityUserLogin, IdentityUserRole, ApplicationUserClaim>
{
    // ...
}

您在这里使用EF Core吗?使用附加属性派生您自己的子类,并在连接标识时使用该属性。@ChrisPickford:subclass我得到了,但您能否添加一个提示,说明在哪里“连接它”?我是否需要将其中一个经理划分为子类?戴维格:不,满了。NET@TroelsLarsen-我担心,如果您想偏离标准实现,那么实现起来会非常困难。您需要创建许多Identity API的实现。本文可能会帮助您:@ChrisPickford:我已经为用户添加了一些字段,但我会看看是否可以找到适当的位置来扩展声明。谢谢,啊。比我想象的要容易。我会在有时间的时候尽快检查并接受答案。我还没有脱离困境。在ApplicationUserManager中似乎也需要进行更改,其中UserStore还需要一些额外的泛型参数。我忘记了usermanager和UserStore。我的答案已经更新了。谢谢,我现在已经成功了。还应注意,当将
ApplicationUser
IdentityUser
切换到
IdentityUser
时,您会遇到以下问题:非常感谢您的帮助!是否可以使索赔表正常化?我的意思是将其分为两个表,分别称为
ClaimTypes
Claims
本身。
public class ApplicationUserStore : UserStore<ApplicationUser, IdentityRole, string,
        IdentityUserLogin, IdentityUserRole, ApplicationUserClaim>
{
    public ApplicationUserStore(ApplicationDbContext context)
        : base(context)
    {
    }
} 
var ctx = new ApplicationDbContext();
var manager = new ApplicationUserManager(new ApplicationUserStore(ctx));