views:

3203

answers:

1

Hi, In my asp.net website i am using asp.net form authentication with following configuration

<authentication mode="Forms">
      <forms loginUrl="~/Pages/Common/Login.aspx" defaultUrl="~/Pages/index.aspx" protection="All" timeout="30" name="MyAuthCookie" path="/" requireSSL="false" cookieless="UseDeviceProfile" enableCrossAppRedirects="false" >
      </forms>
     </authentication>

I have following questions

  1. What should be timeout value for session because i am using sliding expiration inside form authention due to which session will expire before form authentication. How can i protect it?

  2. After formauthentication log out i would like to redirect page at logout.aspx but it is automatically redirect me at loginpage.aspx. How is it possible?

+2  A: 
  1. To be on the safe side: TimeOut(Session) <= TimeOut(FormsAuthentication) * 2
  2. If you want to show page other than specified in loginUrl attribute after authentication timeout you need to handle this manually as ASP.NET does not provide a way of doing it.

To achieve #2 you can should manually check the cookie and its AuthenticationTicket for expiration and redirect to your custom page if they have expired.
You can do in it in one of the events: AcquireRequestState, AuthenticateRequest.

Sample code in the event can look like:

var cookie = Retrieve AuthenticationCookie();
if (cookie == null) return;
FormsAuthenticationTicket ticket = null;
try {
    ticket = FormsAuthentication.Decrypt(cookie.Value);
} catch (Exceptoin decryptError) {
    // Handle properly
}
if (ticket == null) return; // Not authorised
if (ticket.Expiration > DateTime.Now) {
    Response.Redirect("SessionExpiredPage.aspx"); // Or do other stuff here
}
Dmytrii Nagirniak
Thanks Dmitriy,My second question: As its written above inside <Form ...> that default page is "index.aspx" and login is "login.aspx". After login at my dashboard page when i remain ideal for 30 minute (timeout) and after that i click on any link i will automatically redirect to login page with following URL http://localhost:/virtualdir/Pages/Login.aspx?ReturnUrl=%2fvirtualdir%2fPages%2fDashBoard.aspxBut here i would like to redirect page at logout page where i can say some logout information
Hemant Kothiyal
Updeted the answer.
Dmytrii Nagirniak
Can you show me example because in my case i take scenario where i set formauthentication timeout= 2 minute while session timeout= 6 minute and after 3 minute when i click on link It doesn't debug anywhere even not at "AcquireRequestState"Please help?
Hemant Kothiyal
Upps. Sorry, the timeout for the FormsAuthentication should be twice longer than Session's one. Corrected that.AcquireRequestState and AuthenticateRequest should always be triggered (no matter what). Make sure you correctly subscribed to these events on the HttpApplication class.
Dmytrii Nagirniak
Thanks,Can you clear another doubt. I have confusion that if i set sliding expiration=true in "Formauthentication" then automatically sliding expiration works for session timeout also or not?Does sliding expiration works for session time out?
Hemant Kothiyal
FormsAuthentication expiration (absolute or sliding) is not related to Session expiration in any way. The Session has no absolute expiration and always expires "sliding" way.
Dmytrii Nagirniak
Are you sure that you want to have Session.Timeout < FormsAuthentication.Timeout * 2? This means that the Session can be abandoned while the user is still logged in. Anywhere that references Session variables will start having NullReferenceExceptions.
Dominic Zukiewicz