How could I read session state information in web.config

zanhtet picture zanhtet · Jun 8, 2011 · Viewed 11.5k times · Source

I configured session state in web.config.

<sessionState cookieless="AutoDetect" timeout="5" sqlConnectionString="....."/>

Now, I want to know timeout and sqlConnectionString from code-behind. Please, help me.

Answer

VinayC picture VinayC · Jun 8, 2011

You can use Session.Timeout to know timeout value.

However, better way is to use configuration API to read configuration. In this case, use code given below to get reference to session state configuration and then use properties such as SqlConnectionString and Timeout to find the necessary configured values.

using System.Web.Configuration;

...

var sessionSection = (SessionStateSection)WebConfigurationManager.GetSection("system.web/sessionState");