今天就跟大家聊聊有关如何在asp.net5项目中实现用户认证与授权,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
创新互联于2013年开始,是专业互联网技术服务公司,拥有项目成都做网站、网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元上林做网站,已为上家服务,为上林各地企业和个人服务,联系电话:18982081108ASP.NET 是开源,跨平台,高性能,轻量级的 Web 应用构建框架,常用于通过 HTML、CSS、JavaScript 以及服务器脚本来构建网页和网站。
角色管理要实现的接口如下:
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); }
其实,也没什么复杂,一个是获得所有预定义角色的列表,另一个是关于角色的增删改查而已,代码如下:
public class HDRoleStore: IQueryableRoleStore where TRole : HDRole, new() { /// /// 存储所有预定义的角色 /// 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 = "用户"; _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行,我们的方法里直接写死了角色列表,如果相结合具体的项目的话,我相信叶良辰有一百种方法从各种数据库、配置文件等取得角色列表,而其他程序代码却基本不用更改。
当然,asp.net5自带的默认实现实现了很多其他接口,这里为了最简单起见,只实现了最基本的。
看完上述内容,你们对如何在asp.net5项目中实现用户认证与授权有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。