您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關如何在asp.net5項目中實現用戶認證與授權,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
ASP.NET 是開源,跨平臺,高性能,輕量級的 Web 應用構建框架,常用于通過 HTML、CSS、JavaScript 以及服務器腳本來構建網頁和網站。
角色管理要實現的接口如下:
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項目中實現用戶認證與授權有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。