简体   繁体   中英

Dynamically extract information from FluentSecurity configuration

I'm working on an ASP.NET MVC Website that uses FluentSecurity to configure authorizations. Now we need a custom ActionLink Helper which will be displayed only if the current user has access to the targeted action.

I want to know if there is a way to know dynamically, from FluentSecurity Configuration (using SecurityConfiguration class for example), if the current logged user has access to an action given her name (string) and her controller name (string). I spend a lot of time looking in the source code of FluentSecurity https://github.com/kristofferahl/FluentSecurity but with no success.

For example:

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

Finally, i will answer myself may this will help another one.

I just emulate the OnAuthorization method of the HandleSecurityAttribute class. this code works well:

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

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM