在ASP.NET Identity中向AspNetUserClaims添加列 [英] Adding columns to AspNetUserClaims in ASP.NET Identity

查看:205
本文介绍了在ASP.NET Identity中向AspNetUserClaims添加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Microsoft.AspNet.Identity.Core 2.2.1 in my solution. I need to integrate this with another system that should automatically add claims.

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

In order to keep track of which claims are manually added and which are created by an external system, I would like another column on my AspNetUserClaims table:

ExternalSystem varchar(64) null

如果我的解决方案中实际上没有Claims类,该如何使用EF迁移(或无需使用)进行操作?

How do I do this using EF Migrations (or without if nescessary), seeing as I don't actually have the claims class in my solution?

推荐答案

您可以创建我们自己的自定义应用程序用户声明类,该类继承自IdentityUserClaim类并添加自定义字段.

You can create our own custom app user claim class which is derived from IdentityUserClaim class and add custom fields.

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类,例如:

After that, you need to configure ApplicationDbContext and ApplicationUser classes like:

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

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

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

Additionally, you have to create custom UserStore class like:

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

然后您可以像下面那样使用ApplicationUserManager:

And then you can use ApplicationUserManager like:

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

这篇关于在ASP.NET Identity中向AspNetUserClaims添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