上篇文章给大家介绍了asp.net5中用户认证与授权(1),基础建立好了,紧接着就要创建对基础类进行操作的类,也就是实现基础类的增删改查当然,为了使用asp.net5的认证机制,这些都是通过特定的接口来实现的。
比如,对于角色来说,角色管理要实现的接口如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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);
}
|
其实,也没什么复杂,一个是获得所有预定义角色的列表,另一个是关于角色的增删改查而已,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
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),希望大家喜欢。后续还会持续更新,请大家持续关注本站。