Place a Timer and Monitor and Take Certain Action Upon its expiry - asp.net

I'm developing an online quiz site where the users can take tests (multiple choice). I have a requirement to put a timer on the test say if the maker of the test sets the time to 30min a timer would be placed on top of the page counting down the time, now i want to monitor the timer on the server side when it reaches to 00:00 the test would stop and the marks obtained is shown.
Solutions i have considered:
1) To use a JS plugin and upon its expiry do a location.href to some ActionResult that will end the test.
2) To create a session and clear it after the given time and before rendering each question check that session if it has expired or not.
My concern about using a javascript based solution is its robustness, i want to implement a server side solution. Please help me find the right path...
Regards.

When using the timer in the browser there is no way to verify that it is running as expected. User can disable javascript and get around your restrictions; heck they can even change your javascript. So relying solely on javascript is not an option.
The best solution is to validate postbacks on server side. You can use javascript on Client side to inform the user so that they are aware of how much time is left and server side to verify they have not gone over the time limit. If synchronization is required, then you can have the javscript ping back every so often to resynch the time.
Expanded Answer
The time of the start of the test should be on the server side. Whenever the user clicks begin test or other start buttton to begin their test. The server should note the current time - perhaps in a session or even in database.
The user should then be informed of the time remaining via javascript and expected end time. These are then updated whenever the user submits a question or perform another postback to the server.
The end time will be the fuzzy part. If the javascript does it's job correctly; you can inform the user their test has ended through this (or window.href etc). If the javascript is not worked; then the user will receive their notification whenever they next attempt to submit something to the server.
Either way, the user should clearly be informed that this is the expected end time, and how much time they have remaining on each page load.

Related

Session times out although the user has been typing in the form

In my asp.net web application, the session is set to its default timeout value. As far as I understand, the timeout starts to count the minutes as soon as the application is idle and no action is done on it. However, when i open up a form and start filling it, if I take longer than 20 minutes to fill up the form then when i press "Save" the application logs out. I'm not inactive, but actually i'm filling up data in the form, so I don't want the session to timeout.
Is there a way to let the session only start counting the time when there is no action at all and let it "sense" the typing of the user ?
What you are looking for is called a "heartbeat". There are a number of ways of achieving this - the easiest will depend on what other assets you already have in your page - jQuery, AJAX controls, whatever.
The basic premise is that it sends, in the background, a request to the server effectively saying "I am still here" to the server to stop the timeout from happening. Ideally your solution should be checking for onkeypress in the javascript to ensure a user is there and filling in the form.
A search for "heartbeat form filling javascript asp.net" leads to a few sample ideas.

How to keep session alive across multiple tab if one of the tab is active and make it die if all tab is inactivity

The user is accessing the website and opened multiple tab.
The session should keep alive when:
Any of the tab is active (mouse move & key strokes on keyboard).
The session should die and redirect to login page automatically when:
All the tabs is inactivity (no mouse move, no key strokes)
Can you please hint me how to do this?
It's a dirty solution, and I haven't tried it yet, but this could work in theory:
When you have detected activity on the page, save a flag in a database table to denote that there is activity on the page (probably also want the timestamp to help you determine how long ago the activity happened and the user identifier to make sure it's the right session). Depending on how often you want to make this save, you could use a simple script like this (https://stackoverflow.com/a/3877867/987905) to keep track of the most recent keystroke or whatever you want to track, then after a certain amount of time (say 5 minutes), decide to save it to a database if there ever was activity (that way you aren't making database saves every time there is activity detection).
During your check to see if the session is active, check the database for the flag and timestamp to see if the session has been reported as alive on any instances of that user being logged in. If so, re-activate the session via your keep alive, or re-authenticate.
Again, I haven't tried this, but have been thinking about it recently. It seems to be a viable solution, just comes with the downside of making database saves/calls behind the scenes, possibly hurting performance.

How to handle concurrency in an ASP.Net WebSite? (auctioneer site)

my problem is follow:
I have an auctioneer site, in which many different objects will be auctioneerd.
My problem is very simple to clear for an more experience user I thinK. How I can handle business and database logic without opened a site or them?
My problem is to say directly, if nights at 3 no user is on the site, the winner (e.g.) must be set - if a page is opened or not.
So I need some kind of "every 2 seconds, do this method" - without opened a site.
My idea was a sepereate application which uses the same business and database-layer as the asp.net page and let this run at the server. Is that a good or bad idea?
The separate process (scheduled app or Windows service) is the only reliable way you can achieve this.
Using the same BLL and DAL are exactly the right thing to do too.
Check out this article on windows services http://msdn.microsoft.com/en-us/library/aa984074(VS.71).aspx
To let every user query the database every 2 seconds would create unnecessary traffic on your site, which is not a good idea. (users tend to refresh the page many times just before the auction closes anyway)
My thoughts:
Add a date to the auction when it closes.
The last user that places a bid is always the winner, and you can't place bids after 3 am (the date the auction closes), so if you visit the site after 3 am (you can't place a bid and) the winning user is displayed. If somebody opens the site just before 3 am and places a bid after 3 am your business logic should check the date of the bid and deny it.. (also: users might live in different timezones so consider displaying the server time on your site).
Setting the date to 'now' would close the auction immediately.
You can also add javascript to refresh the page if your clock passes the hour or something like that. (or use the number of seconds left before the auction closes in a setTimeout function or metarefresh)

How to alert a user that session will be expiring soon in ASP.NET?

I have a scenario where I would like to display a message to the users of my application alerting them that his session will expire soon and that he should save his work to avoid the loss of his work.
This would be a typical scenario where a user is typing information on a textbox and then leaves his computer unattended for a while. Then when he presses a submit button to save his work, he is redirected to login page because his session expired, thus having to type all his information again in the textbox.
How can an alert message be displayed on the screen using ASP.NET 2.0/3.5?
Thanks in advance for your guidance.
G.
Reminders are not going to be much use if the user has left their computer for any period of time over 20 mins. The session will still time out. You'd be better off keeping the session alive:
AH, Ah, ah, ah…Staying Alive…Staying Alive
To answer my own question, looks like the best way to accomplish this is to use a setTimeout javascript function to alert a reminder X mins before the session timeout.
One way to do this is by registing ClientScript.
Pass the session expiry value to the client side java script. Which will execute a countdown, and at the end display the Popup.

Timing User Access in ASP.NET MVC, Beginner

In ASP.NET MVC, what is a good way (the preferred way??) to time how long a user has been on a specific page? For example, I want the user to select something and then only allow the user to do something for 30 seconds. Good links or a reference to a page of a book would be much appreciated.
Thanks in advance!
You can keep track of when a user went somewhere and how long they have been there (or had the page open while balancing their check book...you get the idea). The problem is that you need some form of client controller to let them see XYZ for 30 seconds...and then redirect them to the next page that they can see. So if you wan the user to see a resource for X amount of time you need to employ a javascript client side timer to take them away from the resource when their time is expired. This can be done with the time statically coded to the client (which could be changed by the client) or it can be done by making an AJAX request to the server to see if the time has expired. Or it could be done with an embeded flash player. The key here is that your server side doesn't have as much power over the client side as what you are requiring. Most testing sites deploy some form of this client side javascript to keep track of what the user is doing, when, and for how long!
One pretty easy way is to store the last time the user was on the page in a database - the table could have, for example, the fields UserID, Page, and TimeStamp. Whenever the user tries to do whatever you only want to allow for 30 secs, you check against the database if the time has passed or not. (For such short periods of time as 30 seconds, a database might be a little too slow, though... Depends on your requirement of precision, I guess...).
You could use JavaScript's setTimeout() function:
var timeOut = function() {
alert('Time is up!');
}
setTimeout (timeOut, 30000);
Or you could use a <meta> tag:
<meta http-equiv="refresh" content="30;url=http://www.example.com/time-is-up.html">

Resources