从FluentSecurity配置中动态提取信息



我正在使用使用FluentSecurity配置授权的ASP.NET MVC网站。现在,我们需要一个自定义的ActionLink助手,仅当当前用户可以访问目标操作时才显示。

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

例如:

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

最后,我会回答自己可能会有所帮助。

我只是模仿了handlesecurityAttribute类的攻击方法。此代码运行良好:

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

最新更新