ASP.NET Core Identity自定义数据库结构和完全使用Dapper而非EntityFramework Core
前言
原本本节内容是不存在的,出于有几个人问到了我:我想使用ASP.NET Core Identity,但是我又不想使用默认生成的数据库表,想自定义一套,我想要使用ASP.NE Core Identity又不想使用EntityFramework Core。真难伺候,哈哈,不过我认为这个问题提出的非常有价值,所以就私下花了点时间看下官网资料,最终经过尝试还是搞出来了,不知道是否满足问过我这个问题的几位童鞋,废话少说,我们直接进入主题吧。
ASP.NET Core Identity自定义数据库表结构
别着急哈,我是那种从头讲到尾的人,博文基本上面向大众,没什么基础的和有经验的都能看明白,也不要嫌弃我啰嗦,好,我说完了,开始,开始,又说了一大堆。大部分情况下对于默认情况下我们都是继承自默认的身份有关的类,如下:
复制代码
/// <summary>
///
/// </summary>
public class CusomIdentityDbContext : IdentityDbContext<CustomIdentityUser, CustomIdentityRole, string>
{
/// <summary>
///
/// </summary>
/// <param name="options"></param>
public CusomIdentityDbContext(DbContextOptions<CusomIdentityDbContext> options)
: base(options)
{ }
}
/// <summary>
///
/// </summary>
public class CustomIdentityUser : IdentityUser { }
/// <summary>
///
/// </summary>
public class CustomIdentityRole : IdentityRole { }
复制代码
然后添加身份中间件,最后开始迁移,如下:
复制代码
services.AddIdentity<CustomIdentityUser, IdentityRole>()
.AddEntityFrameworkStores<CusomIdentityDbContext>()
.AddDefaultTokenProviders();
services.AddDbContextPool<CusomIdentityDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("Default")));
复制代码
以上是默认为我们生成的数据表,我们可以指定用户表主键、可以修改表名、列名等等,以及在此基础上扩展属性都是可以的,但是我们就是不想使用这一套,需要自定义一套表来管理用户身份信息,那么我们该如何做呢?其实呢,官网给了提示,
如下链接:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity-custom-storage-providers?view=aspnetcore-2.2,只是说的不是很明确,然后有些童鞋就不知所措了,就是那么几个Store,自定义实现就好了,来,我们走一个。我们首先自定义用户,比如如下:
复制代码
/// <summary>
///
/// </summary>
public class User
{
/// <summary>
///
/// </summary>
public string Id { get; set; }
/// <summary>
///
/// </summary>
public string UserName { get; set; }
/// <summary>
///
/// </summary>
public string Password { get; set; }
/// <summary>
///
/// </summary>
public string Phone { get; set; }
}
复制代码
我们再来定义上下文,如下:
复制代码
/// <summary>
///
/// </summary>
public class CustomDbContext : DbContext
{
/// <summary>
///
/// </summary>
/// <param name="options"></param>
public CustomDbContext(DbContextOptions<CustomDbContext> options) : base(options)
{
}
/// <summary>
///
/// </summary>
public DbSet<User> Users { get; set; }
}
复制代码
接下来实现IUserStore以及UserPasswordStore接口,接口太多,就全部折叠了
View Code
我们还要用到用户角色表,自定义用户角色
复制代码
/// <summary>
///
/// </summary>
public class CustomUserRole
{
/// <summary>
///
/// </summary>
public string Id { get; set; }
/// <summary>
///
/// </summary>
public string UserId { get; set; }
/// <summary>
///
/// </summary>
public string RoleId { get; set; }
}
复制代码
接下来再来实现用户角色Store,如下:
复制代码
/// <summary>
///
/// </summary>
public class CustomUserRoleStore : IRoleStore<CustomUserRole>
{
/// <summary>
///
/// </summary>
/// <param name="role"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<IdentityResult> CreateAsync(CustomUserRole role, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="role"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<IdentityResult> DeleteAsync(CustomUserRole role, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
public void Dispose()
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<CustomUserRole> FindByIdAsync(string roleId, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="normalizedRoleName"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<CustomUserRole> FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="role"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<string> GetNormalizedRoleNameAsync(CustomUserRole role, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="role"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<string> GetRoleIdAsync(CustomUserRole role, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="role"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<string> GetRoleNameAsync(CustomUserRole role, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="role"></param>
/// <param name="normalizedName"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task SetNormalizedRoleNameAsync(CustomUserRole role, string normalizedName, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="role"></param>
/// <param name="roleName"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task SetRoleNameAsync(CustomUserRole role, string roleName, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="role"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<IdentityResult> UpdateAsync(CustomUserRole role, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
复制代码
简单来说就是根据需要,看看要不要实现如下几个Store罢了
IUserRoleStore
IUserClaimStore
IUserPasswordStore
IUserSecurityStampStore
IUserEmailStore
IPhoneNumberStore
IQueryableUserStore
IUserLoginStore
IUserTwoFactorStore
IUserLockoutStore
然后对于根据选择自定义实现的Store都进行注册,然后进行迁移,如下:
复制代码
services.AddIdentity<CustomUser, CustomUserRole>()
.AddDefaultTokenProviders();
services.AddDbContextPool<CustomDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("Default")));
services.AddTransient<IUserStore<CustomUser>, CustomUserStore>();
复制代码
没什么难题,还是那句话,自定义实现一套,不过是实现内置的Store,其他通过定义的上下文正常去管理用户即可。然后什么登陆、注册之类只需要将对应比如UserManager泛型参数替换成对应比如如上CustomUser即可,这个就不用多讲了。接下来我们再来看第二个问题,如何不使用EntityFramework而是完全使用Dapper。
完全使用Dapper而不使用EntityFramework Core
其实讲解完上述第一个问题,这个就迎刃而解了,我们已经完全实现了自定义一套表,第一个问题操作表是通过上下文,我们只需将上下文更换为Dapper即可,如上我们定义了用户角色表,那我们通过Dapper实现角色表,如下定义角色:
复制代码
/// <summary>
///
/// </summary>
public class CustomRole
{
/// <summary>
///
/// </summary>
public string Id { get; set; }
/// <summary>
///
/// </summary>
public string Name { get; set; }
}
复制代码
复制代码
/// <summary>
///
/// </summary>
public class CustomRoleStore : IRoleStore<CustomRole>
{
private readonly IConfiguration configuration;
private readonly string connectionString;
/// <summary>
///
/// </summary>
/// <param name="configuration"></param>
public CustomRoleStore(IConfiguration configuration)
{
this.configuration = configuration;
connectionString = configuration.GetConnectionString("Default");
}
/// <summary>
///
/// </summary>
/// <param name="role"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<IdentityResult> CreateAsync(CustomRole role, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
using (var connection = new SqlConnection(connectionString))
{
await connection.OpenAsync(cancellationToken);
role.Id = await connection.QuerySingleAsync<string>($@"INSERT INTO [CustomRole]([Id],[Name])
VALUES (@{Guid.NewGuid().ToString()} @{nameof(CustomRole.Name)});
SELECT CAST(SCOPE_IDENTITY() as varchar(36))", role);
}
return IdentityResult.Success;
}
/// <summary>
///
/// </summary>
/// <param name="role"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<IdentityResult> DeleteAsync(CustomRole role, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
public void Dispose()
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<CustomRole> FindByIdAsync(string roleId, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="normalizedRoleName"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<CustomRole> FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="role"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<string> GetNormalizedRoleNameAsync(CustomRole role, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="role"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<string> GetRoleIdAsync(CustomRole role, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="role"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<string> GetRoleNameAsync(CustomRole role, CancellationToken cancellationToken)
{
return Task.FromResult(role.Name);
}
/// <summary>
///
/// </summary>
/// <param name="role"></param>
/// <param name="normalizedName"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task SetNormalizedRoleNameAsync(CustomRole role, string normalizedName, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="role"></param>
/// <param name="roleName"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task SetRoleNameAsync(CustomRole role, string roleName, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="role"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<IdentityResult> UpdateAsync(CustomRole role, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
复制代码
别忘记每自定义实现一个Store,然后进行对应注册
services.AddTransient, CustomRoleStore>();
总结
这里已经提供了完全自定义实现一套表和不使用EntityFramework Core完全使用Dapper的思路,重申一句官网给出了几个Store,只是未明确说明而已,稍微思考并动手验证,其实问题不大。
原文地址https://www.cnblogs.com/CreateMyself/p/11291623.html