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
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?
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?
The Forms Authentication Timeout value sets the amount of time in minutes that the authentication cookie is set to be valid, meaning, that after value number of minutes, the cookie will expire and the user will no longer be authenticated—they will be redirected to the login page automatically.
Now, why is this necessary.. Forms authentication timeout indicates, how long a user is recognised and stay authenticated in case of any lack of inactivity and similarly session timeout indicates how long to preseve users session in case of any inactivity.
Session timeout represents the event occuring when a user does not perform any action on a web site during an interval (defined by a web server). The event, on the server side, changes the status of the user session to 'invalid' (ie.
Forms authentication enables user and password validation for Web applications that do not require Windows authentication. With forms authentication, user information is stored in an external data source, such as a Membership database, or in the configuration file for an application.
To achieve #2 you can 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:
// Retrieve AuthenticationCookie var cookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if (cookie == null) return; FormsAuthenticationTicket ticket = null; try { ticket = FormsAuthentication.Decrypt(cookie.Value); } catch (Exception decryptError) { // Handle properly } if (ticket == null) return; // Not authorised if (ticket.Expiration > DateTime.Now) { Response.Redirect("SessionExpiredPage.aspx"); // Or do other stuff here }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With