[英]ASP.NET Core 2.0 Identity Modifying Primary Key from String to Guid
I have followed this article to modify Primary Key data type of the Identity classes from String to Guid and it does create the uniqueidentifier
data type for respective columns in the SQL Server instead of the default NVARCHAR
but I am unable to use the UserManager
and SignInManager
classes as they refuse to accept the customized ApplicationUser class (that extends IdentityUser, as described in the article) instead of the default IdentityUser
class. 我按照这个文章从字符串的Guid修改身份类的主键的数据类型和它创造的
uniqueidentifier
为SQL Server而不是默认的各列中的数据类型NVARCHAR
但我无法使用UserManager
和SignInManager
类因为他们拒绝接受自定义的ApplicationUser类(扩展了IdentityUser,如本文中所述),而不是默认的IdentityUser
类。 The article does not go into much detail and by looking at answers to other similar questions on SO like this one it seems that either I have to change all the classes of the Identity to accomplish this goal or use the 'other' UserManager
class which I am not entirely sure how to. 本文不进入太多细节,并通过寻找答案,其他类似的问题,所以像这样一个似乎要么我必须要改变标识的所有类实现这一目标,或使用“其他”
UserManager
类我不确定如何操作。
My customized Identity models look like this: 我的自定义身份模型如下所示:
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);
}
}
}
What is the correct way to change the data type for the Primary Key attribute of the Identity Classes in Identity 2.0 so that UserManager
and SignInManager
accept the modifications? 更改Identity 2.0中Identity类的Primary Key属性的数据类型的正确方法是什么,以便
UserManager
和SignInManager
接受修改?
Based on the documentation here: 根据此处的文档:
https://docs.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-2.2 https://docs.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-2.2
You will need to create custom classes that derive from each asp.net identity type which all have a TKey generic parameter which will allow you to specify the type for the primary key. 您将需要创建从每个asp.net标识类型派生的自定义类,这些自定义类都具有TKey通用参数,该参数将允许您指定主键的类型。 Here are all the signatures of example custom identity classes:
这是示例定制标识类的所有签名:
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> { }
Then in your application DB context inherit from IdentityDbContext replacing all the identity classes with the custom ones: 然后,在您的应用程序数据库上下文中,从IdentityDbContext继承,用自定义类替换所有身份类:
public class IdentityDbContext<TUser, TRole, TKey> : IdentityDbContext<TUser, TRole, TKey, IdentityUserClaim<TKey>, IdentityUserRole<TKey>, IdentityUserLogin<TKey>, IdentityRoleClaim<TKey>, IdentityUserToken<TKey>>
Like this: 像这样:
public class AppDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid, ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin, ApplicationRoleClaim, ApplicationUserToken> { }
Then add this to your Startup.cs file: 然后将其添加到您的Startup.cs文件中:
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<AppDbContext>()
.AddDefaultTokenProviders();
Last you can run a new migration and update your data base via the console: 最后,您可以运行新迁移并通过控制台更新数据库:
PM> Add-Migration cmdlet Add-Migration at command pipeline position 1 Supply values for the following parameters: Name: Guid-PK PM> Update-Database -verbose
Cheers. 干杯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.