Click here to Skip to main content
14,735,933 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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");
                    }
                }
            }
        }
    }
   
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
Hemant Singh Rautela 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
Hemant Singh Rautela 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)...???
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
   
v4

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