上篇文章給大家介紹了asp.net5中用戶認(rèn)證與授權(quán)(1),基礎(chǔ)建立好了,緊接著就要?jiǎng)?chuàng)建對(duì)基礎(chǔ)類(lèi)進(jìn)行操作的類(lèi),也就是實(shí)現(xiàn)基礎(chǔ)類(lèi)的增刪改查當(dāng)然,為了使用asp.net5的認(rèn)證機(jī)制,這些都是通過(guò)特定的接口來(lái)實(shí)現(xiàn)的。
比如,對(duì)于角色來(lái)說(shuō),角色管理要實(shí)現(xiàn)的接口如下:
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);
}
其實(shí),也沒(méi)什么復(fù)雜,一個(gè)是獲得所有預(yù)定義角色的列表,另一個(gè)是關(guān)于角色的增刪改查而已,代碼如下:
public class HDRoleStoreTRole> : IQueryableRoleStoreTRole>
where TRole : HDRole, new()
{
/// summary>
/// 存儲(chǔ)所有預(yù)定義的角色
/// /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行,我們的方法里直接寫(xiě)死了角色列表,如果相結(jié)合具體的項(xiàng)目的話,我相信葉良辰有一百種方法從各種數(shù)據(jù)庫(kù)、配置文件等取得角色列表,而其他程序代碼卻基本不用更改。
當(dāng)然,asp.net5自帶的默認(rèn)實(shí)現(xiàn)實(shí)現(xiàn)了很多其他接口,這里為了最簡(jiǎn)單起見(jiàn),只實(shí)現(xiàn)了最基本的。
以上就是給大家介紹的asp.net5中用戶認(rèn)證與授權(quán)(2),希望大家喜歡。后續(xù)還會(huì)持續(xù)更新,請(qǐng)大家持續(xù)關(guān)注本站。
您可能感興趣的文章:- asp.net5中的用戶認(rèn)證與授權(quán)(1)
- 解讀ASP.NET 5 & MVC6系列教程(1):ASP.NET 5簡(jiǎn)介
- 解讀ASP.NET 5 & MVC6系列教程(2):初識(shí)項(xiàng)目
- 解讀ASP.NET 5 & MVC6系列教程(3):項(xiàng)目發(fā)布與部署
- 解讀ASP.NET 5 & MVC6系列教程(4):核心技術(shù)與環(huán)境配置
- 解讀ASP.NET 5 & MVC6系列教程(5):Configuration配置信息管理
- 解讀ASP.NET 5 & MVC6系列教程(6):Middleware詳解
- 解讀ASP.NET 5 & MVC6系列教程(7):依賴注入