How to calculate time in asp.net? - c#

My webapplication is online exam site. when user click on start for writing test, i have to restrict the user particular time only, after time is completed message should display to the user that your exam time is finished and he redirect to home page, how can i write the code, can i go for timer control or webservice which one is good for the online exam site, please help me.

You need to capture the time when the user starts the test inside a Session variable:
Session["StartTime"] = DateTime.Now;// when user start test
Then when you postback the page get the difference between the current time and the time you stored in the Session:
diffrencebetweentime = DateTime.Now - Convert.ToDateTime(Session["StartTime"]); // at every postback
If the time difference is more than what you have decided send user to home page.

Related

ASP.Net - Implementing a Heartbeat Control with multi-tab support

What is it?
The Heartbeat control that I'm currently developing is a client-side/server-side solution which displays a popup modal to the user after 27 minutes of server session inactivity thereby displaying a countdown of 3 minutes before the session expires and returns the user to the Login page. The modal popup also includes an "Extend time" button and a "Logout" button.
How it works
On the client-side, once the page loads the heartbeat control is initialized and the countdown starts. The heartbeat control has a MaxHealth (milliseconds) and a CriticalHealthRatio (percentage) specifying the session life and when the modal should pop-up respectively.
If the user does nothing during the countdown or alternatively clicks "Logout" then the JavaScript code will redirect the user to the Logout page which clears all sessions and redirects to the Login page.
If the user clicks "Extend time" then an ajax call is made to a web service which simply revitalizes the session on the server-side as well as the client-side's MaxHealth.
Extending control to be multi-tab compatible
At this point, the heartbeat control works on a page by page basis, but does not take into account other tabs that are opened.
Scenario 1: A user logs in and after 15 minutes they open a second tab. Opening another tab will revitalize the session, but the to the first tab the heartbeat is still counting down from that initial MaxHealth, not knowing that the session was in fact reset. Even if the user continues to navigate with the second tab, constantly revitalizing the session, the moment the first tab reaches 0, it will automatically cause the user to be logged out.
Partial Solution
In order to solve the underlying problem of causing the user to be logged out on currently opened tabs which aren't being refreshed, my solution was to implement another web service called GetLastResponse() that retrieves a session which contains a DateTime value of the last server response time.
Therefore on each page request, the server resets the LastReponse session to the current time which is handled in the Global.asax and the HeartBeat control will now countdown from that LastResponse time when the page is loaded on the client-side.
With this new web service which returns the LastResponse time, I can now simply have the heartbeat control on every tab call it every X minutes to determine if the session is any different since the last request thereby synchronizing the Heartbeat controls on all tabs to display the popup at the same time as well as remove the popup on all tabs when the user extends the time on one of the tabs.
But of course, this can't possibly work. The moment I call GetLastResponse() the session is effectively reset in the Global.asax.
Even if I add a condition in the Global.asax which doesn't reset the LastResponse session when a request is made to the GetLastResponse() web service, that doesn't stop the actual session lifetime from being revitalized; it only disassociates the LastResponse DateTime from the actual lifetime of the session.
Questions
I believe the proposed solution will work, however being that the LastResponse
and the actual session lifetime will effectively be different, is
there a better way to go on about this while retaining the core
functionality intended of the Heartbeat control (synchronized
countdown, multi-tab extend time, multi-tab logout)?
Is there a way to designate a web service to not reset the session
lifetime when called but also allows C# to access the session?
Is there a way to request the remaining session lifetime without
revitalizing it?

Redirect parent page on Login cookie expiration

I have a problem with server-side auto-redirection to a login page upon cookie timeout.
Our application uses a wrapper page (called "Application.aspx") which hold all our controls, as well as the rest of our application inside IFrames.
My problem occurs when the Login Cookie times out. The user should be looking at a Timeout warning screen in the form of a RadWindow launched when their user-set lock time is reached. This screen exists until the global Authentication cookie expires. However, at that point, any action redirects only the RadWindow to the login, not the full application.
I have tried implementing a Response.Redirect("SessionTimeout.aspx,false); line to the globabl.aspx page in the AuthenticationRequest event. However, it appears somewhere along the line an Auto control is redirecting before my SessionTimeout page can execute its pageLoad.
Sadly, this is for a large company and I cannot share much exact code, but if anyone has found a way around this I would be very happy.
If anyone else finds this and needs the answer, I ended up creating a new cookie containing the expiry date of the original cookie.
I then checked if the current datetime was close to the value of the exp with jscript at regular intervals, and when it was within 30 seconds of expiring, redirected the user client-side.

how to capture the mouse idle state in Master Page Iframe in javascript

I am using a Master page and many other pages from different folders are displayed in the IFrame within the master page.
If the user is not doing anything for some time [5 minutes or so] in any of the other pages, I want to give an alert message either to continue or to log out.
How to do this?
For example setTimout in jsvascript on mouseover for your iFrame. On each mouseover drop your timeout with clearTimeout and create new one.
But I don't think it's a good idea. Better to handle session timeout on the server. More info about sessions state you can find here: ASP.NET Session State
After session ending user might me autmatically logged of and will be requested to login next time on any action whitch post request to the server.

Hit counter for MVC ASP.NET website

How to create a hit counter(visitor counter) for each page in MVC website?
And store information in MS SQL Database. The same as stackoverflow using on each page.
Please show me an example. Thank u
In your master page insert a function call that logs the name of the page or whatever other details you want. Run your counts and there you go.
I was able to do this using Session variables, sql server db in ASP.NET MVC.
Note: I used session variables instead of storing client IP address. As saving all the client address will overflow your database. This works similar to the Stackoverflow pages viewed property showing for every question at the top right.
This is how it worked for me.
Whenever a user opens a particular web page in your website, the counter increments and the updated count will be updated in sql server database(it could be anyother database) tables.
If the same user visits the same webpage again within the same session, the counter will not be incrmented. That means if user refreshes a web page multiple times with in the same session, the counter will not be incremented, as it is a single visit by that user.
If the same visitor closes his session(say he closes the browser) and reopens the same web page the counter will be incremented considering it as a separate visit.
Here are the steps I followed to implement it.
Have a method which will be automatically called before every Action method.
Inside this method, initiate a session if it is not done already and check if the user has visited this page already.
Now, update the counter variable accordingly and save the count to db if required.
Here is the complete procedure I have followed. Hope it helps.
Source: Count number of visitors of a page in asp.net mvc (HIT counter)

ASP.net Ajax Timeout

I am stuck with count down in ajax, I have a small website where I ask each user 5 question in 1 min time, after they login using asp.net membership provider. I want when the user time is over he/she must be taken to result page and show the result, I am using asp.net ajax timer, but if the user press F5 his time start again and by pressing F5 he can continue his test.
how can I stop that. also is there a easy way to show countdown using Ajax for 60sec
Regards
Arshad
Anything client side is potentially hackable, so you can't effectively maintain a time limit using only client-side technologies. When they begin a test, keep a DateTime in their Session to indicate that a test has been begun. If the submission comes in after more than the time limit has elapsed based on the server time, then tell them to try again cause they cheated.

Categories

Resources