使用FluentSecurity加密ASP.NET MVC

    xiaoxiao2021-09-23  118

    本文总结如何使用FluentSecurity加密ASP.NET MVC

    完整教程:https://blog.mariusschulz.com/2011/12/05/securing-an-aspnet-mvc-application-using-fluentsecurity

    github: https://github.com/kristofferahl/FluentSecurity

    使用FluentSecurity加密MVC应用程序

    FluentSecurity优点:

    1  授权规则可以用于controller或者单个action 方法

    2  Security中心化实施,所以可维护性强;[Authorize]属性也就不再必要

    4  可以扩展类库,实施自己的授权规则和违反处理;

    5  security configuration是可以单元测试的;允许测试是否正常工作;

    使用方法

    1 安装方法?

    install-package FluentSecurity

    2 如何获取用户授权状态?

    2.1 打开Global.asax

    2.2 添加引用 using FluentSecurity;

    2.3 FilterConfig.cs文件,RegisterGlobalFilters(GlobalFilterCollection filters)方法体中添加如下代码:

    filters.Add(new HandleSecurityAttribute(), 0);

    设置属性过滤器的运行顺序为0是很重要的,这样能确保安全规则管道优先执行;

    2.4  Application_Start方法体中,RegisterGlobalFilters ()前,添加security configuration(如下代码)

    SecurityConfigurator.Configure(configuration => { // Tell FluentSecurity where to obtain the user authentication status from configuration.GetAuthenticationStatusFrom(() => HttpContext.Current.User.Identity.IsAuthenticated); });

    如果现在运行程序,会得到一个ConfigurationErrorsException 。因为默认情况下:FluentSecurity 会抛出异常,当没有security 显示制定的时候。如果希望关闭此特性,需要制定如下代码:

    configuration.IgnoreMissingConfiguration();

    3 如何制定特殊的安全政策?

    目前,我们配置了authentication 信息,但是还没指定任何authorization 规则;FluentSecurity 使用Policy来配置规则;

    举例:

    3.1 为了防止HomeController的未授权访问,应该在 SecurityConfig.cs文件下添加如下代码

    configuration.For<HomeController>().DenyAnonymousAccess();

    现在,如果有未授权用户尝试访问HomeController,会得到PolicyViolationException

    3.2如何给所有的Controller加上相同政策?

    SecurityConfig.cs文件下加入如下代码:

    // Secure all action methods of all controllers configuration.ForAllControllers().DenyAnonymousAccess(); // Make sure that users can still log on configuration.For<AccountController>(ac => ac.LogOn()).Ignore();

    注意:以上代码中,ac => ac.LogOn() 限制了IgnorePolicy 政策应用于LogOn方法。

    1.4版本中,如下政策可用

    DelegatePolicy — The specified delegate must return true or a success result.

    DenyAnonymousAccessPolicy — The user must be authenticated.

    DenyAuthenticatedAccessPolicy — The user must be anonymous.

    IgnorePolicy — All users are allowed.

    RequireAllRolesPolicy — The user must be authenticated with all of the specified roles.

    RequireRolePolicy — The user must be authenticated with at least one of the specified roles.

    4 如何实施自定义Policy?

    4.1 如下代码:

    public class WeekendsOnlyPolicy : ISecurityPolicy { public PolicyResult Enforce(ISecurityContext context) { DateTime now = DateTime.Now; bool isWeekend = now.DayOfWeek == DayOfWeek.Saturday || now.DayOfWeek == DayOfWeek.Sunday; return isWeekend ? PolicyResult.CreateSuccessResult(this) : PolicyResult.CreateFailureResult(this, "Access denied!"); } }

    5  如何处理Policy违反?

    违反policy时,FluentSecurity 会抛出PolicyViolationException异常。  开发人员可以捕获异常并且做对应处理。 建议符合以下原则:

    5.1 必须实现IPolicyViolationHandler接口,一个handle method, 这个方法接受PolicyViolationException 异常,同时返回ActionResult。

    5.2 handler的明明必须符合格式:<PolicyName>ViolationHandler,因为FluentSecurity 使用命名来定位handler

    综上,推荐使用IOC container来注册 custom policy violation handlers。

    更多信息参考:https://github.com/kristofferahl/FluentSecurity/wiki/Policy-violation-handlers

    6 测试security 配置

    6.1 如何安装?

    install-package FluentSecurity.TestHelper

    6.2 范例代码

    // Arrange Bootstrapper.ConfigureFluentSecurity(); // Act var results = SecurityConfiguration.Current.Verify(expectations => { expectations.Expect<HomeController>().Has<DenyAnonymousAccessPolicy>(); expectations.Expect<AccountController>().Has<DenyAnonymousAccessPolicy>(); expectations.Expect<AccountController>(ac => ac.LogOn()).Has<IgnorePolicy>(); }); // Assert bool isValidConfiguration = results.Valid(); Assert.IsTrue(isValidConfiguration);

    转载请注明原文地址: https://ju.6miu.com/read-677813.html

    最新回复(0)