How can I tell if 2 browser windows are sharing their session? - c#

Many of our users, internal and external, start our web application. Then at some later point, they open a new window from within the browser. They want to have 2 independent sessions of the application running. However, by doing it this way they are actually using the same session data.
Is there a way, in code, to determine if there is another browser window open with the same session?
We're using VS 2008, C# and/or VB.Net.
Thanks.
COMBINING MY RESPONSES FROM BELOW:
Maybe I'm saying this wrong. When they open a second window and change it to a different widget number, and then go back to the original window, on the next post-back it will be using the second window's widget number, not its own
We are using IE7.

The major browsers that I've tested apps on (IE, FF and Google Chrome) all default to using the same collection of cookies regardless of whether you are opening a duplicate web page in a new tab or a new browser instance.
The result is that 2 different tabs, or 2 instances of the same browser, by default, will look like the same session to the server.
Because the multiple instances use the same cookies, the server cannot tell requests from them apart, and will associate them with the same Session data, because they all have the same SessionID, assuming cookie-based SessionID.
Generally there is nothing wrong with this behaviour, and you would have to have a good business case against that behaviour to want to code a work around.
I do not believe it is possible to distinguish the different browser tabs from server side code. There may be some sort of client side script hack that would help.
Would it help to include a Html meta refresh tag so that the various tabs at least update themselves periodically?
If, on the other hand, what you are after is to treat a group of user/server interactions as a kind of "session within a session", you may be able to do this by storing a random Guid (or Widget Number) in ViewState, and checking it on postback.
Hope this helps.

IE8 - shares session between tabs and browser instances; new session can be started using File->New Session command
IE7 - shares session between tabs but not between browser instances
Firefox - shares session between tabs and instances; another Firefox can be started in different profile (firefox.exe -P "profileName" -no-remote) and then have separate session

See http://blogs.msdn.com/ie/archive/2009/05/06/session-cookies-sessionstorage-and-ie8.aspx for discussion of this topic for IE7 and IE8.

They're not sharing the same data. A new session is started in the new browser window and a separate trip to the database is initiated.

You can inspect the headers in Fiddler or you can output the Session.ID in the Windows. Sessions are created for each browser instance, not each window.

Related

Prevent HttpContext.Current.Session being reused on ipad/iphone

I have a system which stores a variable of type SessionService (My class) on the HttpContext.Current.Session
I know there are issues with Chrome reusing the session across tabs and windows.
So I put code in to ask user to close all windows/tabs with the application if the HttpContext.Current.Session["SessionService"] is not null when the application gets to the Index of my Home Controller
This is to avoid issues with users who have access to different logins for the system
This works perfectly on PCs. If I close all tabs then start a new tab, I am able to carry on as normal
However, on an IPAD or IPhone, this doesnt happen and the original session seems to hang around?
Has anyone have it's solution?
I think this must be related to the devices using cached pages. Am i right?

Monitoring changes in web application

I want to monitor changes in background in complex web application. This is one-page application with many scripts and so on. I need to be logged in to have access to data I want to monitor.
I tried to use webrequest, but I think that the application is to complex to do it that way. There is also a problem with authentication.
I also tried WebBrowser component, but web application is telling me, that this browser is too old and I should get newer one.
Perfect solution would:
Open this web application in chrome (or some other modern browser) in background
Save the page to memory
Extract values using something like HtmlAgilityPack
While this will be happening I want to normally use the computer (so opening chrome window is not a good solution for me).
Is there any way to achieve something like that?
if you can cope with an extra browser running, have a look at SeleniumHQ. with its webdriver-backed selenium you can start a dedicated browser instance and perform user actions by coding in high-level programming languages like java. it should not interfere your manual work at all, but will take up the same amount of memory and cpu time your "real" browser would.
if the web application has no captcha and does not object to automated script accessing it, you could also login in a background program by sending appropriate HTTP requests and parse the response. python's urllib2 would be my first choice.
if you dont want any additional processes running, you could also create a browser plugin, that autorefreshs and parses a certain open tab every few seconds.

Website scraper to the next level

