Asp.net Identity 系列之 怎样修改Microsoft.AspNet.Identity.EntityFramework.IdentityUser 的 Id 字段的数据类型

    这篇博客我们来学习如何将AspNetUsers 表的Id 字段 的类型由nvarchar(128) 改为Int 并且子增长

   

1、为什么要修改

  如果你运行过 Asp.net mvc 示例项目,你好会发现 AspNetUsers 表的Id是Nvarchar(128) 类型,值为GUID,不可否认使用GUID来做主键进行主外键关联会增加数据安全性(个人看法),但是非常不利于查询,可读性不够,因此我们尝试着去改为Int类型。

 先看一下修改后的效果:

 2、修改前分析

   查看数据库结构我们知道要修改的表有这样四张表 ,他们涉及到UserId,RoleId.具体结构这里不做分析。

1 SELECT * FROM dbo.AspNetUsers
2 SELECT * FROM dbo.AspNetRoles
3 SELECT * FROM dbo.AspNetUserRoles
4 SELECT * FROM dbo.AspNetUserLogins

3、如何修改代码实现功能

如果你认真研究Asp.Net Identity 你会发现其扩展性非常好.

我们新建一个Mvc4,5项目,打开Models-->IdentityModels.cs .查看到ApplicationUser class(和用户相关的类)继承了IdentityUser.cs (位于Microsoft.Asp.Net.Identity.EntityFramework.dll) 反编译后源码如下:

 1 namespace Microsoft.AspNet.Identity.EntityFramework
 2 {
 3     using Microsoft.AspNet.Identity;
 4     using System;
 5     
 6     public class IdentityUser : IdentityUser<string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>, IUser, IUser<string>
 7     {
 8         public IdentityUser()
 9         {
10             this.Id = Guid.NewGuid().ToString();
11         }
12         
13         public IdentityUser(string userName) : this()
14         {
15             this.UserName = userName;
16         }
17     }
18 }

 观察 IdentityUser.cs 它有继承了IdentityUser<string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>, IUser, IUser<string>这个泛型,所以我们也需要有对应的Int类型的 IdentityUserLogin, IdentityUserRole, IdentityUserClaim泛型类。

如下所示:

 1     public class IntRole : IdentityRole<int, IntUserRole>
 2     {
 3         public IntRole()
 4         {
 5 
 6         }
 7         public IntRole(string name) : this() { Name = name; }
 8     }
 9     public class IntUserRole : IdentityUserRole<int> { }
10     public class IntUserClaim : IdentityUserClaim<int> {}
11     public class IntUserLogin : IdentityUserLogin<int> { }
12 
13     public class IntUserContext : IdentityDbContext<ApplicationUser, IntRole, int, IntUserLogin, IntUserRole, IntUserClaim>
14     {
15         public IntUserContext()
16             : base("DefaultConnection")
17         {
18 
19         }
20     }

 然后我们修改Application.cs

 1     public class ApplicationUser : IdentityUser<int, IntUserLogin, IntUserRole, IntUserClaim>
 2     {
 3         public ApplicationUser() { }
 4         public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, int> manager)
 5         {
 6             // 请注意,authenticationType 必须与 CookieAuthenticationOptions.AuthenticationType 中定义的相应项匹配
 7             var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
 8             // 在此处添加自定义用户声明
 9             return userIdentity;
10         }
11         public ApplicationUser(string name) : this() { UserName = name; }
12     }

由于我们对Application.cs做了修改,还需要查看Identityfig.cs并作相应的修改。修改后的代码如下:

 public class ApplicationUserManager : UserManager<ApplicationUser, int>
    {
        public ApplicationUserManager(IUserStore<ApplicationUser, int> store)
            : base(store)
        {

        }

        public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
        {
            var manager = new ApplicationUserManager(new IntUserStore(context.Get<ApplicationDbContext>()));

// 配置用户名的验证逻辑 manager.UserValidator = new UserValidator<ApplicationUser, int>(manager) { AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true, }; // 配置密码的验证逻辑 manager.PasswordValidator = new PasswordValidator { RequiredLength = 6, RequireNonLetterOrDigit = false, RequireDigit = false, RequireLowercase = false, RequireUppercase = false, }; // 配置用户锁定默认值 manager.UserLockoutEnabledByDefault = true; manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5); manager.MaxFailedAccessAttemptsBeforeLockout = 5; // 注册双重身份验证提供程序。此应用程序使用手机和电子邮件作为接收用于验证用户的代码的一个步骤 // 你可以编写自己的提供程序并将其插入到此处。 manager.RegisterTwoFactorProvider("电话代码", new PhoneNumberTokenProvider<ApplicationUser, int> { MessageFormat = "你的安全代码是 {0}" }); manager.RegisterTwoFactorProvider("电子邮件代码", new EmailTokenProvider<ApplicationUser, int> { Subject = "安全代码", BodyFormat = "你的安全代码是 {0}" }); manager.EmailService = new EmailService(); manager.SmsService = new SmsService(); var dataProtectionProvider = options.DataProtectionProvider; if (dataProtectionProvider != null) { manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser, int>(dataProtectionProvider.Create("ASP.NET Identity")); } return manager; } }

主要对以上红色部分做了修改.主要代码就在这里了,如果Rebuild Solition 会出现很多错误,不要惊慌,慢慢找找原因来修改

 可能需要修改的部分

  由于将string--->int的数据转换,慢慢寻找,会发现有很多,ManagerController.cs比较多等等。

 如果想要查看效果还需要将数据表涉及到UserId,RoleId有原来的Nvarchar(128) 改为Int,并设置子增长.

源码点击这里 下载

每一天,为明天
原文地址:https://www.cnblogs.com/wolferfeng/p/4101303.html