WeihanLi.AspNetMvc.AccessControlHelper 1.3.1

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package WeihanLi.AspNetMvc.AccessControlHelper --version 1.3.1
NuGet\Install-Package WeihanLi.AspNetMvc.AccessControlHelper -Version 1.3.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="WeihanLi.AspNetMvc.AccessControlHelper" Version="1.3.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add WeihanLi.AspNetMvc.AccessControlHelper --version 1.3.1
#r "nuget: WeihanLi.AspNetMvc.AccessControlHelper, 1.3.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install WeihanLi.AspNetMvc.AccessControlHelper as a Cake Addin
#addin nuget:?package=WeihanLi.AspNetMvc.AccessControlHelper&version=1.3.1

// Install WeihanLi.AspNetMvc.AccessControlHelper as a Cake Tool
#tool nuget:?package=WeihanLi.AspNetMvc.AccessControlHelper&version=1.3.1
  1. 实现自己的权限控制显示策略类

    • 实现页面元素显示策略接口 IControlAccessStrategy
    • 实现 Action 访问显示策略接口 IActionAccessStrategy

    示例代码:

  2. 程序启动时注册自己的显示策略

    • asp.net mvc

    可基于Autofac实现的依赖注入,在 autofac 的 Ioc Container中注册显示策略,并返回一个可以从Ioc Container中获取对象的委托或者实现 IServiceProvider 接口的对象,参考 Asp.Net 注册示例

    //autofac ContainerBuilder
    var builder = new ContainerBuilder();
    // etc...
    
    // register accesss control
    builder.RegisterType<ActionAccessStrategy>().As<IActionAccessStrategy>();
    builder.RegisterType<ControlAccessStrategy>().As<IControlAccessStrategy>();
    var container = builder.Build();
    // Important
    AccessControlHelper.RegisterAccessControlHelper<ActionAccessStrategy, ControlAccessStrategy>(type => container.Resolve(type));
    
    • asp.net core

    Startup 文件中注册显示策略,参考 Asp.Net Core 注册示例

    // Configure
    app.UseAccessControlHelper();
    
    // ConfigureServices
    services.AddAccessControlHelper<ActionAccessStrategy, ControlAccessStrategy>();
    
  3. 控制 Action 的方法权限

    通过 AccessControlNoAccessControl Filter 来控制 Action 的访问权限,如果Action上定义了 NoAccessControl 可以忽略上级定义的 AccessControl,另外可以设置 Action 对应的 AccessKey

    使用示例:

    [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();
    }
    
  4. 控制页面元素的显示

    为了使用比较方便,建议在页面上导入命名空间,具体方法如下,详见 Samples:

    通过 HtmlHelper 扩展方法来实现权限控制

    • SparkContainer 使用

      @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
      }
      

      没有权限访问就不会渲染到页面上,有权限访问的时候渲染得到的 Html 如下:

      <div class="container" custom-attribute="abcd">1234</div>
      
      <span class="custome_p111">12344</span>
      
    • SparkLink

      @Html.SparkLink("Learn about me &raquo;", "http://weihanli.xyz",new { @class = "btn btn-default" })
      

      有权限访问时渲染出来的 html 如下:

      <a class="btn btn-default" href="http://weihanli.xyz">Learn about me »</a>
      
    • SparkButton

       @Html.SparkButton("12234", new { @class= "btn btn-primary" })
      

      有权限访问时渲染出来的 html 如下:

      <button class="btn btn-primary" type="button">12234</button>
      
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.13.0 1,106 10/19/2020
1.12.0 1,189 1/27/2020
1.11.0 662 1/19/2020
1.10.0 797 12/8/2019
1.10.0-preview-20191204-022215 409 12/4/2019
1.9.2 759 10/20/2019
1.9.1 715 10/15/2019
1.9.0 789 9/24/2019
1.8.2 777 9/3/2019
1.8.0 1,132 6/21/2019
1.6.0 1,115 2/21/2019
1.5.0 899 11/20/2018
1.4.0 931 10/10/2018
1.3.1 1,157 12/9/2017
1.1.0 1,192 10/16/2017

remove reference for WeihanLi.Common and update IActionAccessStrategy interface,optimize code