程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net5中用戶認證與授權(2)

asp.net5中用戶認證與授權(2)

編輯:ASP.NET基礎

上篇文章給大家介紹了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),希望大家喜歡。後續還會持續更新,請大家持續關注本站。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved