已经好多天没写博客了,鉴于空闲无聊之时又兴起想写写博客,也当是给自己做个笔记。过了这么些天,我的文笔还是依然那么烂就请多多谅解了。今天主要是分享一下在使用.net core2.0下的实际遇到的情况。在使用webapi时用了identity做用户验证。官方文档是的是用EF存储数据来使用dapper,因为个人偏好原因所以不想用EF。于是乎就去折腾。改成使用dapper做数据存储。于是就有了以下的经验。
一、使用Identity服务
先找到Startup.cs 这个类文件 找到 ConfigureServices 方法
services.AddIdentity<ApplicationUser, ApplicationRole>().AddDefaultTokenProviders();//添加Identity services.AddTransient<IUserStore<ApplicationUser>, CustomUserStore>(); services.AddTransient<IRoleStore<ApplicationRole>, CustomRoleStore>(); string connectionString = Configuration.GetConnectionString("SqlConnectionStr"); services.AddTransient<SqlConnection>(e => new SqlConnection(connectionString)); services.AddTransient<DapperUsersTable>();
然后在 Configure 方法 的 app.UseMvc() 前加入下列代码,net core 1.0的时候是app.UseIdentity() 现在已经弃用改为以下方法。
//使用验证 app.UseAuthentication();
这里的 ApplicationUser 是自定义的一个用户模型 具体是继承 IdentityUser 继承它的一些属性
public class ApplicationUser :IdentityUser { public string AuthenticationType { get; set; } public bool IsAuthenticated { get; set; } public string Name { get; set; } }
这里的 CustomUserStore 是自定义提供用户的所有数据操作的方法的类它需要继承三个接口:IUserStore,IUserPasswordStore,IUserEmailStore
IUserStore<TUser>接口是在用户存储中必须实现的唯一接口。 它定义了用于创建、 更新、 删除和检索用户的方法。
IUserPasswordStore<TUser>接口定义实现以保持经过哈希处理的密码的方法。 它包含用于获取和设置工作经过哈希处理的密码,以及用于指示用户是否已设置密码的方法的方法。
IUserEmailStore<TUser>接口定义实现以存储用户电子邮件地址的方法。 它包含用于获取和设置的电子邮件地址和是否确认电子邮件的方法。
这里跟.net core 1.0的实现接口方式有点不同。需要多实现 IUserEmailStore 才能不报错
具体代码如下。以供大家参考。
CustomUserStore
using Microsoft.AspNetCore.Identity; using System; using System.Threading.Tasks; using System.Threading; namespace YepMarsCRM.Web.CustomProvider { /// <summary> /// This store is only partially implemented. It supports user creation and find methods. /// </summary> public class CustomUserStore : IUserStore<ApplicationUser>, IUserPasswordStore<ApplicationUser>, IUserEmailStore<ApplicationUser> { private readonly DapperUsersTable _usersTable; public CustomUserStore(DapperUsersTable usersTable) { _usersTable = usersTable; } #region createuser public async Task<IdentityResult> CreateAsync(ApplicationUser user, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); if (user == null) throw new ArgumentNullException(nameof(user)); return await _usersTable.CreateAsync(user); } #endregion public async Task<IdentityResult> DeleteAsync(ApplicationUser user, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); if (user == null) throw new ArgumentNullException(nameof(user)); return await _usersTable.DeleteAsync(user); } public void Dispose() { } public Task<ApplicationUser> FindByEmailAsync(string normalizedEmail, CancellationToken cancellationToken) { throw new NotImplementedException(); } public async Task<ApplicationUser> FindByIdAsync(string userId, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); if (userId == null) throw new ArgumentNullException(nameof(userId)); Guid idGuid; if (!Guid.TryParse(userId, out idGuid)) { throw new ArgumentException("Not a valid Guid id", nameof(userId)); } return await _usersTable.FindByIdAsync(idGuid); } public async Task<ApplicationUser> FindByNameAsync(string userName, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); if (userName == null) throw new ArgumentNullException(nameof(userName)); return await _usersTable.FindByNameAsync(userName); } public Task<string> GetEmailAsync(ApplicationUser user, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (user == null) throw new ArgumentNullException(nameof(user)); return Task.FromResult(user.Email); } public Task<bool> GetEmailConfirmedAsync(ApplicationUser user, CancellationToken cancellationToken) { throw new NotImplementedException(); } public Task<string> GetNormalizedEmailAsync(ApplicationUser user, CancellationToken cancellationToken) { throw new NotImplementedException(); } public Task<string> GetNormalizedUserNameAsync(ApplicationUser user, CancellationToken cancellationToken) { throw new NotImplementedException(); } public Task<string> GetPasswordHashAsync(ApplicationUser user, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (user == null) throw new ArgumentNullException(nameof(user)); return Task.FromResult(user.PasswordHash); } public Task<string> GetUserIdAsync(ApplicationUser user, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (user == null) throw new ArgumentNullException(nameof(user)); return Task.FromResult(user.Id.ToString()); } public Task<string> GetUserNameAsync(ApplicationUser user, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (user == null) throw new ArgumentNullException(nameof(user)); return Task.FromResult(user.UserName); } public Task<bool> HasPasswordAsync(ApplicationUser user, CancellationToken cancellationToken) { throw new NotImplementedException(); } public Task SetEmailAsync(ApplicationUser user, string email, CancellationToken cancellationToken) { throw new NotImplementedException(); } public Task SetEmailConfirmedAsync(ApplicationUser user, bool confirmed, CancellationToken cancellationToken) { throw new NotImplementedException(); } public Task SetNormalizedEmailAsync(ApplicationUser user, string normalizedEmail, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (user == null) throw new ArgumentNullException(nameof(user)); if (normalizedEmail == null) throw new ArgumentNullException(nameof(normalizedEmail)); user.NormalizedEmail = normalizedEmail; return Task.FromResult<object>(null); } public Task SetNormalizedUserNameAsync(ApplicationUser user, string normalizedName, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (user == null) throw new ArgumentNullException(nameof(user)); if (normalizedName == null) throw new ArgumentNullException(nameof(normalizedName)); user.NormalizedUserName = normalizedName; return Task.FromResult<object>(null); } public Task SetPasswordHashAsync(ApplicationUser user, string passwordHash, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (user == null) throw new ArgumentNullException(nameof(user)); if (passwordHash == null) throw new ArgumentNullException(nameof(passwordHash)); user.PasswordHash = passwordHash; return Task.FromResult<object>(null); } public Task SetUserNameAsync(ApplicationUser user, string userName, CancellationToken cancellationToken) { throw new NotImplementedException(); } public Task<IdentityResult> UpdateAsync(ApplicationUser user, CancellationToken cancellationToken) { return _usersTable.UpdateAsync(user); } } }
二、使用使用dapper做数据存储
接着就是使用dapper做数据存储。该类的方法都是通过 CustomUserStore 调用去操作数据库的。具体代码如下。根据实际的用户表去操作dapper即可。
DapperUsersTable