I work hard, so you don't have to

dotnet SiteMap Provider

dotnet SiteMap Providers are wonderful things, especially when you incorporate them with sitemaps and such.

I am actually going to take you through the process of desigining a custom provider, as dotnet can do half of the hard work for you.

Firstly, you will need to create a basic site, with a basic sitemap and in time, honoured blue peter fashion, here's one I made earlier... (It goes without saying that these nodes each needs a page behind them).


Now the data structure.
Imperative NOTE, you need to get this right the first time otherwise changing it later could seriously hinder your progress.
For my VERY simple example I am going to use two tables:
  • -UserID
  • -Logon
  • -Password
  • -Name
  • -RoleID
  • -RoleID
  • -Description
If you are copying me, finish all up to this point and then go get a coffee.
Welcome back, I like my coffee white with sugar, thanks for asking. But Back to work.
So we have now set up the site map and database structure next is to start assigning roles for the users. When I do this I assign roles by page. In this example I have three pages:
  • Default.aspx - Going to be viewable to all, even those who are not registered
  • Page1.aspx - Going to be viewable only by those who have registered
  • Restricted.aspx - Only viewable by people assigned to the role admin
From the above you can see there are three different scenarios, unregistered, registered, and admin.
Below, you can now see the information that I will hold in each table:
tblUsers tblUsers
UserID Logon Password Name RoleID
1 admin admin admin 2
2 user1 user1 billy 1
3 user2 user2 janice 1
4 harjit harjit harjit 1

RoleID Description
1 user
2 admin

Next, we will need to assign the roles that are going to be allowed access to the web.sitemap file like so:


The next bit is the fun and easy bit, we must now override some of the key functionality of DOTNET, which in many cases is crap (this is however a matter of opinion).
Okay, here goes... Create a new vb file in the app_code project and paste this code into it,
Namespace myNamespace.myNewFunckyClasses
    Public Class SiteMapClass
        Inherits XmlSiteMapProvider
        Public Overloads Overrides Sub Initialize(_ 
            ByVal name As String,_ 
            ByVal attributes As NameValueCollection)
            MyBase.Initialize(name, attributes)
        End Sub

        Public Overrides Function IsAccessibleToUser(_ 
            ByVal context As System.Web.HttpContext,_ 
            ByVal node As System.Web.SiteMapNode) As Boolean

        End Function
    End Class
End Namespace
We are interested in overriding the function IsAccessibleToUser.
When the user logged in you will have stored the USERID somewhere. I will tell you how to fully implement a custom membership provider at a later date, but say for example it is in a session variable called "userid". So begin the overridding of the function...

        Public Overrides Function IsAccessibleToUser(_ 
                ByVal context As System.Web.HttpContext,_ 
                ByVal node As System.Web.SiteMapNode) As Boolean
            Dim roleID As Integer = context.Session.Item("roleID")
            For Each role As String In node.Roles
                If roleID.ToString = role.ToString Or role.ToString = "*" Then
                    'The star denotes a wild card where everyone will have access
                    Return True
                    'access granted
                    Exit Function
                End If
            Return False
            'access denied
        End Function
The final thing that you must do is tell the project that you wish to override the base classes we just did, which involves modifying the web.config.
And heres how...Add the below tags to the
And that SHOULD be it... Come back soon I will add more articles as and when I can. Bye for now!!!
Copyright 2011 CodersAdvocate ©