Destroy Session in an application - asp.net

I have several pages in my application. I have used a session variable called "Session["Variable"]" that is set in page1 and page2. That means The scope should be in page1 and page2. If you go out any of these page will clear the above session variable. Is there any solution to clear the particular session varible in the application level. i.e i don't want to write the code for each and every pages...

Session key once created is accessbile in all pages of the asp.net application and not just within the once where it was added or modified.
use Session.Remove() if you need to explicitly remove a variable/key from session.
Session.Remove("Variable");
Additionally,
Session.RemoveAll(); //Removes all keys from current session.
Session.Abandon(); //Abandon the current session.

If you used MasterPages or derive your pages from a base class, you can use a switch case and determine whether or not the current page is Page1, Page2 or something else. If it is "something else", remove the key from the session.
IE: Switch case in the Page_load of the masterpage

Once you set the session in an asp.net application, the scope of this session is available in everywhere. So, in your case, u can just write one common function for checking the page1 and page2.

Related

SignalR: How to change a value (timer) from different page?

I thought maybe you could push me to the right direction. I'm struggling with a simple issue (hard for me). I have a SignalR countdown page working fine. Now I'm writing a kind of administration page (.aspx) (with start/stop and reset timer functionality), but I'm not able to change the timer/countdown value.
My point here is: I want to change it from a different page.
Many thanks in advance,
N.
store the value as 'global' in cookie or session or localstorage or clientside sqlite or anything depending on the types of client in page1, if page2 wants to change the value in page1, simply modify these global variables. e.g. When page1 starts a timer, check the new value from the global variables first!

Classic ASP Redirecting Title Issue

Just wondering what limitations there are in ASP doing a Server.Transfer two levels? So a page transferring to another page that then transfers to one more page.
Here is our current setup. In an attempt to please SEO, we have created "fake" URLs containing keywords. We then have a 404 error handler (IIS) picking these up, redirecting to another ASP page which pulls out some key information from the URL, and does a Server.Transfer to our "real" page. For reasons outside the scope of this post, it is required that I make a further Server.Transfer from this page. The page we are now on needs to set the page title.
Is this possible?
What you want is certainly possible.
Sure, there are some limitations... but, the limitations are not on the number of server transfers which you plan to daisy chain... Just make sure you don't end up creating a vicious cycle :)
the limitation is as follows;
server.transfer ( and server.execute too for that matter ) cannot access the previous page's variable context.
so if you set a variable say Age=50 in page1 and page1 does a server.transfer to page2, do not expect page2 to know anything about that Age variable declared & set by the page1. In fact, you can even Dim the same variable (Age) in page2, you won't get an error. This is because, neither the .transfer'ed, nor the .execute'd pages work like the [!--include...] files...
So what to do? How do you share information among those pages that you plan to daisy chain using server.transfer? The answer is to use session variables!. That's one effective way.. ( of course you may go out of your way to write to db or text files, but why? )
The only the other thing that your page2, and page3 could share from the the original page1 is the querystring, and post & cookie data! Those request collections will still be available in the transferred ( or executed ) pages.. This means that you can do request("age") in both page2 and page3 if the original page ( page1) was hit as page1.asp?age=99
anyway, coming back to your org. question... what you want is certainly doable...
just don't set any variables in page1, simply work with session variables...
and don't forget to clean up the session vars when you are done on the final page.
hope this helps you...

ASP.NET Page Life Cycle Issues

Apparently I am not familiar with the Life Cycle of a page in ASP.NET. This became apparent when I wanted to dispose of a Session variable after I left the page. I did what made the most sense:
protected void Page_Unload(object sender, EventArgs e)
{
Session.Remove("ServiceSearch");
}
What I didn't know is that this would be called when I go from AND to the page. What I am wanting to do is dispose of that Session variable whenever the user leaves the page. How do I do this?
Page_Unload refers to unloading the Page object right before it is disposed after parsing and creating the page. It has nothing to do with leaving the page. Like #Nick says, there is really no good way to tell that, except to control every exit path. And you can't, because you can't control when the user hits back, or goes to google.com and then pastes in the url they were just at into the browser, etc.
If you want to remove the Session variable just so it doesn't get re-used unintentionally, a better solution is to overwrite the Session variable every time you enter the page, and just let it be disposed with the session on its own time when the session expires.
Session data is useful for storing data beyond the lifetime of a page. If you don't want to store it beyond the life of a page then Session data is not for you here.
If the user leaves the page via a link you could possibly create a link button that is hooked up to a method on the same page. That method would remove the session and do the redirect.
I would hope there is a better solution though. Although, from my understand, there is no page event to use in your case because the page would have to reload to execute the remove session code. When the user leaves via some link the page is not reloaded.
You may possibly be able to handle it via javascript. I've been in situations where I wanted to leave and I got a popup box about some bs. You could probably use the same technique to fire AJAX to remove the session.
I faced a similar situation on php login- a logged-in user could use the arrow back and forth to login page. So, I simply add :
session_start();
session_unset();
session_destroy();
above the html script of the login page so that if the logged-in user arrowed back, the session is both unset and destroyed. Any attempt to arrow forward will only lead to the login page-requesting user login credentials-essentially, the user is logged out any time they arrow back!
Hope this helps!
Alfred

