Search code examples
asp.net-mvcmvcsitemapprovider

MvcSiteMapProvider issue when set route values for node with securityTrimmingEnabled is true


I have project with win auth and i use "securityTrimmingEnabled" option for detect to which parts of menu user have access.

I set site map node properties:

siteMapNode.ParentNode.Title = entity.Parent.Title;
siteMapNode.ParentNode.RouteValues["id"] = entity.Parent.Id; 

And if "securityTrimmingEnabled" is false, everything is ok and i have correct url in map path(/portfolios/facility/57), if "securityTrimmingEnabled" is true, title of node is ok, but "id" param is not correct(/portfolios/facility/0)

How i can resolve this issue?


Solution

  • This is basically the same issue described here.

    The AuthorizeAttributeAclModule is accessing the Url property and causing it to be request cached, so setting properties that affect the URL afterward (such as in a controller action) has no effect.

    Option 1

    Move the code that sets the RouteValues["id"] into a IDynamicNodeProvider implemenation. This will load the values into the shared cache at application startup (and when the cache expires), but it also loads them before the AuthorizeAttributeAclModule fires.

    Option 2

    Move the code that sets the values into the Application_BeginRequest event as described here.

    Option 3

    Create a custom implementation of RequestCacheableSiteMapNode that doesn't override the Url property and a custom SiteMapNodeFactory to provide instances of your new class, then inject them both with DI.

    Option 4

    Remove the request cache value for the Url property manually, so it can be regenerated the next time the Url property is accessed.

    var parentNode = siteMapNode.ParentNode;
    
    parentNode.Title = entity.Parent.Title;
    parentNode.RouteValues["id"] = entity.Parent.Id; 
    
    var urlRequestCacheKey = "__MVCSITEMAPNODE_" + parentNode.SiteMap.CacheKey + "_" + parentNode.Key + "_Url_True_";
    this.HttpContext.Items.Remove(urlRequestCacheKey);