Event of browser crash in asp.net - asp.net

I want to execute a action on the event of browser crash in asp.net. I want to destroy the item added in the cart and revert back to the original state of the application and database if the browser crashes.
How can I do this?

Sadly, not possible, due to several factors.
1) the net is stateless, that is to say that the web server has no idea about the web browser, other than that it requested something. After it's serviced the request, it has no way of communicating with the browser.
2) the browser has the same issue. Once it's asked the server for the content and got it, that's it. If the user closes their browser, or lets their session time out, or if the browser crashes, the server doesn't know about it, and the browser doesn't tell it.
3) if the browser crashes, it's crashed, so it can't send any information anywhere, on account of the fact that it isn't working any more.
You can hook into the session_end event, which will fire when a user's session ends (a session ends when the user hasn't asked for anything for a certain period of time, 20 minutes is the default for this), however there's no way to tell WHY the session ended. the users browser may have crashed, they may have closed their browser, or they may have got bored and gone to another site.
Normally I wouldn't update the application state until after a user checks out, so the basket would be the only thing I'd need to delete. To do that I set a datetime column on my cart table, and update it every time the cart is updated. I then run a scheduled task on the database every day that deletes all baskets that are older than the amount of time I keep the baskets for.

Related

SignalR just for checking if user is online or not

I would like to ask, if it is a good idea to use SinglR just for knowing if the current user now online or not?
For example I have an small website with log in system, and some where on the side i would like to show the logged in members.
Is this a good idea to use signalr for that?
And if it the case should I then on each page start the connection with hub? (In this case when user navigates on the pages, will be the ReConnected method called on hub, or OnDisconnected and OnConnected)?
I'm just starting with signalr, so curious what ppl think.
You could use SignalR though there might be better methods to do this. So when a user logs in, logs out or becomes inactive - you would have some sort of message being sent from the client to the server that indicates the change in status. You can store that information in a temporary database and whenever a value in the database changes you can use SignalR to relay that information to all the connected clients.
Signalr will get reconnected when the user moves from one page to another page. Whenever a user logs into a website the user security details will be persisted in a cookie assuming you are using Cookiebase authentication. So till the user logs out or session timesout the cookie will be active. So there is no real need for Signalr here.
I have been investigating the same thing. From my research, I would say that you COULD do this, but I'm on the fence of whether it's the best way to go about it. I would expect a LOT of disconnecting, connecting and reconnecting. If you're persisting this data in a database, you should anticipate a lot of database traffic. if you're only on a single server though, you could just persist this in memory.
Something to also note is that the ConnectionId changes with each page refresh. At first, I thought that was dumb because I wanted the connection id to be consistent so i could keep a handle on a user with it. However, if you open a link in a new tab and then close one of them, you have to still keep the other connection in storage. If the id was the same you would remove it on disconnect even though the other tab was open, so your user would incorrectly be marked as offline.
However, the other issue that i'm thinking about is that if you're just browsing around the site in a single tab, you will disconnect for a split second between each page load. So you might run into connection consistency issues with that.
I'd say online presence with signalr is more common to be used for a chat room or game lobby. So I'd say this is possible, but whether it's a good solution -- i'm unsure.

get number of user in online in asp.net application

I am developing a matrimonial web application in asp.net. In that I want to display number of users in online (logged in).
I am planning to maintain a "login-status table" in dB. And will maintain the table every time the user have logged in and out. But there is problem if the user have closed the browser directly without logging out.
Is there any other easy way to achieve this. Pleas provide sample code.
And I want to know how can I update the table that if the user click the browser's close button.
I recently implemented something similar. The challenge I had is what defines "online". What if the user walks away from the computer, closes the browser, network is disconnected?
In the end I went with online means visited a page in the last 15 minutes. It avoids the issues with trying to detect when they walk away from the browser, or close it.
I did consider binding to the browsers onclose event and hitting a /user/closed/browser url. I have used that before in a call center website to unlock the call. But really the 15 minute rule was "good enough".
I coded it by keeping an in memory dictionary that I persisted to db every 5 minutes. Sorry I have no code to provide.

State managemet through session in online exam website

I am working on an asp.net application. "Online Examination".
my application will uses the concept of session to maintain the state of user who is giving the exam. , since in case browser shut down, system crash i do not want my user should start from the 1st question.
Infact, i want my user should start from where he left. Now i am not able to think on this.
How to test this. Is there any easy way so that i can simply test my concept.
CASE1: USER WANT TO QUIT FROM EXAM. SO HE/SHE CAN INTENTIONALLY CLOSE THE BROWSER.
CASE2: BY MISTAKE USER CLOSE THE BROWSER, SO WHEN HE RETURN BACK HE/SHE WILL START THE EXAM FROM WHERE HE/SHE LEFT.
he just have to enter the url> enter his exam id> continue the exam.
Plssss help me in this respect. give me some idea how to apply session management.
You cannot distinguish between Case1 and Case2. Typically in these scenarios there is a button on the web page like Logout or Finish that would mark the exam as completed. Simply closing the browser would mean that the exam is still in progress.
Also note that you cannot use standard Session state management - that lasts only while the browser is open. You have to store the exam progress data in a database based on the user's ID.
Use application variables so that we can easily remove this when the user suddenly closes the browser or tab.

Web Host has strange settings, need alternative session state suggestions

