简体   繁体   中英

how to restore session in asp.net

I made a website in .net 4.0, In which user first login and redirect to dashboard page, After login there are 8 pages in user panel and a logout button in master page. I want to store on the page on which user logout and also when next time he login, his session will be restored(redirect to same page from where he logout) . anyone can give me any hint ??

You are confusing sessions with Session s... Session is an ASP.NET class that is available to every page provided you have Session support activated in your application (it is on by default, and stores its values in memory by default). But these Session s time out; after a certain period of inactivity -- say, 15 minutes -- the server clears the memory.

What you mean is just a session: the user logs in, does stuff and then logs out.

In order to automatically return to the last page he was on when he logged out, all you need to do is have the Log out button's code store the Request.Uri.AbsoluteUri string in the database. So just have that button's Click event handler in your codebehind store that value along with the user's ID.

Then the code behind of the Login button's Click event can simply retrieve that value and Response.Redirect to that Uri.

You could use:

Request.Url.AbsoluteUri

And store that in a session.

Session["LastVisitedPage_" + userId] = Request.Url.AbsoluteUri;

You can use Request.Url to get the url of the current page. When the user clicks on the log out button, store the current url in the database. Next time the user logs in, retrieve the url from the database and redirect to it.

To logout:

 Session.abandon();

To store session:

 Session["name"] = Request.Url.AbsoluteUri;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM