views:

119

answers:

2

I have implemented a custom UrlAuthorization module as shown here

The code is as follows:

public class CustomUrlAuthorizationModule : IHttpModule 
    {
        public void Init(HttpApplication context)
        {
            context.AuthorizeRequest += new EventHandler(context_AuthorizeRequest);
        }



        void context_AuthorizeRequest(object sender, EventArgs e)
        {
            HttpApplication context = (HttpApplication)sender;

            if (context.User != null && context.User.Identity.IsAuthenticated)
            {
                HttpContext _httpContext = context.Context;
                SiteMapNode node = SiteMap.Provider.FindSiteMapNode(_httpContext);
                if (node == null)
                    throw new UnauthorizedAccessException();


            }
        }

        public void Dispose()
        {
        }

    }

My question is, do I have to call init from within every single one of my pages on load or is there a way to set IIS to do it automatically with every load.

This question is probably very dumb....

+1  A: 

As long as you register your HTTPModule within the web.config, IIS will set it up for you.

Init is called as the initialization of your module, then you add the handler to the respective context event to handle, therefore it will respond to all requests.

Mitchel Sellers
Thanks, I knew there had to be a simple way.
Matt
+2  A: 

You have to hook up the module in the web.config. For example:

<configuration>
 <system.web>
  <httpModules>
   <add type=
    "MattsStuff.CustomUrlAuthorizationModule, MattsStuff"
    name="CustomUrlAuthorizationModule" />
  </httpModules>
 </system.web>
gWiz
+1 for showing an example - thanks
Praesagus