Featured post
ASP.NET: Why is FormsAuthenticationTicket null after authentication timeout? -
i'm implementing authentication timeout detection mechanism per previous question , answer of mine here. i've implemented http module uses authenticaterequest event run code capture whether authentication period has expired. code below:
public class authenticationmodule : ihttpmodule { #region ihttpmodule members void ihttpmodule.dispose() { } void ihttpmodule.init(httpapplication application) { application.authenticaterequest += new eventhandler(this.context_authenticaterequest); } #endregion /// <summary> /// inspect auth request... /// </summary> /// <remarks>see "how implement iprincipal" in msdn</remarks> private void context_authenticaterequest(object sender, eventargs e) { httpapplication = (httpapplication)sender; httpcontext context = a.context; // extract forms authentication cookie string cookiename = formsauthentication.formscookiename; httpcookie authcookie = context.request.cookies[cookiename]; // no longer forms cookie in array once timeout has expired if (authcookie != null) { formsauthenticationticket authticket = formsauthentication.decrypt(authcookie.value); datetime expirationtime = authticket.expiration; // check if authenticated session dead if (authticket != null && authticket.expired) { // send them response indicating they've expired. } } } }
the problem that, once authentication period has expired (i set 1 min test), there no longer forms cookie (see comment in code). means authentication cookie null, , won't make past null check in code. there's convenient "expired" property formsauthenticationticket feel should checking see if period expired. how far if cookie no longer there? reasonable assume authentication period has expired if there's no longer forms cookie?
any appreciated on this.
you might want try this:
if (user != null) { formsidentity id = (formsidentity)user.identity; formsauthenticationticket ticket = id.ticket; if (ticket.expired) { //do } }
edit:
1: see user null. using user.identity out of question.
2: how trying code have in original question in beginrequest event instead of authenticaterequest.
- Get link
- X
- Other Apps
Comments
Post a Comment