从FluentSecurity配置

时间:2017-12-19 13:48:22

标签: asp.net-mvc security authentication fluent-security

我正在使用FluentSecurity配置授权的ASP.NET MVC网站上工作。现在我们需要一个自定义的ActionLink Helper,只有当前用户有权访问目标操作时才会显示。

我想知道是否有一种方法可以动态地了解FluentSecurity配置(例如使用SecurityConfiguration类),如果当前记录的用户有权访问给定她的名字(字符串)和她的控制器名称(字符串)的操作。我花了很多时间查看FluentSecurity https://github.com/kristofferahl/FluentSecurity的源代码,但没有成功。

例如:

public bool HasAccess(string controllerName, string actionName) {
      //code I'm looking for goes here
}

1 个答案:

答案 0 :(得分:0)

最后,我会回答自己,这可能有助于另一个人。

我只是模拟HandleSecurityAttribute类的OnAuthorization方法。这段代码效果很好:

public static bool HasAccess(string fullControllerName, string actionName)
    {
        ISecurityContext contx = SecurityContext.Current;
        contx.Data.RouteValues = new RouteValueDictionary();

        var handler = new SecurityHandler();

        try
        {
            var result = handler.HandleSecurityFor(fullControllerName, actionName, contx);
            return (result == null);
        } catch (PolicyViolationException)
        {
            return false;
        }

    }
相关问题