Click here to Skip to main content
Click here to Skip to main content

Form Authentication Tickets

, 5 Dec 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
How to manage different session time out for different users

Sample Image - Forms_Authentication.jpg

Introduction

Some times we require that there should be different time outs for each user. In case when you give choice to user of your web that he can define his time out himself. In this case each user will set his own time and you have to make your web site of such type that it will maintain session for each user according to time he specifed.

What will we do:

Here I will explain you how to achieve this goal using ASP.Net's form authentication.

Problem

Some asp.net user has problem that when they use authentication tickets, session time out shows inconsistent attitude. Here I will explain how to maintian different time out for different users.

What is form authentication?

In ASP there was no mechanisam for logging user, you just have to put some values in session

that could be user id, and then check this value on each page user is trying to access.
But in .net form authentication provide to a mechanism for logging user.
When to login you set cookie that is encrypted you need to put user id in that cookie, that

is very easy, and after that asp.net will do all the task for you need not to check for

authentication each page.
What you have to do is specify a login page in web.config, there are few other settings also

i will tell you in detail. dont worry abou that that are very easy and can be done in few

seconds.

Lets Start Work:

Create 3 page
1. Create asp.net project (example contains C# code)

2. Creat login page, login.aspx. (form authentication automatically redirect to this page if request is unautorized)

3. Create default page, default.aspx (every site has a defaul page, form autentication automatically redirect to this page after successful login).

4.create details page, details.aspx (optional page, that shows your product etc)


some page will be created automatically for example
1.web.config
2.global.asax

You have completed all the structure of your website well done!!! lets start conding now!


Starting Coding:

Web.config:


First of all do some change in web.config file...
Go to the section authentication of web.config
i. set authentication code to forms

ii. provide login url, that is your login page where user will be redirected in case he is not loged on and trying to access some page.

iii.provide passwordformat for now just put it clear dont confused here this is just to set

your password will be clear format or encrypted that is an other topic.
iv. provide user names if you have some static users, you can also load list form database

will tell you letter how to do that.

 <authentication mode="Forms" > 
        <forms  name=".ASPXSessionDemoTest" loginUrl="login.aspx" protection="All"  >
            <credentials passwordFormat = "Clear"> 
                <user name="admin" password="admin"/>               
                 
            </credentials>
        </forms>    
        </authentication>Go to the section autorization of the web config.
i.deny user set ? mark.
so that it ask for password to each user.
   <authorization>
        <deny users="?" /> <!-- Allow all users -->
            <!--  <allow     users="[comma separated list of users]"
                             roles="[comma separated list of roles]"/>
                  <deny      users="[comma separated list of users]"
                             roles="[comma separated list of roles]"/>
            -->
    </authorization>
We have alomost done with web.config file.


Login.aspx:

when user enter user name
and password and click login button write following code (find form source)

   // Create a custom FormsAuthenticationTicket containing
   // application specific data for the user. 
    
   // user email address
   string email        = this.TextBox1.Text ; 
   //user password
   string password     = this.TextBox2.Text ; 
   bool   isPersistent = false; //Persist.Checked; 
   //write down u r own methods for authentication
   //either from database or file
   
   if (Authenticat(email)) 
   { 
    //if user is authunticated then
    //get the user time from databse or file where user have 
metiontioed it. this will return an intger value, for example 30, 60 this is time out in 
minutes. 
    int timeout=settimeout();
    
    //this variable will be accessible automatically
    //on each form...
    //so if you have some role base system
    //or you want to assign some data to this user
    //then put this along with this ticket
    //as done here 
    //supose mubi is admin and i want store this info in ticket
    //so that i can get it on each page
    //you will get this information from database that what is 
his roles. i am hard coding here.
     
    string username="mubi"; 
    string userData = setrole(username); 
    FormsAuthenticationTicket ticket = new 
FormsAuthenticationTicket(
     1,
     email,
     System.DateTime.Now,
     System.DateTime.Now.AddMinutes(timeout),
     isPersistent,
     userData,
     FormsAuthentication.FormsCookiePath); 
    // Encrypt the ticket.
    string encTicket = FormsAuthentication.Encrypt(ticket); 
    // Create the cookie.
    Response.Cookies.Add(new 
HttpCookie(FormsAuthentication.FormsCookieName, encTicket)); 
    // Redirect back to original URL.
     
Response.Redirect(FormsAuthentication.GetRedirectUrl(email,isPersistent)); 

 

Logout Button:

on logout button press write following code

//sign out from form authentication

FormsAuthentication.SignOut();

//abandon session

Session.Abandon();

Response.Redirect("logon.aspx"); 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mubi | www.mrmubi.com
Business Analyst Valentia Technologies
Ireland Ireland
Mubi
^^^^^^^^^^^^^^^^^^^^^^^
www.mubshir.com
Follow on   Twitter

Comments and Discussions

 
GeneralForms Authentication SignOut Pinmemberumaramiya18-Apr-07 23:47 
GeneralRe: Forms Authentication SignOut PinmemberMubshir Raza Ali19-Apr-07 0:00 
QuestionAuthenticat? Pinmembermrkenn777122-Jun-05 10:42 
Generalstore Logout datetime PinmemberQ_Quek31-Mar-05 14:47 
GeneralRe: store Logout datetime PinmemberMubshir Raza Ali19-Apr-07 0:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 5 Dec 2005
Article Copyright 2004 by Mubi | www.mrmubi.com
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid