3

I have used the below code in app code session.cs file. It has been loaded initially in httpmodule.cs, whenever every page hit in browser. I found the httpcontext.current.session value is null.

if (HttpContext.Current != null && HttpContext.Current.Session != null)
        {
            try
            {
                if (HttpContext.Current.Session.Keys.Count > 0)
                {

                }
                else
                {
                    HttpContext.Current.Response.Cookies["ecm"].Expires = DateTime.Now;
                    HttpContext.Current.Response.Cookies["ecmSecure"].Expires = DateTime.Now;


                }
            }
            catch (Exception ex)
            {

            }
        }

web.config session settings :

<sessionState mode="InProc" 
              stateConnectionString="tcpip=127.0.0.1:42424" 
              sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" 
              cookieless="false" 
              timeout="5" />
Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
  • [This](http://stackoverflow.com/questions/7705802/httpcontext-current-session-is-null-in-mvc-3-appplication) will probably be helpful – Jonesopolis Sep 17 '13 at 17:54
  • Sorry jonesy. It isn't help full – user2739544 Sep 17 '13 at 18:02
  • What exactly means "loaded initially" ? Is that webforms or MVC asp.net app ? How session is declared in web.config ? – Antonio Bakula Sep 17 '13 at 18:34
  • Intially loaded the httpmodule.cs then we access the session there. – user2739544 Sep 17 '13 at 18:36
  • So initially really means in BeginRequest of HttpModule ? – Antonio Bakula Sep 17 '13 at 18:44
  • From my experiences (I haven't been able to validate it, so just a comment) but the global Session variable doesn't become initialized until after the base Controller (custom or otherwise) has completed it's initialization. For this reason you can't reference Session from before this point, otherwise you'll get a null. Even calling it during a custom Controller constructor will result in null. Once the base Controller is initialized it seems to work fine, even during a constructor for the inherited controller. – siva.k Sep 17 '13 at 19:21
  • What if you change your web.config to simple `` ? – Yuriy Galanter Sep 17 '13 at 21:17

2 Answers2

0

One possible way could be to wrap it in a property and access it like a string:-

string SessionVariable
{
   get{
      return Session("SessionVariable") ?? String.Empty;
   }
   set{
      Session("SessionVariable") = value;
   }
}

then use like:-

if( String.IsNullOrEmpty( SessionVariable) )
{
   // do something
}
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

Thanks friends with responses. I found the answer. The session value is generated http module. only the end request is called after access the session value. The below code i used.

public void Init(HttpApplication context)
{
    context.BeginRequest += new EventHandler(context_BeginRequest);
    context.EndRequest += new EventHandler(context_EndRequest);
    context.AcquireRequestState += new EventHandler(SessionClear);
}

public void SessionClear(object sender, EventArgs e)
{
    try
    {
        if (HttpContext.Current != null && HttpContext.Current.Session != null)
        {
            if (!(HttpContext.Current.Session.Keys.Count > 0))
            {

            }

        }
    }
    catch (Exception ex)
    {
    }
}