繁体   English   中英

ASP.NET Core 2.0身份修改从字符串到Guid的主键

[英]ASP.NET Core 2.0 Identity Modifying Primary Key from String to Guid

我按照这个文章从字符串的Guid修改身份类的主键的数据类型和它创造的uniqueidentifier为SQL Server而不是默认的各列中的数据类型NVARCHAR但我无法使用UserManagerSignInManager类因为他们拒绝接受自定义的ApplicationUser类(扩展了IdentityUser,如本文中所述),而不是默认的IdentityUser类。 本文不进入太多细节,并通过寻找答案,其他类似的问题,所以像这样一个似乎要么我必须要改变标识的所有类实现这一目标,或使用“其他” 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中Identity类的Primary Key属性的数据类型的正确方法是什么,以便UserManagerSignInManager接受修改?

根据此处的文档:

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

干杯。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM