asp.net5中使用者認證與授權(2)_實用技巧

來源:互聯網
上載者:User

上篇文章給大家介紹了asp.net5中使用者認證與授權(1),基礎建立好了,緊接著就要建立對基礎類進行操作的類,也就是實現基礎類的增刪改查當然,為了使用asp.net5的認證機制,這些都是通過特定的介面來實現的。

比如,對於角色來說,角色管理要實現的介面如下:

   public interface IQueryableRoleStore<TRole> : IRoleStore<TRole>, IDisposable where TRole : class   {     IQueryable<TRole> Roles { get; }   }   public interface IRoleStore<TRole> : IDisposable where TRole : class   {     Task<IdentityResult> CreateAsync(TRole role, CancellationToken cancellationToken);     Task<IdentityResult> DeleteAsync(TRole role, CancellationToken cancellationToken);     Task<TRole> FindByIdAsync(string roleId, CancellationToken cancellationToken);     Task<TRole> FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken);     Task<string> GetNormalizedRoleNameAsync(TRole role, CancellationToken cancellationToken);     Task<string> GetRoleIdAsync(TRole role, CancellationToken cancellationToken);     Task<string> GetRoleNameAsync(TRole role, CancellationToken cancellationToken);     Task SetNormalizedRoleNameAsync(TRole role, string normalizedName, CancellationToken cancellationToken);     Task SetRoleNameAsync(TRole role, string roleName, CancellationToken cancellationToken);    Task<IdentityResult> UpdateAsync(TRole role, CancellationToken cancellationToken);   }

其實,也沒什麼複雜,一個是獲得所有預定義角色的列表,另一個是關於角色的增刪改查而已,代碼如下:

 public class HDRoleStore<TRole> : IQueryableRoleStore<TRole>      where TRole : HDRole, new()    {      /// <summary>      /// 儲存所有預定義的角色      /// </summary>      private readonly Dictionary<string, TRole> _roles = new Dictionary<string, TRole>();      /// <summary>     /// 所有角色     /// </summary>     public IQueryable<TRole> Roles     {       get       {         if (_roles.Count == )         {           TRole role = new TRole();           role.Id = "admin";           role.Name = "管理員";           _roles.Add(role.Id, role);           role = new TRole();           role.Id = "user";           role.Name = "使用者";           _roles.Add(role.Id, role);           role = new TRole();           role.Id = "power";           role.Name = "大蝦";           _roles.Add(role.Id, role);         }         return _roles.Values.AsQueryable();       }     }     public Task<IdentityResult> CreateAsync(TRole role, CancellationToken cancellationToken)     {       _roles[role.Id] = role;       return Task.FromResult(IdentityResult.Success);     }     public Task<IdentityResult> DeleteAsync(TRole role, CancellationToken cancellationToken)     {       if (role == null || !_roles.ContainsKey(role.Id))       {         throw new InvalidOperationException("Unknown role");       }       _roles.Remove(role.Id);       return Task.FromResult(IdentityResult.Success);     }     public void Dispose()     {     }     public Task<TRole> FindByIdAsync(string roleId, CancellationToken cancellationToken)     {       if (_roles.ContainsKey(roleId))       {         return Task.FromResult(_roles[roleId]);       }       return Task.FromResult<TRole>(null);     }     public Task<TRole> FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken)     {       return          Task.FromResult(            Roles.SingleOrDefault(r => String.Equals(r.Name, normalizedRoleName, StringComparison.OrdinalIgnoreCase)));     }     public Task<string> GetNormalizedRoleNameAsync(TRole role, CancellationToken cancellationToken)     {       return Task.FromResult(role.Name);     }     public Task<string> GetRoleIdAsync(TRole role, CancellationToken cancellationToken)     {       return Task.FromResult(role.Id);     }     public Task<string> GetRoleNameAsync(TRole role, CancellationToken cancellationToken)     {       return Task.FromResult(role.Name);     }     public Task SetNormalizedRoleNameAsync(TRole role, string normalizedName, CancellationToken cancellationToken)     {       role.Name = normalizedName;       return Task.FromResult();     }     public Task SetRoleNameAsync(TRole role, string roleName, CancellationToken cancellationToken)     {       role.Name = roleName;       return Task.FromResult();     }     public Task<IdentityResult> UpdateAsync(TRole role, CancellationToken cancellationToken)     {       _roles[role.Id] = role;       return Task.FromResult(IdentityResult.Success);     }   }

可以看到,在第12行,我們的方法裡直接寫死了角色列表,如果相結合具體的項目的話,我相信葉良辰有一百種方法從各種資料庫、設定檔等取得角色列表,而其他程式碼卻基本不用更改。

當然,asp.net5內建的預設實現實現了很多其他介面,這裡為了最簡單起見,只實現了最基本的。

以上就是給大家介紹的asp.net5中使用者認證與授權(2),希望大家喜歡。後續還會持續更新,請大家持續關注本站。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.