Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to auto-redirect to ReLogin page when session time outs.

In web.config file, i have the following code

XML
<configuration>
    <system.web>
       <sessionState timeout="1" cookieless="false" mode="InProc"></sessionState>  
    </system.web>
</configuration>


In Global.asax file-

C#
protected void Session_End(object sender, EventArgs e)
{
    HttpContext.Current.Response.Redirect("~/ReLogin.aspx");
}


I am getting this ERROR:

Object reference not set to an instance of an object.
Posted
Updated 17-May-16 22:51pm
v3

Hi,

You can implement Form authentication which is cookie based and cookie will expire after the timeout set in config file and user will be redirected to the given page.

Refer below links for implementing form based authentication.

http://support.microsoft.com/kb/301240[^]
Understanding and Implementing ASP.NET Custom Forms Authentication[^]
Role-based Security with Forms Authentication[^]

Or set user session after login and check user session in each page or create a base class in each page in which you can check user session.

Hope this will help you.

or

for more info go through below link

Session Time Out Warning Message Using jQuery in ASP.NET
 
Share this answer
 
1.The session of your application will be ended by the web browser and your HttpContext object will become null.

2.For doing like you want you could use, in your Globas.asax.cs, Application_Error event to manage the unhandled error like in the next example:
C#
void Application_Error(object sender, EventArgs e)
{
    Exception ex = this.Server.GetLastError().GetBaseException();
    MyEventLog.LogException(ex); //Log the error in the Event Log!
    //
    FormsAuthentication.SignOut();
    this.Response.Redirect("Default.aspx?Error=1");//Here you can change with your URL!
}

3.You can find more details about "Exceptions Management" for an ASP.NET MVC application, but is applicable also for an ASX application, in my next article: MVC Basic Site: Step 2 - Exceptions Management[^]
 
Share this answer
 
v2
Comments
[no name] 25-Aug-14 7:21am    
Its not working :(
Raul Iloc 25-Aug-14 7:24am    
1.Note that Application_Error will be generated when the user will try to access your application after the session was expired.

2.What did you try and is not working?
[no name] 25-Aug-14 7:28am    
The code which u have written i added that in Application_Error and i am not using MVC Application
Raul Iloc 25-Aug-14 7:30am    
The code that I gave you is not form an MVC projext, is from an ASPX project and in my application is working. Note that you should change for your context and read also my 1st point from my comment above!
[no name] 25-Aug-14 7:43am    
i am getting error likr MyEventLog is not exit
you cannot user Response.Redirect or Server.Tranfer inside Global.asax

you can try this instead(on master page)

C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserId"] == null)
                Response.Redirect(" /HomePage.aspx");
        }
    }
 
Share this answer
 
Comments
[no name] 25-Aug-14 7:21am    
I am using iframe, if i redirecting the page to relogin.aspx in master page its opening the relogin page in iframe only
The easiest way what I feel is to use Meta information and get the trick working. Consider we have a page WebPage.aspx add the below code in the the WebPage.aspx.cs file.

C#
private void Page_Load(object sender, System.EventArgs e){      
    Response.AddHeader("Refresh",Convert.ToString((Session.Timeout * 60) + 5));      
    if(Session["IsUserValid"].ToString()=="")              
    Server.Transfer("Relogin.aspx");
}


In the above code, The WebPage.aspx is refreshed after 5 seconds once the Session is expired. And in the page load the session is validated, as the session is no more valid. The page is redirected to the Re-Login page. Every post-back to the server will refresh the session and the same will be updated in the Meta information of the WebPage.aspx.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900