Hiding nodes which should not be available when authenticated

Feb 21, 2011 at 10:46 PM

Is there a way to hide nodes when the user is authenticated?

I am trying to have a node for logging in that is visible only when the user is not logged in. You can have MvcSiteMapProvider hide items that require the user to be logged in first (using the AuthorizeAttribute), but how do you do the opposite, make it visible only when the user is not logged in?

I tried making my own attribute attribute so I could do something like this:

public class AccountController
  public ActionResult LogOn() { }

And then created a NotAuthenticatedAttribute derived from ActionMethodSelectorAttribute. That didn't work so I derived it from AuthorizeAttribute, which also wasn't identified by MvcSiteMapProvider.

Is there any way to do this?

Feb 22, 2011 at 7:08 AM

You can try writing your own VisibilityProvider that does this:


Visibility provider

In some situations, nodes should be visible in the breadcrumb trail but not in a complete sitemap. This can be solved using the MvcSiteMapProvider.Extensibility.ISiteMapNodeVisibilityProvider extension point that can be specified globally for every node in the sitemap or granularly on a specific sitemap node. A sample is available on the Advanced node visibility page.

Feb 22, 2011 at 5:09 PM
Edited Feb 22, 2011 at 5:13 PM

That works.

    public class LogOnVisibilityController : ISiteMapNodeVisibilityProvider
        public bool IsVisible(SiteMapNode node, HttpContext context, IDictionary<string, object> sourceMetadata)
            return !context.Request.IsAuthenticated;
 <mvcSiteMapNode title="Login" controller="Account" action="LogOn"  visibilityProvider="Website.Controllers.LogOnVisibilityController, Website" />

I'd still like it if there were a NotAuthenticated attribute, but that is more of an MVC framework issue. I'd like to have it to prevent ambiguity between controller functions and then have MvcSiteMapProvider use it as well.

Thanks maartenba.