【问题标题】:ASP.NET Core 2.0 Identity Modifying Primary Key from String to GuidASP.NET Core 2.0 Identity 将主键从字符串修改为 Guid
【发布时间】:2018-09-22 14:29:40
【问题描述】:

我已按照this 文章将身份类的主键数据类型从 String 修改为 Guid,它确实为 SQL Server 中的各个列创建了 uniqueidentifier 数据类型,而不是默认的 NVARCHAR 但我我无法使用 UserManagerSignInManager 类,因为它们拒绝接受自定义的 ApplicationUser 类(如文章中所述扩展 IdentityUser)而不是默认的 IdentityUser 类。 本文没有详细介绍,而是通过查看 SO 上其他类似问题的答案 就像 this one 一样,我似乎要么必须更改 Identity 的所有类才能实现这一目标,要么使用我不完全确定如何使用的“其他”UserManager 类。

我的自定义身份模型如下所示:

public class IdentityModels
{
    // Add profile data for application users by adding properties to the ApplicationUser class
    public class ApplicationUser : IdentityUser<Guid>
    {
    }
    public class ApplicationRole : IdentityRole<Guid>
    {
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(GetConnectionString());
        }

        private static string GetConnectionString()
        {
            const string databaseName = "testname";
            const string databaseUser = "testuser";
            const string databasePass = "testpass";

            return $"Server=localhost;" +
                   $"database={databaseName};" +
                   $"uid={databaseUser};" +
                   $"pwd={databasePass};" +
                   $"pooling=true;";
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            // Customize the ASP.NET Core Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Core Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);
        }
    }
}

在 Identity 2.0 中更改身份类的主键属性的数据类型以便UserManagerSignInManager 接受修改的正确方法是什么?

【问题讨论】:

    标签: c# asp.net asp.net-identity


    【解决方案1】:

    根据此处的文档:

    https://docs.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-2.2

    您将需要创建从每个 asp.net 身份类型派生的自定义类,它们都有一个 TKey 通用参数,允许您指定主键的类型。以下是示例自定义身份类的所有签名:

    public class ApplicationUserToken : IdentityUserToken<Guid> { }
    public class ApplicationUserLogin : IdentityUserLogin<Guid>{ }
    public class ApplicationRoleClaim : IdentityRoleClaim<Guid>{ }
    public class ApplicationUserRole : IdentityUserRole<Guid>{ }
    public class ApplicationUser : IdentityUser<Guid>{ }
    public class ApplicationUserClaim : IdentityUserClaim<Guid>{ }
    public class ApplicationRole : IdentityRole<Guid> { }
    

    然后在您的应用程序数据库上下文中从 IdentityDbContext 继承,将所有身份类替换为自定义类:

    public class IdentityDbContext<TUser, TRole, TKey> : IdentityDbContext<TUser, TRole, TKey, IdentityUserClaim<TKey>, IdentityUserRole<TKey>, IdentityUserLogin<TKey>, IdentityRoleClaim<TKey>, IdentityUserToken<TKey>>
    

    像这样:

     public class AppDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid, ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin, ApplicationRoleClaim, ApplicationUserToken> { }
    

    然后将其添加到您的 Startup.cs 文件中:

    services.AddIdentity<ApplicationUser, ApplicationRole>()
                    .AddEntityFrameworkStores<AppDbContext>()
                    .AddDefaultTokenProviders();
    

    最后您可以通过控制台运行新的迁移并更新您的数据库:

    PM> Add-Migration cmdlet Add-Migration at command pipeline position 1 Supply values for the following parameters: Name: Guid-PK PM> Update-Database -verbose

    干杯。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-04
      • 1970-01-01
      相关资源
      最近更新 更多