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

时间:2018-09-22 14:29:40

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

我已经按照this的文章将Identity类的Primary Key数据类型从String修改为Guid,并且确实为SQL Server中的各个列创建了uniqueidentifier数据类型,而不是默认的{ {1}},但是我无法使用NVARCHARUserManager类,因为它们拒绝接受定制的ApplicationUser类(扩展了IdentityUser,如本文所述),而不是默认的{{1 }}类。 本文不做过多详细介绍,而是通过查看SO上其他类似问题的答案 像this one一样,似乎我必须更改Identity的所有类以实现此目标,或者使用我不确定如何做到的'other'SignInManager类。

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

IdentityUser

更改Identity 2.0中Identity类的Primary Key属性的数据类型以使UserManagerpublic 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); } } } 接受修改的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

基于此处的文档:

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

干杯。