asp.net: how to redirect to homepage when no session exists for web user?

I have a web app with loads of pages and most of them require some session variables in order to function.
i want to put some defensive code in my master page's page_load or init events to detect if the user has a session (meaning any session variable instead of a particular variable) and if not redirect them to the homepage to start all over.
whats the best way to do this? should i use session_end instead?
a simple solution for this would be best.
EDIT:
so i am guessing the master page is the place i want to add this to?
Can you not just go:
if(Session.Count == 0)
{
// no session variables
}
?
You can get the count of session variables and if it equals 0, then redirect.
If Session.Count = 0 Then
'Redirect
End If
The session is created as soon as the user enters your site. To detect if the session was created the way you wanted it, set a property in the session like
Session["is_valid"] = true
Check on other pages if this is set, and if not, Page.Redirect the user to the appropriate page.

How to Track F5/Refresh in ASP.Net

I am using VS 2005, C# 2, ASP.Net 2.0
I am unable to find out how to track that user pressed F5/Ctrl+F5/ Open a new Window(Ctrl + N) in ASP.Net.
I know that there is a Page.IsPostBack property, which tells that a page is loaded in response to an action taken by user.
I am just curious to know, that why isn't there a property as IsRefresh or Page.IsRefresh in ASP.Net, which will return true,
whenever user takes any of the above actions.
Is there a way to know this?
Actually my problem is that i have a DLL using which all of my aspx pages are inherited, I have to
insert some values in a table whenever the page is opened for the first time that's it, if user just opens the page or do not take any action,
an entry should be inserted into the database, but as far as I have tried, I controlled it anyhow using the Page.IsPostBack property, but I got stuck
in the refresh case, as it is inserting records unconditionally.
Similar to using a function in Global.asax (as others have suggested) you could use a session variable "flag". When the page first loads set a session variable and then just check against it in your page load function:
if (Session("visited") != "true"
//page has not been visited, log visit to DB
Just make sure you set the session flag sometime after the above check during the page load.
It won't be exact (sessions can timeout while a page is active, users can completely leave the site and come back in the same browser and the session stays alive, etc) but for your tracking it is much better than counting every page hit in the DB.
Perhaps you want the Session_Start method in the Global.asax file, which will be triggered once at the start of each user session?
In your Global.asax file, add or edit the method:
void Session_Start(object sender, EventArgs e)
{
}
why isn't there a property as IsRefresh or Page.IsRefresh in ASP.Net
Because ASP.NET cannot possibly know. The browser does not send any information that could allow it to determine whether the page is being requested due to a refresh or normal load. You will need to reconsider your requirements: what is the actual purpose of the database logging?
Session_Start method in Global.asax file is fired every time when a browser session is started. You can use this method to count number of unique users on your website.
Session_End method in Global.asax is fired when a session ends (explicitly or timedout). So you can decrement the count here.
Hope the above to example uses of these methods helps you understand how you can use them.
Because of the stateless nature of HTTP protocol there is no way to tell apart the initial load from the refresh
As has already been said. This isn't possible. A request issued due to a refresh is no different to a request issued the first time the page is loaded.
It sounds to me like you are trying to track page views somehow. This is certainly possible though it will require some work on your part. Your best bet is probably to log the URL of the page. You may also want to include the query string in order to differentiate between page loads for different pieces of data (if this happens in your application). You will also want to log the ID of the current user, and the ID of their session.
You can then make sure that you don't insert two page views for the same user for the same page in the same session, effectively filtering out any reloads of a page.
You do need to be aware that this isn't the same as detecting a refresh, what you are detecting is two page views in the same session, this could be a refresh, or it could be use of the back button, or just reloading from the address bar.
My suggestion would be to create a cookie on very first load, then on Page_Load check to see if the cookie exists. If it does, don't insert the record. You can use Session_End to destroy or create the cookie as someone suggested if that works with your application's architecture.

Resources