Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 3rd, 2007, 07:22 AM
Authorized User
 
Join Date: Jan 2006
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to redirect to logout page when session expire

When the session timeout in the webconfig is reached which is in inproc mode how do i redirect to logout page from global.asax
thanks in advance

__________________
Regards,
P.Madhusudhana Rao
 
Old April 3rd, 2007, 08:04 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You can't.

Only when the browser requests another page could you issue any kind of page redirect.

Usually this type of behavior is achieved through the use of a client side javascript timeout. If the browser sits on a page for the time that your timeout is configured for, it automatically changes the browser's URL to your logout page.

Something more or less like this:

<script language="javascript">
  setTimeout("location.href='logout.aspx'", 1200000); //20 minutes x 60 x 1000 milliseconds
</script>

-Peter
 
Old April 3rd, 2007, 08:21 AM
Authorized User
 
Join Date: Jan 2006
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok when the session ends an error occurs "response not availble in this context"
why is this happening,and when i click on otherpages it normally opens up though the session ends how can we handle that my code looks like this

protected void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs
        Exception ex = Server.GetLastError();
        ms_logger.Error(ex.Message, ex);
        if(Context!=null)
            Context.ClearError();
        ms_logger.Debug("Forwarding to ErrorPage.aspx");
        Response.Redirect(AppConstants.ERROR_URL, true);
    }
protected void Session_End(object sender, EventArgs e)
    {
       Response.Redirect(AppConstants.LOGOUT_URL, true);
    }
I am getting error near line "Response.Redirect(AppConstants.ERROR_URL, true)"
as response not available in this context and when i skip that error its been in the same page and later works normally when clicked on other pages irrespective of session time out.

my point is that when session times out atleast when i click on otherpages it should take me to loginpage but not happening here. the same set of code works in asp.net1.1

Thank you planoie waiting for more help


 
Old April 3rd, 2007, 08:29 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

You can't use the Response object inside of the Global.asax like this. (Global doesn't inherit from Page) You may be able to use HttpCurrent.Context.Response.Redirect() or Server.Transfer() to do what you need to do, however, you should *never* rely on the Session_End event to preform a task. (Especially when its a function that you rely on for you application to function properly.)

This has been discussed at great extent in various other threads throughout the forum so I won't go into great detail, but basically, there is NOT a 100% gurantee that the Session_End event will be raised. (If you use StateServer or SQLServer for State management, the Session_End event is NEVER fired)

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old April 3rd, 2007, 08:31 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

When the browser is not actively loading a page it is not connected to the server. As far as the server is concerned there is no request. Therefore, there is no response. If a session times out, it is because there has been no activity from the client browser. So you simply can't "push" a redirect to a browser that isn't connected. That is why you have to use some clientside script. If the browser is still open on the user's machine, then the client-side javascript timeout will fire after the set amount of time. If they close the browser, then they have manually ended their session (even though they didn't tell the server about it).

When you make a page request from the browser to the server after a session has timed out, the server just creates another session for you. It has no way of knowing that the request is coming from a new client or a timed out client. The only way for you to determine that is to use additional information. It sounds like you should look into forms authentication this handles some of this automatically. One critical thing to understand however is that you CAN have a user who is authenticated but who has an expired session. Forms authentication is stored in a non-persistent, non-expiring cookie that exists for the life of the browser whereas the session timeout is managed by the server and well eventually expire.

-Peter
 
Old April 3rd, 2007, 08:39 AM
Authorized User
 
Join Date: Jan 2006
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi dparsons i have been using HttpCurrent.Context.Response.Redirect() also tried with server.transfer but couldnot resolve it its not at all going to the logout or error page but the same bit of code was working fine in 1.1 when a session timeout occurs when we open the minimized window it will be in logout page.

thanks dparsons for letting me know that a good concept

 
Old April 3rd, 2007, 08:47 AM
Authorized User
 
Join Date: Jan 2006
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok i will give the global.asax.cs and web.config files its the same in both 1.1 application and 2.0 but still it is not working in 2.0 here is the code files

I have added a Global.asax.cs in the App_code folder and this file looks like this

public class Global : System.Web.HttpApplication
{
    private static ILog ms_logger = LogManager.GetLogger(typeof(Global));
    private System.ComponentModel.IContainer components = null;

    public Global()
    {
        InitializeComponent();
    }

    protected void Application_Start(object sender, EventArgs e)
    {
        log4net.Config.DOMConfigurator.Configure();
    }

    protected void Application_End(object sender, EventArgs e)
    {
    }

    protected void Application_Error(object sender, EventArgs e)
    {
        Exception ex = Server.GetLastError();
        ms_logger.Error(ex.Message, ex);
        if(Context!=null)
            Context.ClearError();
        ms_logger.Debug("Forwarding to ErrorPage.aspx");
        Response.Redirect(AppConstants.ERROR_URL, true);
    }

    protected void Session_Start(object sender, EventArgs e)
    {
        ms_logger.Debug("Starting new session");
    }

    protected void Session_End(object sender, EventArgs e)
    {
        Response.Redirect(AppConstants.LOGOUT_URL, true);
    }

    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
    }

And in the web.config file i have added the session state as inpoc and it looks like this

<sessionState
            mode="InProc"
            stateConnectionString="tcpip=127.0.0.1:42424"
            sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
            cookieless="false"
            timeout="1">
        </sessionState>

wantedly i have configured timeout to 1 min.
after one min i am getting error in global.asax.cs near the context.clearall but i added if context!=null and it was fine now i am getting near Response.Redirect(AppConstants.ERROR_URL, true),
but the same code worked in the 1.1 application and when session times out it takes me to logout page but in 2.0 its not happening remaining in the same page after session timeout.


 
Old April 3rd, 2007, 09:57 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

As Peter pointed out, when Session_OnEnd occurs it is because there has been no client activity over the course of X period of time.

Again, as Peter pointed out a timeout can occur because there has been no activity in the browser or the user has closed their browser. Your session is dead at this point and the server has no way of knowing which browser needs to go where.

Why this would works for you in 1.1 I am not sure but I suggest that you use Peters advice and use a client side Javascript.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old April 3rd, 2007, 10:04 AM
Authorized User
 
Join Date: Jan 2006
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hai Peter and dparson,

   I finally found out based on your clientside javascript i have missed that javascript which is in a user control in the 1.1 that user control i am not using here


Thank your very much for ur good time spending to clarify my doughts.

 
Old April 3rd, 2007, 02:30 PM
Authorized User
 
Join Date: Jan 2007
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi madhusrp,

Can you please share that script with us. I am having same problem and your script might help me as well.

Tahir





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to expire previous page johnsonlim026 ASP.NET 1.0 and 1.1 Basics 6 May 22nd, 2009 12:05 PM
identify which session variable expire neon20 Classic ASP Basics 0 September 4th, 2007 05:59 AM
what is session id and when does it expire nidhimittal PHP How-To 3 June 29th, 2006 05:17 AM
what is session id and when does it expire nidhimittal Beginning PHP 0 June 26th, 2006 07:20 AM
Page Expire Chris Marsh VS.NET 2002/2003 1 April 4th, 2004 12:02 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.