国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

.net core2.0下使用Identity改用dapper存儲數據(實例講解)

2024-07-10 12:48:55
字體:
來源:轉載
供稿:網友

前言、

已經好多天沒寫博客了,鑒于空閑無聊之時又興起想寫寫博客,也當是給自己做個筆記。過了這么些天,我的文筆還是依然那么爛就請多多諒解了。今天主要是分享一下在使用.net core2.0下的實際遇到的情況。在使用webapi時用了identity做用戶驗證。官方文檔是的是用EF存儲數據來使用dapper,因為個人偏好原因所以不想用EF。于是乎就去折騰。改成使用dapper做數據存儲。于是就有了以下的經驗。

一、使用Identity服務

先找到Startup.cs 這個類文件 找到 ConfigureServices 方法

services.AddIdentity<ApplicationUser, ApplicationRole>().AddDefaultTokenProviders();//添加Identityservices.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);  } }}            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿鲁科尔沁旗| 泊头市| 封丘县| 错那县| 苗栗市| 东光县| 特克斯县| 宜川县| 赣榆县| 罗平县| 山丹县| 丰县| 沅江市| 大石桥市| 武冈市| 沙湾县| 公主岭市| 蓬莱市| 广元市| 安仁县| 昌黎县| 普安县| 华容县| 阳泉市| 泸水县| 南陵县| 城固县| 锦屏县| 宜州市| 子长县| 本溪| 青州市| 旬邑县| 商都县| 县级市| 山丹县| 济阳县| 新疆| 东兴市| 嘉鱼县| 沙坪坝区|