Click here to Skip to main content
12,500,915 members (48,192 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET4
i have one doubt I am storing username in session variable if session timesout it should automatically return to login page I am not getting please suggest me how to write code i kept sliding expiration= false. but it is not working
Posted 27-Dec-12 3:57am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

So , in your web config make next changes :
<system.web>
<compilation debug="true" />
<authentication mode="Forms" />
<sessionstate mode="InProc" cookieless="false" timeout="1">
</sessionstate>
</system.web>

Add Global.asax class file in root of your application or website.
This method works only if Global.asax is present in application.

you can also put this code in in a class and inherit all pages of application from this class acting as base class for all pages to check for session timeout.

protected void Page_Init(object sender, EventArgs e)
    {
        if (Context.Session != null)
        {
            if (Session.IsNewSession)
            {
                HttpCookie newSessionIdCookie = Request.Cookies["ASP.NET_SessionId"];
                if (newSessionIdCookie != null)
                {
                    string newSessionIdCookieValue = newSessionIdCookie.Value;
                    if (newSessionIdCookieValue != string.Empty)
                    {
                        // This means Session was timed Out and New Session was started
                        Response.Redirect("Login.aspx");
                    }
                }
            }
        }
    }
  Permalink  
Comments
ridoy 27-Dec-12 15:01pm
   
+5
Oleksandr Kulchytskyi 27-Dec-12 15:07pm
   
thanks !)
shankar1989 6-Sep-14 1:26am
   
its not coming on page init after session timout
hemantrautela 10-Jan-13 8:49am
   
I have little confusion,
What will happen if I used some session variables for all users, which are required for my web functionality...???

This functionality checks any one session or a specific session(which one & when/where created it)...???
Oleksandr Kulchytskyi 10-Jan-13 9:00am
   
What will happen if I used some session variables for all users????
What do you mean?
if you add some data to Session storage this data will be only avaliable for 1 user whic issued this session!!
if you want to access to some variable storage for more than only 1 user use Application level storage instead of Session
hemantrautela 10-Jan-13 9:16am
   
some mistakes in my question.. it is not for all users , it is other users who need not login...

My main query regarding for... my 2nd point ..
This functionality checks any one session or a specific session(which one & when/where created it)...???
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

As you told you stored username in Session variable like
Session["username"]="somevalue";

now each page_load() where you want only authorized person go there, you can check its value as

if(Session["username"]==null)
{
Response.Redirect("login.aspx");
}
else
{
// you can acess its value, I think you need.
string uname = Session["username"].ToString();
}

..IF THIS SOLUTION IS NOT GOOD ENOUGH PLEASE GIVE REASON WHY..??
...IF YOU DOWN VOTE THEN GIVE ME REASON ALSO...

Thanks
Developers Blog[^]
Hemant Singh Rautela
  Permalink  
v4

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160919.1 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100