今天就跟大家聊聊有關(guān)如何在asp.net5項(xiàng)目中實(shí)現(xiàn)用戶(hù)認(rèn)證與授權(quán),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)于2013年開(kāi)始,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元上林做網(wǎng)站,已為上家服務(wù),為上林各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108ASP.NET 是開(kāi)源,跨平臺(tái),高性能,輕量級(jí)的 Web 應(yīng)用構(gòu)建框架,常用于通過(guò) HTML、CSS、JavaScript 以及服務(wù)器腳本來(lái)構(gòu)建網(wǎng)頁(yè)和網(wǎng)站。
角色管理要實(shí)現(xiàn)的接口如下:
public interface IQueryableRoleStore: IRoleStore , IDisposable where TRole : class { IQueryable Roles { get; } } public interface IRoleStore : IDisposable where TRole : class { Task CreateAsync(TRole role, CancellationToken cancellationToken); Task DeleteAsync(TRole role, CancellationToken cancellationToken); Task FindByIdAsync(string roleId, CancellationToken cancellationToken); Task FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken); Task GetNormalizedRoleNameAsync(TRole role, CancellationToken cancellationToken); Task GetRoleIdAsync(TRole role, CancellationToken cancellationToken); Task GetRoleNameAsync(TRole role, CancellationToken cancellationToken); Task SetNormalizedRoleNameAsync(TRole role, string normalizedName, CancellationToken cancellationToken); Task SetRoleNameAsync(TRole role, string roleName, CancellationToken cancellationToken); Task UpdateAsync(TRole role, CancellationToken cancellationToken); }
其實(shí),也沒(méi)什么復(fù)雜,一個(gè)是獲得所有預(yù)定義角色的列表,另一個(gè)是關(guān)于角色的增刪改查而已,代碼如下:
public class HDRoleStore: IQueryableRoleStore where TRole : HDRole, new() { /// /// 存儲(chǔ)所有預(yù)定義的角色 /// private readonly Dictionary_roles = new Dictionary (); /// /// 所有角色 /// public IQueryableRoles { 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 = "用戶(hù)"; _roles.Add(role.Id, role); role = new TRole(); role.Id = "power"; role.Name = "大蝦"; _roles.Add(role.Id, role); } return _roles.Values.AsQueryable(); } } public Task CreateAsync(TRole role, CancellationToken cancellationToken) { _roles[role.Id] = role; return Task.FromResult(IdentityResult.Success); } public Task 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 FindByIdAsync(string roleId, CancellationToken cancellationToken) { if (_roles.ContainsKey(roleId)) { return Task.FromResult(_roles[roleId]); } return Task.FromResult (null); } public Task FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken) { return Task.FromResult( Roles.SingleOrDefault(r => String.Equals(r.Name, normalizedRoleName, StringComparison.OrdinalIgnoreCase))); } public Task GetNormalizedRoleNameAsync(TRole role, CancellationToken cancellationToken) { return Task.FromResult(role.Name); } public Task GetRoleIdAsync(TRole role, CancellationToken cancellationToken) { return Task.FromResult(role.Id); } public Task 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 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)了最基本的。
看完上述內(nèi)容,你們對(duì)如何在asp.net5項(xiàng)目中實(shí)現(xiàn)用戶(hù)認(rèn)證與授權(quán)有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。