I am building a web site for a sports club. The club is not interested in moving their hosting to a different web host, so I'm stuck with the current host. I deployed the new web application to the host, but after a few hours of testing it became obvious something was acting weird with regards to logins and the session state.
Whenever I spent more than 10 minutes idle, all of a sudden my MVC Verification Tokens would stop decrypting. I went into IIS and set the machine key to a static value, and this problem went away, but it became obvious that they had the ASP.NET idle timeout setting set to 10 minutes. Which means that every 10 minutes InProc session data would be destroyed if there was no activity on the site. I filed a support ticket with the host, but they are unwilling to change that setting for me on my app pool, stating "The setting can't be changed on the server at this time as increasing this could affect the performance off(sic) the server".
The club is somewhat small, at most 500 members of the club and very few of the members will be accessing the site often, so I can pretty much guarantee that the 10 minute timeout will be happening multiple times a day, and I have session timeouts set to 60 minutes, so it won't be a good idea to have the site clear sessions if the sole user of the site at a given time is idle for 10 minutes.
My first thought was to use SQL session storage. I've never used it, but I know it exists. However, one caveat that exists is that we are limited to 350mb of SQL storage. While this is probably more than the site will need right now, I imagine down the line we might come close to hitting the limit. I don't anticipate storing much data in the session, only a few things when interacting with paypal and shopping carts (maybe a system message here or there as well), so perhaps this isn't a huge concern?
We do have unlimited disk space, so another alternative I thought about was to perhaps write a custom disk-based session storage solution. Is there any reason this might not be a better idea than SQL?
And finally, I had a totally off-the-wall "oh god I can't believe I'm thinking about doing this" idea, which would be to have an AJAX call on every page periodically (say, every 8 minutes) "ping" a non-cached lightweight MVC action just to keep the server alive. This would probably work, I think, except for the one scenario where someone:
goes to the shopping page and creates a cart
gets transferred to paypal
spends 10+ minutes on the paypal screen for whatever reason while nobody else is on the site
comes back to see their cart disappear.
In this instance I might just move carts to the database anyway.
So... thoughts, comments, suggestions? I'm really frustrated about the host not letting me change this setting and my first instinct would be to find another host, but as I said before, the club has some strange attachment to the host and doesn't want to move somewhere else.
Thanks in advance!
A few thoughts:
On the problem: It's common for sessions to be 'lost' on Load Balancing systems. This could possibly be fixed by moving to a single designated server (if the hosting company isn't doing this now and provides the option
Cookie sessions: If you're using .Net Membership, you can change the login process to use cookies. This comes with the obvious some users dont allow cookies pitfall. Again, a consideration. Currently working on a school site where they want users to remain logged in.
** With Cookies your Login is stable, but your sessions will not be any better kept. So you're out of luck carrying much to count on in the Session State.
The end result is that you can track users by IP or Cookie, but unless this is a fixable Load Balancing problem, you're heading toward a Session-less application.

ASP.NET: How to limit access to a page to just one user?

How to restrict the page by accessing only one user at a time. Using asp.net can i use global.asax, is there any other way to restrict, if one user accessing the page, another user not able to access that page. we have to give message that one user is accessing the page. is it possible. can you help me or give some reference.
Although there are probably many better ways of dealing with this sort of problem, I'm going to assume that you do actually need this.
What I would do:
Make your application so that when the page is loaded(when it isn't "locked"), it logs to a database that the page was loaded and "lock" it. In the actual page, I'd have some kind of AJAX to constantly poll the web server every 5-15 seconds to tell your application the user is still on the page. And then make it so that the page becomes unlocked after 5-15 seconds from the time saved to the database by the last AJAX call.
Again, I really suspect that there is a better way around an issue like this, but this is a direct answer to your question
Based on this:
yeah sure, jupaol, it is depend on accounts, in my web application, one report has to approve only one user, but the approve authority having two users. if both of them accessing the same page and approve at a time, it will big mess. here i an not using database.
The problem is related with concurrency, there are several ways to face an issue like this, for example the easiest one is to use optimistic concurrency. Even when you are not using a database for this, you can emulate it.
You should be storing the result of the approvers somewhere, in order to mark the report as approved, with this in mind you should be able to do something like this:
Before render the page get the latest report status
If the report has not been approved, render normally
If the report was approved seconds before, render it in read-only mode reporting who approved it (or similar approach)
Add a validation to your ChangeStatus method, in this method do the following:
Get the latest status of the current report
If the report is still not validated, then block the thread (you could use a Mutex or similar) and mark the report as validate it
If the report was already validate it, raise a domain exception and handle it in your page correctly (perhaps render the page in read-only mode explaining that the report was already validate it)
If you want a more responsive application, (RIA), you might want to consider the following approaches:
Perhaps this would be the worst approach but it's still an option, you could keep a log tracking when a user request your page, then in subsequent requests check if the log is still valid, if it is not, then redirect to another page indicating the page is in use, otherwise allow access to the page. I believe this is an error-prone approach because you would be relying on this simple validation in order to prevent an inconsistency in your system, besides you would have the polling problem described in the following approach
Using AJAX to poll data from a service checking if the report has been approved. Perhaps this is the easiest way to accomplish this but it is not recommended it, because you would be polling your server constantly, and eventually you would have scalability problems
You could use Comet to get notified to the browser (client) whenever a server event has occurred, in this case when your report has been approved. The problem with this approach is that you have to keep an opened connection with the server in order to get notified.
The last approach and the most recommended these days is to use Web Sockets, this is the technology used in StackOverflow to get notifications in real time.

Resources