Freigeben über


Authorization Sample 302 – Site Map Navigation

In Authorization Sample 301 I explained the ins-and-outs of the authorization sample and offered a few hints as to how it could be used. In this post, I will show a specific example where I put together a Site Map in Silverlight.

image[9]

I wanted to stay fairly close to the design David Poll used in his post on the topic. I created a SiteMapNode type to store the information necessary to authorize, create links, and work with Uri mapping.

   public class SiteMapNode
  {
    public IEnumerable<AuthorizationAttribute>
             AuthorizationAttributes { get; set; }
    public string Description { get; set; } 
    public Uri MappedUri { get; set; }
    public Uri MappingUri { get; set; }
    public string TargetName { get; set; } 
    public string Title { get; set; } 
    public Uri Uri { get; set; } 
  }

Next, I created a fairly simple AuthorizationRule to interpret the information in the SiteMapNode. It takes the target input as a Uri and uses it to look up AuthorizationAttributes in the site map.

   public class SiteMapAuthorizationRule : AuthorizationRule
  {
    public override IEnumerable<AuthorizationAttribute>
                      GetAuthorizationAttributes(object target)
    {
      List<AuthorizationAttribute> attributes =
                                     new List<AuthorizationAttribute>();
  
      Uri targetUri = target as Uri;
      if (targetUri != null)
      {
        if (WebContext.AuthorizationMap.ContainsKey(targetUri))
        {
          attributes.AddRange(
                       WebContext.AuthorizationMap[targetUri]);
        }
      }
  
      return attributes;
    }
  }

Finally, I added the rule to the AuthorizationRuleManager as the default rule for authorizing Uris. Now, every time the extension library authorizes navigation on the content frame, it will use my custom rule.

   AuthorizationRuleManager.AddAuthorizationRule(
                             typeof(Uri), new SiteMapAuthorizationRule());

The rest of the sample shows how to turn the site map into a bunch of authorized links. I’ve included the source here for your reference.

Alternate approaches might tie the site map into the UriMapper, define it in xml, or represent it in an altogether different manner. Each of these options could be implemented following the same pattern; create the site map, create a rule to interpret it, and register the rule. In my sample, I chose to create the site map inline for brevity put there is plenty of flexibility here.

Comments

  • Anonymous
    September 28, 2010
    unfortunately UriMapper/UriMapping is sealed and you will do that big work, at the feature i hope UriMapper/UriMapping not marked as sealed and we can mark our UriMapping with your attributes. Great job Kyle and thanks for you hear us.