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
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn"
protection="All"
timeout="1"
name=".USERLOGINCONTROLAUTH"
path="/"
requireSSL="false"
slidingExpiration="true"
defaultUrl="~/Home/Index"
cookieless="UseDeviceProfile"
enableCrossAppRedirects="false"/></authentication>The default values are described below:
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:
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,
HttpResponseBase response)
{
_request = request;
_response = response;
}
//[DebuggerStepThrough()]
public void SetLoginCookie(string userName,string password,bool isPermanentCookie)
{
if (_response != null)
{
if (isPermanentCookie)
{
FormsAuthenticationTicket userAuthTicket =
new FormsAuthenticationTicket(1, userName, DateTime.Now,
DateTime.MaxValue, true, password, FormsAuthentication.FormsCookiePath);
string encUserAuthTicket = FormsAuthentication.Encrypt(userAuthTicket);
HttpCookie userAuthCookie = new HttpCookie
(FormsAuthentication.FormsCookieName, encUserAuthTicket);
if (userAuthTicket.IsPersistent) userAuthCookie.Expires =
userAuthTicket.Expiration;
userAuthCookie.Path = FormsAuthentication.FormsCookiePath;
_response.Cookies.Add(userAuthCookie);
}
else
{
FormsAuthentication.SetAuthCookie(userName, isPermanentCookie);
}
}
}
}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 =
new CookieHelper(HttpContext.Request,HttpContext.Response);
newCookieHelper.SetLoginCookie(userName, password, rememberMe);
return true;
}
else
{
return false;
}
}
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.
.jpg)







See the prices for this post in Mr.Bool Credits System below: