Timing User Access in ASP.NET MVC, Beginner - asp.net

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">

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.

Place a Timer and Monitor and Take Certain Action Upon its expiry

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.

Is it possible to fetch session information based on session ID in asp.net?

Is there a way that in a web page within the asp.net web application, show all the active sessions and the time remaining before log out? I also want to be able to manipulate these sessions (force time out, extend time out etc) from one centralised location.The session is in proc.
Or in other words, is there a class that exposes all the sessions within the application?
Imran Baloch's gives you an excellent option in this URL:
http://weblogs.asp.net/imranbaloch/archive/2010/04/05/reading-all-users-session.aspx
This is a little tricky but it does the work.
Hope it helps.

Tracking over 500 events per session

I'm about to launch a web-site very soon, and I want to track over 500 actions per session. Basicall, I want to track each hover-over, and Google Analytics only supports 500 actions per session. What is the best way to do this reliably but also a relatively inexpensive solution. I'm thinking of something like this...
Batch up events on the client
Send via ajax every N events or seconds
Write to a pretty cheap but reliable persistent store
To batch up the events, simple push them into your own javascript object, maybe with the same signature:
yourEventArray.push(['Category','Action', 'Label']);
Create a function that gets called on some interval:
...page load event...
var gaqTimeout = setTimeout(uploadEvent, 100);
function uploadEvent() {
if (yourEventArray.length > 0) {
// do something like loop through items in array and pop them out once added to _gaq
}
}
How does this apply? If you're talking about queuing GA events to the server for later processing, you should really think about finding a better solution that handles your specific situation.
UPDATE - based on your comment
Other products that I familiar with that do a good job tracking hover events and mouse events:
ClickPath
You could use Piwik with Custom Variables as Events
Yahoo! web analytics
Lastly, check out this great post that contrasts 3 of the most popular Web Analytics tools and their limitations.
My 2 cents, hope it gets you in the right direction!
Mixpanel is based on events and there are no limits. It is not free, but it is awesome for this use case.
Another option is to study the Measurement Protocol in Google's Universal Analytics. You could define all your actions as virtual pageviews, and have up to 10M a month.
See also the Limits and Quotas.
The limit you describe, of 500 events per session, may not actually be that big of a deal if you re-think it a bit. Realize that a Google Analytics session is (most likely) not the same thing as your user logging into your app and doing things.
You might want to try setting a custom dimension of "LoggedIn" to "Yes" when someone logs in, but "No" otherwise. Send this CD with every pageview and event. This is a more typical idea of a session in a web app. Then every pageview or event you send to GA can be analyzed within the context of this dimension.
Additional References
http://analytics.blogspot.com/2011/08/update-to-sessions-in-google-analytics.html
http://www.analytics-ninja.com/blog/2011/08/how-google-analytics-calculates-visits.html
https://developers.google.com/analytics/devguides/platform/customdimsmets
https://support.google.com/analytics/answer/2709829?hl=en

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)

Resources