redirecting to loginpage on session timeout

Last post 05-14-2008 5:33 AM by Benson Yu - MSFT. 29 replies.

Sort Posts:

  • redirecting to loginpage on session timeout

    05-08-2008, 2:50 AM

    Hi,

    I am having one application in vs 2005 and in that,

    i want to set the session time out and the application should be redirected to the login page on session timeout?

    i am very new to this concept.please explain it and help me.

    Thanks

    chit

     

    Chithra_Iyer
  • Re: redirecting to loginpage on session timeout

    05-08-2008, 5:08 AM
    • Loading...
    • blurearc
    • Joined on 04-25-2008, 11:15 AM
    • Mumbai India
    • Posts 530

    create a class which check about session value.

    1.  if you want to redirect on post back then check this class value on page load.. and if found session null then redirect.
    2. now if you want it work automatically.. use Ajax timer control of asp.net and keep it in a user control and use this control on each page..

    cheers

     

    "Mark as answered if you feel this helps you"
    "Curiosity is Bliss"
    Regards,

    Ravi Kant Srivastava
    (Sr. Software Engineer)
    Connexxions Business
    Mumbai
    INDIA
  • Re: redirecting to loginpage on session timeout

    05-08-2008, 5:19 AM

    Hi,

     I would like to follow your first point and sorry i am not getting that.

    can you explian the way to do?.

    which class value i have to check?on which page's lad event?

    Plzzz help me

    cheers

    Chithra_Iyer
  • Re: redirecting to loginpage on session timeout

    05-08-2008, 5:33 AM
    • Loading...
    • blurearc
    • Joined on 04-25-2008, 11:15 AM
    • Mumbai India
    • Posts 530

    Use this in a new clas in your asp.net project

     

    using System.Web;

    namespace Test

    {

    public static class SessionCheck

    {

    public static bool Session(string SessionKey)

    {

    return (HttpContext.Current.Session[SessionKey]) != null ? true : false;

    }

    }

    }

    now if you want to redirect on any POSTBACK , so call this class in Page_Load event of your aspx codebehind  like this

    SessionCheck.Session("Give your Session Value"). it will return either true or false.. then you can redirect your user to login.

    "Mark as answered if you feel this helps you"
    "Curiosity is Bliss"
    Regards,

    Ravi Kant Srivastava
    (Sr. Software Engineer)
    Connexxions Business
    Mumbai
    INDIA
  • Re: redirecting to loginpage on session timeout

    05-08-2008, 5:43 AM

    here,where u r specifying to redirect to login page?

    can u correct my code..plzzzz

    this is the one given in web.config file

    <system.web><sessionState timeout="2" >

     

    </sessionState>

    <compilation debug="true">

     

    </compilation>

    <authentication mode="Windows">

     

    </authentication>

    and i have created one global.asax file(since it is not displaying by default).

    then ,in Global.asax,the code is like

    void Session_End(object sender, EventArgs e)

    {

     

    Response.Redirect("SessionExpired.aspx");

    }

     

    but it is not working.?

    can u correct this,or can u tell some other way from the first step.because i am doing this for the first time.

    plzzzz helpme.

    cheers

    Chithra_Iyer
  • Re: redirecting to loginpage on session timeout

    05-08-2008, 5:58 AM
    Answer
    • Loading...
    • blurearc
    • Joined on 04-25-2008, 11:15 AM
    • Mumbai India
    • Posts 530

    dear..

    this will never work..

    because..global.asax is only 1 for every web site..

    but there can be many instances on 1000'nds of browser..

    so which browser will be redirected. have a think..

    now take a scenario that we both are logged in to the same site.. but if ur session has expired then why i should logout.

    to do it automatically.. you have to put ASP.net AJAX timer control..

    apart from that i don't think there is any way by which you can redirect.. untill and unless you will do some request to ur server.

     

    "Mark as answered if you feel this helps you"
    "Curiosity is Bliss"
    Regards,

    Ravi Kant Srivastava
    (Sr. Software Engineer)
    Connexxions Business
    Mumbai
    INDIA
  • Re: redirecting to loginpage on session timeout

    05-08-2008, 6:05 AM

    ok thanks,

    then, is there any other way to do this without using ajax timer?.

    plzzz help me. ASAP

    it is urgent

    cheers

    Chithra_Iyer
  • Re: redirecting to loginpage on session timeout

    05-08-2008, 6:09 AM

    Hi

    what u need to do is. chek for the session using the above said method and redirect if the returned values is false.

    if(SessionCheck.Session("Give your Session Value")==false)

    {

       

    Response.Redirect("login.aspx");

    }

    Place the above code in the page load event

  • Re: redirecting to loginpage on session timeout

    05-08-2008, 6:20 AM
    • Loading...
    • blurearc
    • Joined on 04-25-2008, 11:15 AM
    • Mumbai India
    • Posts 530

    this is what i am trying to explain.. thatis why i wrote that class..

    because untill and unless any request will made to server ... server will not able to work out.. that what to do in session end..

    or if you know.. old javascript style ajax.. you can also do in that way... but .. if you use page_load solution.. then it will be fast and easy to complete ur requirment.

     

     

    "Mark as answered if you feel this helps you"
    "Curiosity is Bliss"
    Regards,

    Ravi Kant Srivastava
    (Sr. Software Engineer)
    Connexxions Business
    Mumbai
    INDIA
  • Re: redirecting to loginpage on session timeout

    05-08-2008, 6:20 AM

    hi,

    Session("Give your Session Value") means what?

    where i have to give?

    in global.asax file?

    please can u explain it with my code which i have given in the previous mail??

     

    Chithra_Iyer
  • Re: redirecting to loginpage on session timeout

    05-08-2008, 6:37 AM

    ok i have created a new class and placed the code which you have given inside that class.

    then the remainind section i am not getting.page load of which page?

    if it is in any page also,it should work.

    I ma having one main page and am dividing it int 3iframes.so load event of which page i shoul write?

    i am callig pages dynamically in these frames depends on the requirements.

    then one more thing is,in,SessionCheck.Session("Give your Session Value"). ,what is the session value?

    cheers

    Chithra_Iyer
  • Re: redirecting to loginpage on session timeout

    05-08-2008, 6:43 AM
    • Loading...
    • blurearc
    • Joined on 04-25-2008, 11:15 AM
    • Mumbai India
    • Posts 530

    buddy, this you have to deciede that where you want to put your login of logout.. but normally every page should check session values if session dependent... so you can put this check in every page..

    or create one usercontrol. put this on user control.. and then call that user control on each page..

    :)

    "Mark as answered if you feel this helps you"
    "Curiosity is Bliss"
    Regards,

    Ravi Kant Srivastava
    (Sr. Software Engineer)
    Connexxions Business
    Mumbai
    INDIA
  • Re: redirecting to loginpage on session timeout

    05-08-2008, 6:51 AM

    Ok chitra.

    Let us say your website has alogin page. After succesful login you may be setting the logged in users detail in session like this.

    Session["loggedinuser"] = "chitra";

    so what u neednow is swhen the user is idle for a long time you want to redirect him to a differnt page. The class given by bluearc will check whether the session variable exists or not. THe session value is actually the name of session you give. In my example I have given it as loggedinuser.

    So in the page load of your main page you have to give something like this.

    if(SessionCheck.Session("loggedinuser")==false)

    {

       

    Response.Redirect("login.aspx");

    }

    Replace loggedinuser with your value

  • Re: redirecting to loginpage on session timeout

    05-08-2008, 7:11 AM

    Hi I have created one class like this

    using System;

    using System.Data;

    using System.Configuration;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Web.UI.HtmlControls;

    /// <summary>

    /// Summary description for SessionCheck

    /// </summary>

    public static class SessionCheck

    {

    public static bool Session(string SessionKey)

    {

    return (HttpContext.Current.Session[SessionKey]) != null ? true : false;

    }

     

    }

     

    ok then i ahve given the code (in one of my pages)like this(in load event)

    if (SessionCheck.Session("Username") == false)

    {

     

    Response.Redirect(
    "SessionExpired.aspx");

    }

    then also it is not working.

    in my web.config file i have given the timeout as 2(just for sample).

    is there any wrong code in my web.config?

    why it is not working?

    where is the error

    plss help me

    Chithra_Iyer
  • Re: redirecting to loginpage on session timeout

    05-08-2008, 7:19 AM
    Answer

    hi chitra,

    have a look at the following link

    http://prajeeshkk.blogspot.com/search/label/Authentication

    here i explained it briefly

    Regards,

    Prajeesh
    ASP.net Developer
    ipix solutions
    (If this post is helpful, please mark as an answer )
    My Blog:http://prajeeshkk.blogspot.com/