Moved to https://github.com/WeihanLi/WeihanLi.Web.Extensions/tree/dev/src/WeihanLi.Web.Extensions/AccessControlHelper, please use
WeihanLi.Web.Extensionsinstead
Due to project needs, permission control needs to be controlled in the Asp.net mvc-based Web project framework, so this permission control component is available.
The project is based on .NETStandard, and supports both asp.net mvc (.NET faremwork 4.5 or above) and asp.net core projects (asp.net 2.0 or above), and is based on ASP.NET MVC and ASP.NET Core to access control of Action and permission control of page elements implemented.
Asp.net core supports more. Asp.net core can use TagHelper to control the permission access of elements on the page, and also supports access to static resources through middleware.
Nuget Package https://www.nuget.org/packages/WeihanLi.AspNetMvc.AccessControlHelper/
Install permission control component WeihanLi.AspNetMvc.AccessControlHelper
asp.net:
Install-Package WeihanLi.AspNetMvc.AccessControlHelperasp.net core:
dotnet add package WeihanLi.AspNetMvc.AccessControlHelperImplement your own permission control display policy class
IControlAccessStrategyAction access display policy interface IResourceAccessStrategySample code:
ASP.NET Mvc
ASP.NET Core
ResourceAccessStrategy
ControlAccessStrategy
Register your own display policy when the program starts
Dependency injection based on Autofac implementation can be used to register the display policy in the Ioc Container of autofac, and return an object that can obtain the object from the Ioc Container or implement IServiceProvider interface. Reference: https://github.com/WeihanLi/AccessControlHelper/blob/master/samples/PowerControlDemo/Global.asax.cs#L23
//autofac ContainerBuilder
var builder = new ContainerBuilder ( ) ;
// etc...
// register accesss control
builder . RegisterType < ResourceAccessStrategy > ( ) . As < IResourceAccessStrategy > ( ) ;
builder . RegisterType < ControlAccessStrategy > ( ) . As < IControlAccessStrategy > ( ) ;
var container = builder . Build ( ) ;
// Important
AccessControlHelper . RegisterAccessControlHelper < ActionAccessStrategy , ControlAccessStrategy > ( type => container . Resolve ( type ) ) ; Register the display policy in the Startup file, refer to https://github.com/WeihanLi/AccessControlHelper/blob/master/samples/AccessControlDemo/Startup.cs
// ConfigureServices
services . AddAccessControlHelper < ResourceAccessStrategy , ControlAccessStrategy > ( ) ;
// 自己注册服务,如果只用到资源访问,比如只有 API 可以只注册 IResourceAccessStrategy,反之如果只用到视图上的权限控制可以只注册 IControlAccessStrategy
//services.TryAddScoped<IResourceAccessStrategy, ActionAccessStrategy>();
//services.TryAddSingleton<IControlAccessStrategy, ControlAccessStrategy>();
//services.AddAccessControlHelper();
// 自定义服务生命周期
// services.AddAccessControlHelper<ActionAccessStrategy, ControlAccessStrategy>(ServiceLifetime.Scoped, ServiceLifetime.Singleton);
// asp.net core 【推荐用法】
services . AddAccessControlHelper ( )
. AddResourceAccessStrategy < ResourceAccessStrategy > ( ServiceLifetime . Scoped )
. AddControlAccessStrategy < ControlAccessStrategy > ( )
;
// Configure 中间件,可选,当你需要一个全局的 access control 时使用(会忽略控制器上的 AllowAnonymous)
// app.UseAccessControlHelper(); // use this only when you want to have a global access control especially for static files Control method permissions for Action
AccessControl and NoAccessControl Filter are used to control the access permissions of Action . If NoAccessControl is defined on the Action, you can ignore AccessControl defined by the previous level. In addition, you can set AccessKey corresponding to the Action
Example of usage:
[ NoAccessControl ]
public IActionResult Index ( )
{
return View ( ) ;
}
[ AccessControl ]
public IActionResult About ( )
{
ViewData [ "Message" ] = "Your application description page." ;
return View ( ) ;
}
[ AccessControl ( AccessKey = "Contact" ) ]
public IActionResult Contact ( )
{
ViewData [ "Message" ] = "Your contact page." ;
return View ( ) ;
} In asp.net core, you can also set Policy and use the [AccessControl] method directly.
// [Authorize(AccessControlHelperConstants.PolicyName)]
[ Authorize ( "AccessControl" ) ]
public IActionResult Contact ( )
{
ViewData [ "Message" ] = "Your contact page." ;
return View ( ) ;
}Control the display of page elements
For more convenient use, it is recommended to import the namespace on the page. The specific method is as follows. See Samples for details:
asp.net mvc
Add namespace references
Add the namespace WeihanLi.AspNetMvc.AccessControlHelper to the web.config file in the project's Views directory
< system .web.webPages.razor>
< pages pageBaseType = " System.Web.Mvc.WebViewPage " >
< namespaces >
< add namespace = " System.Web.Mvc " />
< add namespace = " System.Web.Mvc.Ajax " />
< add namespace = " System.Web.Mvc.Html " />
< add namespace = " System.Web.Optimization " />
< add namespace = " System.Web.Routing " />
< add namespace = " PowerControlDemo " />
< add namespace = " WeihanLi.AspNetMvc.AccessControlHelper " /> <!-- add WeihanLi.AspNetMvc.AccessControlHelper -->
</ namespaces >
</ pages >
</ system .web.webPages.razor>Use on the Razor page
SparkContainer usage
@using ( Html . SparkContainer ( "div" , new { @class = "container" , custom - attribute = "abcd" } ) )
{
@Html . Raw ( "1234" )
}
@using ( Html . SparkContainer ( "span" , new { @class = "custom_p111" } , "F7A17FF9-3371-4667-B78E-BD11691CA852" ) )
{
@ : 12344
}If you have permission to access, you will not render to the page. The rendered HTML when you have permission to access is as follows:
< div class =" container " custom-attribute =" abcd " > 1234 </ div >
< span class =" custome_p111 " > 12344 </ span > SparkLink
@Html . SparkLink ( "Learn about me »" , "http://weihanli.xyz" , new { @class = "btn btn-default" } )The rendered html when access is authorized is as follows:
< a class =" btn btn-default " href =" http://weihanli.xyz " > Learn about me » </ a > SparkButton
@Html . SparkButton ( "12234" , new { @class = "btn btn-primary" } )The rendered html when access is authorized is as follows:
< button class =" btn btn-primary " type =" button " > 12234 </ button >asp.net core
HtmlHelper extension
Add namespace references
Refer to the namespace WeihanLi.AspNetMvc.AccessControlHelper in _ViewImports.cshtml in the Views directory
@using AccessControlDemo
@using WeihanLi . AspNetMvc . AccessControlHelper // add WeihanLi.AspNetMvc.AccessControlHelper
@addTagHelper * , Microsoft . AspNetCore . Mvc . TagHelpersUse it on the Razor page, use it the same way as above
TagHelper
Add TagHelper Reference
Reference WeihanLi.AspNetMvc.AccessControlHelper TagHelper in _ViewImports.cshtml in the Views directory
@using AccessControlDemo
@addTagHelper * , Microsoft . AspNetCore . Mvc . TagHelpers
@addTagHelper * , WeihanLi . AspNetMvc . AccessControlHelper // add WeihanLi.AspNetMvc.AccessControlHelper TagHelperUse on the Razor page
Add asp-access to elements that require permission control. If you need to configure access-key to configure it through asp-accesss-key , example: <ul class="list-group" asp-access asp-access-key="12334">...</ul>
In this way, when you have permission, the content of this ul will be output. If you do not have permission, it will not be output. For security reasons, if you configure asp-access-key , asp-access-key will be removed and will not be output to the browser.
If you have any problems using it, please feel free to contact me.
Contact me: [email protected]