gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:47:59 24 4
gpt4 key购买 nike

我在我的解决方案中使用 Microsoft.AspNet.Identity.Core 2.2.1。我需要将其与另一个应自动添加声明的系统集成。

为了跟踪哪些声明是手动添加的以及哪些声明是由外部系统创建的,我想在我的 AspNetUserClaims 表中增加一列:

ExternalSystem varchar(64) null

我如何使用 EF 迁移(如果需要,则不使用)执行此操作,因为我的解决方案中实际上没有声明类?

最佳答案

您可以创建我们自己的自定义应用程序用户声明类,该类派生自 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>
{
// ...
}

此外,您必须创建自定义 UserStore 类,例如:

public class ApplicationUserStore : UserStore<ApplicationUser, IdentityRole, string,
IdentityUserLogin, IdentityUserRole, ApplicationUserClaim>
{
public ApplicationUserStore(ApplicationDbContext context)
: base(context)
{
}
}

然后您可以像这样使用 ApplicationUserManager:

var ctx = new ApplicationDbContext();
var manager = new ApplicationUserManager(new ApplicationUserStore(ctx));

关于c# - 向 ASP.NET Identity 中的 AspNetUserClaims 添加列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42998020/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com