上篇文章給大家介紹了asp.net5中用戶認證與授權(1),基礎建立好了,緊接著就要創(chuàng)建對基礎類進行操作的類,也就是實現基礎類的增刪改查當然,為了使用asp.net5的認證機制,這些都是通過特定的接口來實現的。
比如,對于角色來說,角色管理要實現的接口如下:
public interface IQueryableRoleStoreTRole> : IRoleStoreTRole>, IDisposable where TRole : class
{
IQueryableTRole> Roles { get; }
}
public interface IRoleStoreTRole> : IDisposable where TRole : class
{
TaskIdentityResult> CreateAsync(TRole role, CancellationToken cancellationToken);
TaskIdentityResult> DeleteAsync(TRole role, CancellationToken cancellationToken);
TaskTRole> FindByIdAsync(string roleId, CancellationToken cancellationToken);
TaskTRole> FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken);
Taskstring> GetNormalizedRoleNameAsync(TRole role, CancellationToken cancellationToken);
Taskstring> GetRoleIdAsync(TRole role, CancellationToken cancellationToken);
Taskstring> GetRoleNameAsync(TRole role, CancellationToken cancellationToken);
Task SetNormalizedRoleNameAsync(TRole role, string normalizedName, CancellationToken cancellationToken);
Task SetRoleNameAsync(TRole role, string roleName, CancellationToken cancellationToken);
TaskIdentityResult> UpdateAsync(TRole role, CancellationToken cancellationToken);
}
其實,也沒什么復雜,一個是獲得所有預定義角色的列表,另一個是關于角色的增刪改查而已,代碼如下:
public class HDRoleStoreTRole> : IQueryableRoleStoreTRole>
where TRole : HDRole, new()
{
/// summary>
/// 存儲所有預定義的角色
/// /summary>
private readonly Dictionarystring, TRole> _roles = new Dictionarystring, TRole>();
/// summary>
/// 所有角色
/// /summary>
public IQueryableTRole> 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 TaskIdentityResult> CreateAsync(TRole role, CancellationToken cancellationToken)
{
_roles[role.Id] = role;
return Task.FromResult(IdentityResult.Success);
}
public TaskIdentityResult> 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 TaskTRole> FindByIdAsync(string roleId, CancellationToken cancellationToken)
{
if (_roles.ContainsKey(roleId))
{
return Task.FromResult(_roles[roleId]);
}
return Task.FromResultTRole>(null);
}
public TaskTRole> FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken)
{
return
Task.FromResult(
Roles.SingleOrDefault(r => String.Equals(r.Name, normalizedRoleName, StringComparison.OrdinalIgnoreCase)));
}
public Taskstring> GetNormalizedRoleNameAsync(TRole role, CancellationToken cancellationToken)
{
return Task.FromResult(role.Name);
}
public Taskstring> GetRoleIdAsync(TRole role, CancellationToken cancellationToken)
{
return Task.FromResult(role.Id);
}
public Taskstring> 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 TaskIdentityResult> UpdateAsync(TRole role, CancellationToken cancellationToken)
{
_roles[role.Id] = role;
return Task.FromResult(IdentityResult.Success);
}
}
可以看到,在第12行,我們的方法里直接寫死了角色列表,如果相結合具體的項目的話,我相信葉良辰有一百種方法從各種數據庫、配置文件等取得角色列表,而其他程序代碼卻基本不用更改。
當然,asp.net5自帶的默認實現實現了很多其他接口,這里為了最簡單起見,只實現了最基本的。
以上就是給大家介紹的asp.net5中用戶認證與授權(2),希望大家喜歡。后續(xù)還會持續(xù)更新,請大家持續(xù)關注本站。
您可能感興趣的文章:- asp.net5中的用戶認證與授權(1)
- 解讀ASP.NET 5 & MVC6系列教程(1):ASP.NET 5簡介
- 解讀ASP.NET 5 & MVC6系列教程(2):初識項目
- 解讀ASP.NET 5 & MVC6系列教程(3):項目發(fā)布與部署
- 解讀ASP.NET 5 & MVC6系列教程(4):核心技術與環(huán)境配置
- 解讀ASP.NET 5 & MVC6系列教程(5):Configuration配置信息管理
- 解讀ASP.NET 5 & MVC6系列教程(6):Middleware詳解
- 解讀ASP.NET 5 & MVC6系列教程(7):依賴注入