基础类建立好后,下一步就要创建对基础类进行操作的类了,也就是实现基础类的增删改查(听起来不太高大上),当然,为了使用asp.net5的认证机制,这些都是通过特定的接口来实现的。
比如,对于角色来说,角色管理要实现的接口如下:
1 public interface IQueryableRoleStore<TRole> : IRoleStore<TRole>, IDisposable where TRole : class 2 { 3 4 IQueryable<TRole> Roles { get; } 5 }
1 public interface IRoleStore<TRole> : IDisposable where TRole : class 2 { 3 4 Task<IdentityResult> CreateAsync(TRole role, CancellationToken cancellationToken); 5 Task<IdentityResult> DeleteAsync(TRole role, CancellationToken cancellationToken); 6 Task<TRole> FindByIdAsync(string roleId, CancellationToken cancellationToken); 7 Task<TRole> FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken); 8 Task<string> GetNormalizedRoleNameAsync(TRole role, CancellationToken cancellationToken); 9 Task<string> GetRoleIdAsync(TRole role, CancellationToken cancellationToken); 10 Task<string> GetRoleNameAsync(TRole role, CancellationToken cancellationToken); 11 Task SetNormalizedRoleNameAsync(TRole role, string normalizedName, CancellationToken cancellationToken); 12 Task SetRoleNameAsync(TRole role, string roleName, CancellationToken cancellationToken); 13 Task<IdentityResult> UpdateAsync(TRole role, CancellationToken cancellationToken); 14 }
其实,也没什么复杂,一个是获得所有预定义角色的列表,另一个是关于角色的增删改查而已,代码如下:
1 public class HDRoleStore<TRole> : IQueryableRoleStore<TRole> 2 where TRole : HDRole, new() 3 { 4 /// <summary> 5 /// 存储所有预定义的角色 6 /// </summary> 7 private readonly Dictionary<string, TRole> _roles = new Dictionary<string, TRole>(); 8 9 /// <summary> 10 /// 所有角色 11 /// </summary> 12 public IQueryable<TRole> Roles 13 { 14 get 15 { 16 if (_roles.Count == 0) 17 { 18 TRole role = new TRole(); 19 role.Id = "admin"; 20 role.Name = "管理员"; 21 _roles.Add(role.Id, role); 22 23 role = new TRole(); 24 role.Id = "user"; 25 role.Name = "用户"; 26 _roles.Add(role.Id, role); 27 28 role = new TRole(); 29 role.Id = "power"; 30 role.Name = "大虾"; 31 _roles.Add(role.Id, role); 32 } 33 return _roles.Values.AsQueryable(); 34 } 35 } 36 37 public Task<IdentityResult> CreateAsync(TRole role, CancellationToken cancellationToken) 38 { 39 _roles[role.Id] = role; 40 return Task.FromResult(IdentityResult.Success); 41 } 42 43 public Task<IdentityResult> DeleteAsync(TRole role, CancellationToken cancellationToken) 44 { 45 if (role == null || !_roles.ContainsKey(role.Id)) 46 { 47 throw new InvalidOperationException("Unknown role"); 48 } 49 _roles.Remove(role.Id); 50 return Task.FromResult(IdentityResult.Success); 51 } 52 53 public void Dispose() 54 { 55 56 } 57 58 public Task<TRole> FindByIdAsync(string roleId, CancellationToken cancellationToken) 59 { 60 if (_roles.ContainsKey(roleId)) 61 { 62 return Task.FromResult(_roles[roleId]); 63 } 64 return Task.FromResult<TRole>(null); 65 } 66 67 public Task<TRole> FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken) 68 { 69 return 70 Task.FromResult( 71 Roles.SingleOrDefault(r => String.Equals(r.Name, normalizedRoleName, StringComparison.OrdinalIgnoreCase))); 72 } 73 74 public Task<string> GetNormalizedRoleNameAsync(TRole role, CancellationToken cancellationToken) 75 { 76 return Task.FromResult(role.Name); 77 } 78 79 public Task<string> GetRoleIdAsync(TRole role, CancellationToken cancellationToken) 80 { 81 return Task.FromResult(role.Id); 82 } 83 84 public Task<string> GetRoleNameAsync(TRole role, CancellationToken cancellationToken) 85 { 86 return Task.FromResult(role.Name); 87 } 88 89 public Task SetNormalizedRoleNameAsync(TRole role, string normalizedName, CancellationToken cancellationToken) 90 { 91 role.Name = normalizedName; 92 return Task.FromResult(0); 93 } 94 95 public Task SetRoleNameAsync(TRole role, string roleName, CancellationToken cancellationToken) 96 { 97 role.Name = roleName; 98 return Task.FromResult(0); 99 } 100 101 public Task<IdentityResult> UpdateAsync(TRole role, CancellationToken cancellationToken) 102 { 103 _roles[role.Id] = role; 104 return Task.FromResult(IdentityResult.Success); 105 } 106 }
可以看到,在第12行,我们的方法里直接写死了角色列表,如果相结合具体的项目的话,我相信叶良辰有一百种方法从各种数据库、配置文件等取得角色列表,而其他程序代码却基本不用更改。
当然,asp.net5自带的默认实现实现了很多其他接口,这里为了最简单起见,只实现了最基本的。
下一篇: