简体   繁体   English

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

[英]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但我无法使用UserManagerSignInManager类因为他们拒绝接受自定义的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属性的数据类型的正确方法是什么,以便UserManagerSignInManager接受修改?

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.

相关问题 在ASP.NET Identity 2.0中将用户的主键从字符串更改为Int-麻烦吗? - Change primary key from string to Int for users in ASP.NET Identity 2.0 - Worth the trouble? 在Controller中更改ASP.NET Core 2.0标识连接字符串 - Changing ASP.NET Core 2.0 Identity Connection String in Controller 如何避免在asp.net MVC身份表中使用GUID(作为UserId)作为主键? - How to avoid using GUID (As UserId) as primary key in asp.net mvc identity tables? Identity ASP.NET 核心寄存器修改 - Identity ASP.NET Core register modifying 为什么ASP.NET Identity 2.0使用GUID /字符串作为用户ID? - Why is ASP.NET Identity 2.0 using a GUID/string as user id? 在ASP.NET Core 2.0中更新身份 - Updating Identity in ASP.NET Core 2.0 Identity Server 4(2.0)不读取Asp.Net核心标识cookie - Identity Server 4 (2.0) not reading Asp.Net Core Identity cookies 在 ASP.NET Identity Core 中,我可以更改 UserToken 主键吗 - In ASP.NET Identity Core can I change UserToken primary key 如何获取身份 id 类型 guid 并分配给另一个表中的外键 id。 适用于Asp.net内核 - How to get identity id type guid and assign to foreign key id in another table. Apply in Asp.net Core Asp.net Core Identity 2.0 Google注销 - Asp.net Core Identity 2.0 Google Logout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM