Click here to Skip to main content
Sign Up to vote bad
good
See more: ASP.Net-4.0
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:57


2 solutions

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:01
+5
Oleksandr Kulchytskyi - 27 Dec '12 - 15:07
thanks !)
hemantrautela - 10 Jan '13 - 8:49
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:00
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:16
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)...???
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  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 415
1 OriginalGriff 325
2 Arun Vasu 268
3 CPallini 213
4 Tadit Dash 203
0 Sergey Alexandrovich Kryukov 10,005
1 OriginalGriff 7,654
2 CPallini 4,171
3 Rohan Leuva 3,447
4 Maciej Los 2,974


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