TimeZoneOffsets and medium trust issues

14 October 2009
Two issues I had to fight with today which took longer to solve than I originally thought.

First I looked into a problem I had since the last new version of my travelog website. When I add a new entry for the day, the date and time is send along with the other data to the server with an http post. But every time I save a entry the time is reduced by 7 hours. So after a few edits even the day itself is wrong.
I can explain the 7 hours, that the time difference between where I am (Peru) and the server (Germany), but where does it come from?

I looked at the database server and it has the time as specified in the web form. So the issue is getting the data back from the server. I checked my JavaScript which pulls the data via AJAX from the server and puts it onto the form, that looked fine too. Then I used Firebug to look at the JSON data that comes from the web server. The time there is seven hours less than whats in the database! I use ASP.NET web methods to return an object serialized as JSON.

The JSON serializer figures out, the client is 7 hours behind the server and takes those seven hours off each DateTime value. How does the server even know about those seven hours. I first thought about the http request headers but there is no time zone information in there. Well I still don´t know how it knows.

To fix the problem I send the timezoneOffset in the browser available through the JavaScript getTimezoneOffset() method when making a request. On the server I find out the time zone offset of the server and then fix all DateTime fields before sending them down. Not nice but it works.

  // get a local time zone info
  TimeZoneInfo tz = TimeZoneInfo.Local;

  // get it in hours
  int offset = tz.BaseUtcOffset.Hours;

  // add one hour if we are in daylight savings
  if (tz.IsDaylightSavingTime(DateTime.Now))
  {
      offset++;
  }

The second problem I had was with a very simple site, I added an aspx page to download zip files from the site and log the downloads. It worked fine locally but on the live server the line:

if (System.IO.File.Exists(fileName)
which makes sure the file exists before it is trying to send it returned false. I triple checked that the file was there and it was. I checked NTFS permissions and IIS settings, all fine. I could download the file directly via http from the same location. I ran ProcessMonitor on the server and the web process didn't even try to open the file. What is going on here?

I finally removed the line above and let the Response.TransmitFile method throw the error, now it told me what was going on. I had set the site to Medium Trust because it only performed very basic operations. In Medium Trust your application is not allowed to access the file system.

So in this case System.IO.File.Exists returns a false, but shouldn´t it really throw an exception? I am not sure, but it made it harder to debug.

Pages in this section

Categories

ASP.Net | Community | Development | IIS | IT Pro | Security | SQL (Server) | Tools | Web | Work on the road | Windows