How to read FormsAuthentication.Timeout in .NET 3.5
Posted by Dylan Beattie on 31 January 2014 • permalinkForms authentication in .NET has a timeout property, controlled via the web.config section like so:
<system.web>
<authentication>
<forms name="foo" loginUrl="~/Account/Login" timeout="30" />
</authentication>
</system.web>
This setting has existed since .NET 1.0, I believe, but only in .NET 4 did we get a corresponding property on the FormsAuthentication object. To access the Timeout property in .NET 4+, you just call
FormsAuthentication.Timeout
To do the same in .NET 3.5, you need to do this:
var defaultTimeout = TimeSpan.FromMinutes(30);
var xml = new XmlDocument();
var webConfigFilePath = Path.Combine(HttpRuntime.AppDomainAppPath, "web.config");
xml.Load(webConfigFilePath);
var node = xml.SelectSingleNode("/configuration/system.web/authentication/forms");
if (node == null || node.Attributes == null) return (defaultTimeout);
var attribute = node.Attributes["timeout"];
if (attribute == null) return (defaultTimeout);
int minutes;
if (Int32.TryParse(attribute.Value, out minutes)) return(TimeSpan.FromMinutes(minutes));
return(defaultTimeout);
I'll file this under "Things that wrong with Forms Authentication, #217"