Archive for category .Net Development

Windows 2008 IIS .Net SecurityException

I was attempting to install a .Net 3.5 application written in C# onto a Windows 2008 Enterprise R2 (64 bit) virtualized system.  What makes this a little unique is that the ASP.Net code is stored on a UNC file share.  Because of this issue, I received the following error:

Security Exception
Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application’s trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the permission of type ‘System.Web.AspNetHostingPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′ failed.

Initially I was baffled and with enough searching I came across the “caspol” solution from Microsoft.

Which in essence is:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\caspol.exe -m -ag 1 -url "file:////\\fs\share\*" FullTrust -exclusive on

And of course, this doesn’t work! I had overlooked that inside each .Net framework directory (yes there are two on 64 bit systems) is a separate configuration file.

By running the 64 bit caspol.exe command, I was able to quickly fix the issue.

C:\WINDOWS\Microsoft.NET\Framework64\v2.0.50727\caspol.exe -m -ag 1 -url "file:////\\fs\share\*" FullTrust -exclusive on

I restarted the IIS service for good measure and we’re off to the races.

, , , , ,

No Comments

.Net C Sharp Date Range Check Method

This function is a trivial example of how to do a basic date range check in .Net. I do a few silly conversions from string to timestamp only to serve as an example.

View Code CSHARP
/*
Assumes the local culture.  
Does a check to see if the current time is within the range 
  of 7 AM and 1 PM.  If so, returns true.
*/
if (IsCurrentDateInRange("7:00", "13:00")) {
   //The current date is in range.
} else {
  //The current date is out of range.
}
View Code CSHARP
using System;
/*
startTime should be in the format HH:mm such as 09:45
endTime should be in the format HH:mm such as 18:45
The culture is assumed to be the local one which can be 
troubling in applications that extend themselves internationally.
*/
public bool IsCurrentDateInRange(string startTime, string endTime) {
  DateTime dtNow = DateTime.Now;
  DateTime dtStart, dtEnd;
  int hour = 0, minute = 0;
 
  string[] strDt = startTime.Split(new char[] { ':' });
  if (strDt.Length > 1)
  {
     Int32.TryParse(strDt[0], out hour);
     Int32.TryParse(strDt[1], out minute);
 
     dtStart = new DateTime(dtNow.Year, dtNow.Month, dtNow.Day, hour, minute, 0);
  }
  else
  {
     //TODO: log an error in formatting.
     return false;
  }               
 
  strDt = endTime.Split(new char[] { ':' });
  if (strDt.Length > 1)
  {
     hour = minute = 0;
     Int32.TryParse(strDt[0], out hour);
     Int32.TryParse(strDt[1], out minute);
 
     dtEnd = new DateTime(dtNow.Year, dtNow.Month, dtNow.Day, hour, minute, 0);
  }
  else
  {
     //TODO: log an error in formatting.
    return false;
  }
 
  TimeSpan tsEndDiff = dtEnd - dtNow;
  TimeSpan tsStartDiff = dtNow - dtStart;
  if (tsStartDiff.TotalSeconds >= 0 && tsEndDiff.TotalSeconds >= 0)
  {
     return true;
  }
  else 
     return false;
}

, , ,

No Comments