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

时间:2023-03-09 03:01:38
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类型。

先看一下修改后的效果:

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

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

2、修改前分析

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

 SELECT * FROM dbo.AspNetUsers
SELECT * FROM dbo.AspNetRoles
SELECT * FROM dbo.AspNetUserRoles
SELECT * FROM dbo.AspNetUserLogins

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

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

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

 namespace Microsoft.AspNet.Identity.EntityFramework
{
using Microsoft.AspNet.Identity;
using System; public class IdentityUser : IdentityUser<string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>, IUser, IUser<string>
{
public IdentityUser()
{
this.Id = Guid.NewGuid().ToString();
} public IdentityUser(string userName) : this()
{
this.UserName = userName;
}
}
}

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

如下所示:

     public class IntRole : IdentityRole<int, IntUserRole>
{
public IntRole()
{ }
public IntRole(string name) : this() { Name = name; }
}
public class IntUserRole : IdentityUserRole<int> { }
public class IntUserClaim : IdentityUserClaim<int> {}
public class IntUserLogin : IdentityUserLogin<int> { } public class IntUserContext : IdentityDbContext<ApplicationUser, IntRole, int, IntUserLogin, IntUserRole, IntUserClaim>
{
public IntUserContext()
: base("DefaultConnection")
{ }
}

然后我们修改Application.cs

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

由于我们对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 = ,
RequireNonLetterOrDigit = false,
RequireDigit = false,
RequireLowercase = false,
RequireUppercase = false,
}; // 配置用户锁定默认值
manager.UserLockoutEnabledByDefault = true;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes();
manager.MaxFailedAccessAttemptsBeforeLockout = ; // 注册双重身份验证提供程序。此应用程序使用手机和电子邮件作为接收用于验证用户的代码的一个步骤
// 你可以编写自己的提供程序并将其插入到此处。
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,并设置子增长.

源码点击这里 下载

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