The tutorial aims to describe the process to avoid temporary log in session or in other words how to create a permanent user login session and you can use this technique in any type of DOT NET project.
Before getting into the depth of this article, you must be familiar with forms authentication in DOT NET. The configuration of form authentication resides in web.config file which has the following configuration-file fragment with the assigned values.
Listing 1: Forms Authentication
The default values are described below:
- loginUrl points to your application's custom logon page. You should place the logon page in a folder that requires Secure Sockets Layer (SSL). This helps ensure the integrity of the credentials when they are passed from the browser to the Web server.
- protection is set to All to specify privacy and integrity for the forms authentication ticket. This causes the authentication ticket to be encrypted using the algorithm specified on the machineKey element, and to be signed using the hashing algorithm that is also specified on the machineKey element.
- timeout is used to specify a limited lifetime for the forms authentication session. The default value is 30 minutes. If a persistent forms authentication cookie is issued, the timeout attribute is also used to set the lifetime of the persistent cookie.
- name and path are set to the values defined in the application's configuration file.
- requireSSL is set to false. This configuration means that authentication cookies can be transmitted over channels that are not SSL-encrypted. If you are concerned about session hijacking, you should consider setting requireSSL to true.
- slidingExpiration is set to true to enforce a sliding session lifetime. This means that the session timeout is periodically reset as long as a user stays active on the site.
- defaultUrl is set to the Default.aspx page for the application.
- enableCrossAppRedirects is set to false to indicate that forms authentication does not support automatic processing of tickets that are passed between applications on the query string or as part of a formPOST.
This method creates an authentication ticket for the supplied user name and adds it to the cookies collection of the response, or to the URL if you are using cookieless authentication. The first overload of this function has two parameters:
- userName: The name of the authenticated user
- createPersisntentCookie: True to create a persistent cookie (one that is saved across browser sessions); otherwise, false.
This method add a cookie or persistent cookie to the browser with an expire time set in "timeOut" parameter with the name and path set in "name" and "path" parameter. The user will be automatically logged out once the cookie is expired. So the user login session depends on the expire of forms authentication ticket saved in browser cookie. Here, we will create a permanent user login session using this technique.
The functionality of this class is to add a form authentication ticket to the browser cookie collection with a life time expiry.
Listing 2: Illustrates Cookie helper
public sealed class CookieHelper
private HttpRequestBase _request;
private HttpResponseBase _response;
public CookieHelper(HttpRequestBase request,
_request = request;
_response = response;
public void SetLoginCookie(string userName,string password,bool isPermanentCookie)
if (_response != null)
FormsAuthenticationTicket userAuthTicket =
new FormsAuthenticationTicket(1, userName, DateTime.Now,
DateTime.MaxValue, true, password, FormsAuthentication.FormsCookiePath);
string encUserAuthTicket = FormsAuthentication.Encrypt(userAuthTicket);
HttpCookie userAuthCookie = new HttpCookie
if (userAuthTicket.IsPersistent) userAuthCookie.Expires =
userAuthCookie.Path = FormsAuthentication.FormsCookiePath;
This function is used in login page or control on the click of login button. In the attached sample project, the following function is written in AccountController class. This function validates the login of the user and then adds a permanent form authentication ticket to the browser.
Listing 3: Validates the login of the user
Private bool Login(string username, string password,bool rememberMe)
if (Membership.ValidateUser(userName, password))
CookieHelper newCookieHelper =
newCookieHelper.SetLoginCookie(userName, password, rememberMe);
Making use of the above technique will help you to develop a functionality that will avoid users to log out after a particular period of time from the web page. This is required more often than to make the users log in again and again which can really lead to some frustration. See you next time.