So!
For a fansite I run I also run a website scraper(/xmleader) that reads information from a secure weblocation of a game. It works perfectly as it is now but I want to make it better and mainly faster.
The 1st problem I faced was how to maintain a session where you can do a ton of requests (like 1 to 10 every 30 seconds) while maintaining logged in. Normal httprequest didn't really worked because the login was secured with a token that must be submitted together with my login information. Now the solution was made as followed: On a Form is just placed a webbrowser control and when the login page was loaded(documentCompleted event) I fill the login information inside the document and simply submit it.
Now I can access all the secure pages I want to BUT not with a HttpWebRequest I placed inside the code. BUT When I placed multiple WebBrowserControls on the same form all them could access the secure part of the site. So that is how I placed 6 of them to do -kind of- parallel requests (for xml and html) to access information in my account quickly.
This works like a charm actually, you nicely see 7 browsers browse away and analyse the domdocument but naturally this creates a lot of overhead since I don't need the images and all the flash etc to load (or the iFrames which cause very annoying multiple documentLoaded events). So I want to login once and be able to request inside the code with HttpWebRequest with the session/cookie information of the webbrowser(or login in some other way).
So how do I do this? Is this even possible or should I approach it completely differently ?
(ps I write everything in C#)
You can show the first WebBrowser, login and, after the submit, you get the cookies from it and attach them all over your HttpWebRequests.
Having only the WebBrowser shown for the first login should improve your performance a lot!
Only pay attention to browser validation / async content loading.
You can't use HttpWebRequests to share the same session with WebBrowser. You'd need to use an API based on UrlMon or WinInet, that's what WebBrowser uses behind the scene.
I listed some of the options here: https://stackoverflow.com/a/22686805/1768303.
Perhaps, the XMLHTTPRequest COM object would be the most feasible one.

Why does my new .Net 4.0 EF application randomly lock up?

I have recently rolled out a new ASP .Net application. This application uses .Net 4.0 and the Entity Framework 4. It is a "basic" CRUD application. I almost exclusively used the EntityDataSource for Data Access.
The users have reported "ocassional freezing" of the application (they all use IE 8). If the user closes his browser and starts back up, the app is fine. It happens maybe once or twice per day total, usually to the heavy users. (So, I have been leaning toward memory leack or some other kind of resource limitation.)
Until just a little while ago, I have never seen this happen. It dosn't leave anything in the Event Log on the client PC nor Server. Also, there is nothing in the IIS error logs or the SQL logs.
A little while ago, I finally got it to happen. I was switching all of my EntityDataSources to use a single Object Context per page. I downloaded the EF Profiler (which seems to be a great product) and it pointed out that I am using multiple object contexts on many of my pages, due to the fact that many of them have more than one EntityDataSource. So, I was cleaning all this up and testing each page, when lo and behold....IE stopped loading pages!
At first, the page started loading slowly (and this was a page that I had already modified to use a single Object Context, so although I still believe I need to implement this "singleton" pattern throughout all my pages, I guess it isn't the culprit! Anyway..), then at some point I received:
Message: Sys.WebForms.PageRequestManagerTimeoutException: The server request timed out.
Still, nothing in the Event Log. I also checked the IIS web logs and I don't see anything, but they are pretty tough to read with the naked eye, I may have to download a parser to make sure there is nothing in there.
Anyway, I decided to start up another window and navigate to my app, and sure enough that window worked fine, no problems. I went back to the windows that stopped responding, refreshed it, waited... went to the home page of my app (which is basically just some static html.. and nothing.. ). Next, I opened a new tab in the window that is currently frozen and that tab works fine. So, I go back to the window that can no longer load pages from my app and try some other web sites.... espn.com loads fine, msn.com loads fine, I then enter the URL for my app home page (and tried some other pages in my app) and NOTHING. IIS doesn't even log the attempts in its Log Files (its like IE has just decided not to do anymore "gets" for my localhost!). Finally, after waiting for a while (while I was typing this post) I now have this error message in that window:
Internet Explorer cannot display the webpage. What you can try: The problem can be caused by a variety of issues, including: BLAH BLAH BLAH...
So, I jump back to that window and it can still browse Internet sites just fine. But, once again when I try to go to any of the pages in my app, I get NOTHING (spinning blue circle in the tab). So, once again I start a new tab in that same browser and I can browse around my application just fine.
What has my app done to this browser instance that will no longer let it load any of its pages (in fact, it seems like it isn't even doing a GET on any of the apps pages, since there is nothing new in the IIS logs...except for when I load the pages in these other tabs and/or browser windows that work fine, so the logging is still working...)? I will be forever indebted to the person that can help me figure this out. I am not sure it will do much good, but I am going to leave this browser window up on the chance that someone can tell me some things to try to diagnose this problem. It feel like I am THIS close to figuring it out. I finally have a window up that is misbehaving. I am afraid to close the window or do a new build on the app until I get some good input on things to try....
I should add that when this happened to me, I was running on my local PC, so I was the only user and only had one window open to the app..
Although, I am completely stumped!
We had the same issue and seemed to fix it by adding the following JavaScript to all pages with ajax:
window.onunload = abortRequest;
function abortRequest() {
Sys.WebForms.PageRequestManager.getInstance().abortPostBack();
}
I hope this helps!
sounds like a request just takes too long to execute. A web-browser is only allowed to keep two concurrent connections to a single webserver unless you utilize http pipelining (which isn't well supported anyway). However, My experience is that this is per browser, not per tab.
This is why nothing shows up in your logs, because your browser never initiate a request because it is waiting for the page to finish loading before it can fetch another page. I would start looking into the last request initiated before the lockup, it is probably that page that locks for some reason and never finishes.
ASP.NET utilizes only one thread as long as all request are sequential. As soon as you fire up two request simultaneously, a new thread is initiated (Init() on HttpApplication is invoked), and that can cause intermittent errors if you do funky stuff on Init().
If you think this is related to EF, you might read up on how to avoid locking in multi-threaded environment (because a web application is very much multi-threaded however this usually kicks in a production environment).
Ugh, I feel your pain - I hate these types of issues. Here's my divide-n-conquer process when something like this is happening (adapted somewhat to your case):
Step 1 - Environment Visibility
Get yourself set up so you can roughly see inside your systems. Set up Performance Monitor, and add a couple of important counters (IIS Current Requests, ASP.NET Current Requests, SQL Server - Active Transactions). The idea here with the "active requests/transactions" counters is you want to find out which part of the system is holding on to the request.
Additionally, add counters for total request time in IIS and the DB (so you can see it climbing as something is hanging on).
On your client PC, set up Fiddler.
Step 2 - Reproduce Issue
Usually at this point, the application realizes you can see it and starts behaving perfectly. ;) Stop and make sure you can still reproduce the issue.
Step 3 - Take the DB out of the equation
Create 4 pages in your application:
Static HTML file (.htm) that will NOT go through the ASP.NET runtime engine. This is your baseline for raw IIS.
ASPX page with no tricks (no database access, no processing, just a simple page that ASP.NET can spit out)
ASPX page with one simple database call (of some size, maybe do a single big query or something)
ASPX page with some complex transactions (multiple queries, etc)
Step 4 - Reproduce and measure
Since this issue will probably be pretty obvious once you can see it, find the bottleneck with your performance counters and Fiddler. Your issue sounds like you could have an intermittent connectivity issue (either between client and web server or web server/SQL), especially if IE is giving you the "page cannot be displayed" error, but you should see that in Fiddler. On the server side, you should see one of those performance counters hanging.
well, sounds like the app is waiting for a database operation to complete but is locked, probably because of another operation.
Try using the SQL profiler to trace the activity, and isolate what processes are running for a long time.
You can differentiate the requests using the SPID column in the trace view, it contains a unique ID number for each session from your App.
I have new information regarding this issue.
The application uses Integrated Authentication. This causes every reqest to come back (twice) with access denied, before IE send the credentials.
I can't help but wonder if this is what could be causing the "time outs".
I implemented an Ajax "Loading" animation on every page that prevents the user from clicking a button until the previous request is completed and that has GREATLY diminished the amount of "freezes" the users are experiencing.
Although, it still happens somewhat often.

Stumped. (C#, JSP)

So long story short, the company I work for, that is all about saving money, went from 1 service application, to 2 service web interfaces.
They're not secure, have no cert's, not even over SSL...yet, they have a timeout associated with each interface. Which then breaks the page you're viewing, and you have to refresh IE, and then proceed to login again, and reset your dropdowns etc...
So.
I've tried using a generic webbrowser interface in C# to have a foundation in which I can refresh the connection to the server with the page open, as to avert the timeout.
But, first attempt results in my app, going to the appropriate page, then opening a new IE window when a service ticket is opened, and there must be a lack of cached data, which results in the new IE window that opens, to revert back to the main page which prompts me to login.
The interfaces are Jsp, but I'm more familiar with c#...
Tried catching link clicks to navigate the destination in the current window, but it seems that the data you double click that prompts the new page to open, is merely a data in a table, and not technically a "link"....
I've brought this to the attention of management, but like most huge corporations, I know nothing, and they never have to deal with it, so as they say in every meeting, "silence means compliance"....
Any thoughts?
Probably not the right answer, but...
Session cookies?
Session cookies = fail..
No 'remember me' option...
I can however enter a link in each browser to "goto the home" section of each app, which each page is already at, so it keeps the connection alive, and doesn't navigate away from the current page (keeping the dropdowns etc that are set after login)
But still, the above problem exists where if I use all this in a C# browser, etc...
I told my boss first thing 'they can't tell me the timeout is for security!' heh
And I still didn't get a positive answer from 'well at 10 seconds per re-logon, per x amount of people = thousands of dollars a year spent on re-logging on...'
:D
Use ajax.
poll a server page in a small interval (a minute)
if it returns "timeouted=true" (or whatever convention you like), refresh the page with javascript.
include the ajax snippet on every page (using <jsp:include>, for example)

Categories