ASP.NET 2.0: Problem in Httpcontext.current.session.add()

asp.netglobal-asax

Can anybody help me to find out solution of following problem.

  1. In ASP.NET website: at Application_OnPostAuthenticate() event, whatever code i write is executed for every request. therefore due to this customidentity object, countryid and weatherid is called everytime for each request (call for database for value). It effect response time of page and unneccessary code execute.

    void Application_OnPostAuthenticateRequest(object sender, EventArgs e)
    {

    // Get a reference to the current User
    
    IPrincipal objIPrincipal = HttpContext.Current.User;
    
    // If we are dealing with an authenticated forms authentication request
    
    if ((objIPrincipal.Identity.IsAuthenticated) && (objIPrincipal.Identity.AuthenticationType == "Forms"))
    {
        CustomPrincipal objCustomPrincipal = new CustomPrincipal();
        objCustomPrincipal = objCustomPrincipal.GetCustomPrincipalObject(objIPrincipal.Identity.Name);
        HttpContext.Current.User = objCustomPrincipal;
        CustomIdentity ci = (CustomIdentity)objCustomPrincipal.Identity;            
        HttpContext.Current.Cache["CountryID"] = FatchMasterInfo.GetCountryID(ci.CultureId);
        HttpContext.Current.Cache["WeatherLocationID"] = FatchMasterInfo.GetWeatherLocationId(ci.UserId);
        Thread.CurrentPrincipal = objCustomPrincipal;
    }
    

    }

To solve this problem when i try tochange code as follows
HttpContext.Current.Session.Add("test", FatchMasterInfo.GetWeatherLocationId(ci.UserId);); in place of cache i found foolowing error
"Object refrence not set to the instance of object"

I don't know whether we can store session variable inside Application_OnPostAuthenticate() event or not?

Best Answer

You could try doing this a bit later in the request, such as in the PreRequestHandlerExecute event:

protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
    IPrincipal objIPrincipal = HttpContext.Current.User;
    if ((objIPrincipal.Identity.IsAuthenticated) && (objIPrincipal.Identity.AuthenticationType == "Forms"))
    {
        HttpSessionState session = HttpContext.Current.Session;
        CustomPrincipal objCustomPrincipal = new CustomPrincipal();
        if (session[objIPrincipal.Identity.Name] == null)
        {
            // get data from database or wherever
            objCustomPrincipal = objCustomPrincipal.GetCustomPrincipalObject(objIPrincipal.Identity.Name);
            CustomIdentity ci = (CustomIdentity)objCustomPrincipal.Identity;
            Object countryID = FatchMasterInfo.GetCountryID(ci.CultureId);
            Object weatherLocationID = FatchMasterInfo.GetWeatherLocationId(ci.UserId);
            // save in session (not cache as cache is application-wide, not per-user):
            session.Add(objIPrincipal.Identity.Name, objCustomPrincipal);
            session.Add(objIPrincipal.Identity.Name + "_CountryID", countryID);
            session.Add(objIPrincipal.Identity.Name + "_WeatherLocationID", weatherLocationID);
        }
        else
        {
            // already have custom principal object in session
            objCustomPrincipal = (CustomPrincipal)session[objIPrincipal.Identity.Name];
        }

        // set the custom principal object to context/thread
        HttpContext.Current.User = objCustomPrincipal;
        Thread.CurrentPrincipal = objCustomPrincipal;
    }
}
Related Topic