首页 技术 正文
技术 2022年11月12日
0 收藏 897 点赞 3,867 浏览 4006 个字

目录

  • 开发任务
  • 代码实现

开发任务

  • DotNetNB.Security.Core:定义 core,models,Istore;实现 default memory store
  • DotNetNB.WebApplication:创建 ResourceController 和 PermissionController 进行验证

代码实现

  • ResourceController
  • PermissionController

ResourceController

创建 ResourceController,通过 ResourceManager 获取所有 Resource

using DotNetNB.Security.Core;
using Microsoft.AspNetCore.Mvc;namespace DotNetNB.WebApplication.Controllers
{
[ApiController]
[Route("[controller]")]
public class ResourceController : ControllerBase
{
private readonly IResourceManager _resourceManager; public ResourceController(IResourceManager resourceManager)
{
_resourceManager = resourceManager;
} [HttpGet]
[Route("")]
public async Task<IActionResult> GetAll()
{
return Ok(await _resourceManager.GetAllAsync());
}
}
}

在 Program 中先将 AddEntityAccessControl 进行注释

builder.Services.AddSecurity(options =>
{
options.AddActionAccessControl();
//.AddEntityAccessControl();
});

在 ServiceCollectionExtensions 的扩展方法 AddSecurity 中创建 option,并调用,同时注入 Store 和 Manager

using DotNetNB.Security.Core.Store;
using Microsoft.Extensions.DependencyInjection;namespace DotNetNB.Security.Core.Extensions
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddSecurity(this IServiceCollection services, Action<SecurityOption>? configure)
{
var option = new SecurityOption { Services = services };
configure?.Invoke(option); services.AddSingleton<IResourceStore, DefaultResourceStore>()
.AddSingleton<IPermissionStore, DefaultPermissionStore>()
.AddScoped<IResourceManager, ResourceManager>()
.AddScoped<IPermissionManager, PermissionManager>()
.AddHostedService<ResourceProviderHostedService>();
return services;
}
}
}

在 ResourceProviderHostedService 的 StartAsync 方法中将 host 启动时的所有 action 注入进来

using DotNetNB.Security.Core.Models;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;namespace DotNetNB.Security.Core
{
public class ResourceProviderHostedService : IHostedService
{
private readonly IServiceProvider _serviceProvider; public ResourceProviderHostedService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
} public async Task StartAsync(CancellationToken cancellationToken)
{
using var scope = _serviceProvider.CreateScope();
var providers = scope.ServiceProvider.GetServices<IResourceProvider>();
var resourceManager = scope.ServiceProvider.GetService<IResourceManager>(); var resources = new List<Resource>();
foreach (var provider in providers)
{
resources.AddRange(await provider.ExecuteAsync());
} await resourceManager.CreateAsync(resources);
} public async Task StopAsync(CancellationToken cancellationToken)
{
}
}
}

设置 DotNetNB.WebApplication 为启动项,启动项目,可以通过接口看到 action 相关信息

图片001

PermissionController

创建 PermissionController,通过 PermissionManager 获取所有 Permission

using DotNetNB.Security.Core;
using Microsoft.AspNetCore.Mvc;namespace DotNetNB.WebApplication.Controllers
{
[ApiController]
[Route("[controller]")]
public class PermissionController : ControllerBase
{
private readonly IPermissionManager _permissionManager; public PermissionController(IPermissionManager permissionManager)
{
_permissionManager = permissionManager;
} [HttpGet]
public async Task<IActionResult> GetAll()
{
return Ok(await _permissionManager.GetAllAsync());
}
}
}

创建 dto 对象 CreatePermissionRequest

namespace DotNetNB.WebApplication.ViewModels
{
public class CreatePermissionRequest
{
public string Key { get; set; } public string DisplayName { get; set; } public string Description { get; set; } public IEnumerable<string> resources { get; set; }
}
}

在 PermissionController 中添加创建 Permission 的接口

[HttpPost]
public async Task<IActionResult> Create([FromBody] CreatePermissionRequest request)
{
await _permissionManager.CreateAsync(request.Key, request.DisplayName, request.Description, request.resources);
return Ok();
}

在 Program 中将我们的 Permission 模块添加到 Identity 模块上,相当于一个桥接

builder.Services.AddIdentity<IdentityUser<string>, IdentityRole<string>>()
.WithPermissions<IdentityUser<string>, IdentityRole<string>>();

GitHub源码链接:

https://github.com/MingsonZheng/dotnetnb.security

课程链接

https://appsqsyiqlk5791.h5.xiaoeknow.com/v1/course/video/v_5f39bdb8e4b01187873136cf?type=2

本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。

欢迎转载、使用、重新发布,但务必保留文章署名 郑子铭 (包含链接: http://www.cnblogs.com/MingsonZheng/ ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。

如有任何疑问,请与我联系 (MingsonZheng@outlook.com) 。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,156
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,624
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,468
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,240
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,877
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,043