Global.asax in ASP.NET for locking web pages, security and license management system


HTML clipboard

Introduction

The Global.asax file is an optional file used to declare and handle application and session-level events and objects for an ASP.NET web site running on an IIS Web Server. The file contains ASP.NET program code, and is the .NET counterpart of the Global.asa file used for ASP. The Global.asax file resides in the IIS virtual root of an ASP.NET application.
Default Structure of Global.asax

using System;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace TestApplication
{
    public class Global : System.Web.HttpApplication
    {
         
        protected void Application_Start(object sender, EventArgs e)
        {
        }

        protected void Session_Start(object sender, EventArgs e)
        {
          
 
        }
 
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
 
        }
 
protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
 
        }
 
        protected void Application_Error(object sender, EventArgs e)
        {

        }

         protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {
 
        }
 
protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
        {
           
        }
 
        private bool IsUrlInModule(List<string> modules, string url)
        {
           
        }
    }
}

global.asax events

• HttpApplication Events

  1. Application_AcquireRequestState

    Occurs when ASP.NET acquires the current state (for example, session state) that is associated with the current request.
  2.  
  3. Application_AuthenticateRequest

    Occurs when a security module has established the identity of the user.
     
  4. Application_AuthorizeRequest

    Occurs when a security module has verified user authorization.
     
  5. Application_BeginRequest

    Occurs as the first event in the HTTP pipeline chain of execution when ASP.NET responds to a request.
     
  6. Application_Disposed

    Adds an event handler to listen to the Disposed event on the application.
     
  7. Application_EndRequest

    Occurs as the last event in the HTTP pipeline chain of execution when ASP.NET responds to a request.
     
  8. Application_Error

    Occurs when an unhandled exception is thrown.
     
  9. Application_PostAcquireRequestState

    Occurs when the request state (for example, session state) that is associated with the current request has been obtained.
     
  10. Application_PostAuthenticateRequest

    Occurs when a security module has established the identity of the user.
     
  11. Application_PostAuthorizeRequest

    Occurs when the user for the current request has been authorized.
     
  12. Application_PostMapRequestHandler

    Occurs when ASP.NET has mapped the current request to the appropriate event handler.
     
  13. Application_PostReleaseRequestState

    Occurs when ASP.NET has completed executing all request event handlers and the request state data has been stored.
     
  14. Application_PostRequestHandlerExecute

    Occurs when the ASP.NET event handler (for example, a page or an XML Web service) finishes execution.
     
  15. Application_PostResolveRequestCache

    Occurs when ASP.NET bypasses execution of the current event handler and allows a caching module to serve a request from the cache.
     
  16. Application_PostUpdateRequestCache

    Occurs when ASP.NET completes updating caching modules and storing responses that are used to serve subsequent requests from the cache.
     
  17. Application_PreRequestHandlerExecute

    Occurs just before ASP.NET begins executing an event handler (for example, a page or an XML Web service).
     
  18. Application_PreSendRequestContent

    Occurs just before ASP.NET sends content to the client.
     
  19. Application_PreSendRequestHeaders

    Occurs just before ASP.NET sends HTTP headers to the client.
     
  20. Application_ReleaseRequestState

    Occurs after ASP.NET finishes executing all request event handlers. This event causes state modules to save the current state data.
     
  21. Application_ResolveRequestCache

    Occurs when ASP.NET completes an authorization event to let the caching modules serve requests from the cache, bypassing execution of the event handler (for example, a page or an XML Web service).
     
  22. Application_UpdateRequestCache

    Occurs when ASP.NET finishes executing an event handler in order to let caching modules store responses that will be used to serve subsequent requests from the cache.
     
  23. Application_Init

    This method occurs after _start and is used for initializing code.

    Application_Start

    As with traditional ASP, used to set up an application environment and only called when the application first starts.
     
  24. Application_End

    Again, like classic ASP, used to clean up variables and memory when an application ends.

• Session Events:

  1. Session_Start

    As with classic ASP, this event is triggered when any new user accesses the web site.
  2.  
  3. Session_End

    As with classic ASP, this event is triggered when a user's session times out or ends. Note this can be 20 mins (the default session timeout value) after the user actually leaves the site.
• Profile Events:
  1. Profile_MigrateAnonymous

    Occurs when the anonymous user for a profile logs in.

• Passport Events:

  1. PassportAuthentication_OnAuthenticate
    Raised during authentication. This is a Global.asax event that must be named PassportAuthentication_OnAuthenticate.
Possibly more events defined in other HttpModules

ystem.Web.Caching.OutputCacheModule
System.Web.SessionState.SessionStateModule
System.Web.Security.WindowsAuthentication
System.Web.Security.FormsAuthenticationModule
System.Web.Security.PassportAuthenticationModule
System.Web.Security.UrlAuthorizationModule
System.Web.Security.FileAuthorizationModule
System.Web.Profile.ProfileModule

Conclusion


In this article we have seen that global.asax file events. So we can use this every web application for application control, state and Application management , locking web pages , security of web application and license management system.

Is this the Work Around or Best Solution?

Yes this is Best Solution.

Up Next
    Ebook Download
    View all
    Learn
    View all