Here's a heck of a thing. I'm doing my own FormsAuthentication Cookie/Ticket, rather then using the built in FormsAuthentication.RedirectFromLoginPage, as I need support for UserData, and other encrypted goodness. So, I need to:
// Create the authentication ticket FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, //version userName, // user name DateTime.Now, //creation DateTime.Now.AddMinutes(??), //Expiration false, //Persistent userDataGoodness); //Secret Sauce
but, I want to use the Timeout value that is already in the Web.config:
<authentication mode="Forms"> <!-- TODO: Set requireSSL to true for production --> <forms requireSSL="false" slidingExpiration="true" loginUrl="~/login.aspx" name="AuthenticationTicket" protection="All" timeout="20" /> </authentication>
which seems reasonable, eh? Plus, as I'm a nice guy (hopefully not a hack) I like to do things the Kosher way. I'd had to hack this up. So, I figure I'll use something like 'FormsAuthentication.Timeout' - except it doesn't exist. I can get to everything else, just not the Timeout.
And the Googling and Reflecting begins. Sometimes I think that's my full time job, Googling and Reflecting.
Here's my thought process, for your edutainment:
string path = "root\\NetFrameworkV1:forms";ManagementObject pm = new ManagementClass(path).CreateInstance();pm["Selector"] = "config://localhost"; // represents the machine.config filepm.Get();Response.Output.WriteLine("timeout = {0}<br>", pm["timeout"]);
Private Function TimeOut_Get() As Integer'Get formsauthentication TimeOut value'Kludge, timeout property is not exposed in the classFormsAuthentication.SetAuthCookie("Username", False) Dim ticket As FormsAuthenticationTicket = FormsAuthentication.Decrypt(Response.Cookies(FormsAuthentication.FormsCookieName).Value)Dim ts As New TimeSpan(ticket.Expiration.Ticks - ticket.IssueDate.Ticks)Return ts.MinutesEnd Function
Note, here's where insanity and over engineering set in...
System.Xml.XmlDocument x = new System.Xml.XmlDocument(); x.Load(UrlPath.GetBasePhysicalDirectory() + "web.config"); System.Xml.XmlNode node = x.SelectSingleNode("/configuration/system.web/authentication/forms"); int Timeout = int.Parse(node.Attributes["timeout"].Value,System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
Scott at DevReach in Bulgaria in October
Developer Stand up Comedy - Coding 4 Fun
TechDays/DevDays Netherlands and Belgium:
Posts by Category Posts by Month
Greatest Hits Dev Tools List