views:

174

answers:

1

I have a simple website which is using C# and Linq to SQL to read an write to the DB. Everything works fine on my local box but now that I have setup on my hosting environment I am getting a "Security Exception" when trying to write to the DB, reading is fine. I have contacted the hosting company who say that they have configured everything that they can on the server side. This is an example of the linq query and the exception that is thrown

Linq Query:

public void LogLoginAttempt(string Username, string Password, bool isValidated)
{
   try
   {
      Data.ProfileLoginLog pll = new Library.Data.ProfileLoginLog();

      pll.Username = Username;
      pll.Password = Password;
      pll.isValidated = isValidated;
      pll.CreateDate = DateTime.Now;

      dc.ProfileLoginLogs.InsertOnSubmit(pll);
      dc.SubmitChanges();
  }
  catch (Exception ex)
  {
    ErrorLog e = new ErrorLog();
    e.LogFatalError(ex);
  }
}

Exception:

Server Error in '/' Application.
--------------------------------------------------------------------------------

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.Security.Permissions.ReflectionPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


[SecurityException: Request for the permission of type 'System.Security.Permissions.ReflectionPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
   System.Security.CodeAccessSecurityEngine.ThrowSecurityException(Assembly asm, PermissionSet granted, PermissionSet refused, RuntimeMethodHandle rmh, SecurityAction action, Object demand, IPermission permThatFailed) +150
   System.Security.CodeAccessSecurityEngine.ThrowSecurityException(Object assemblyOrString, PermissionSet granted, PermissionSet refused, RuntimeMethodHandle rmh, SecurityAction action, Object demand, IPermission permThatFailed) +100
   System.Security.CodeAccessSecurityEngine.CheckSetHelper(PermissionSet grants, PermissionSet refused, PermissionSet demands, RuntimeMethodHandle rmh, Object assemblyOrString, SecurityAction action, Boolean throwException) +284
   System.Security.PermissionSetTriple.CheckSetDemand(PermissionSet demandSet, PermissionSet& alteredDemandset, RuntimeMethodHandle rmh) +69
   System.Security.PermissionListSet.CheckSetDemand(PermissionSet pset, RuntimeMethodHandle rmh) +150
   System.Security.PermissionListSet.DemandFlagsOrGrantSet(Int32 flags, PermissionSet grantSet) +30
   System.Threading.CompressedStack.DemandFlagsOrGrantSet(Int32 flags, PermissionSet grantSet) +40
   System.Security.CodeAccessSecurityEngine.ReflectionTargetDemandHelper(Int32 permission, PermissionSet targetGrant, CompressedStack securityContext) +123
   System.Security.CodeAccessSecurityEngine.ReflectionTargetDemandHelper(Int32 permission, PermissionSet targetGrant, Resolver accessContext) +41

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.3082; ASP.NET Version:2.0.50727.3082
+1  A: 

The important part of the error message to me is:

The application attempted to perform an operation not allowed by the security policy. ...

Request for the permission of type 'System.Security.Permissions.ReflectionPermission, ...

So the security problem isn't caused (directly) by authenticating against the database. Instead, some code needs to use Reflection to run correctly, and the system's security policy doesn't allow that.

To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Max Lybbert
yeah but how do you fix it?
Kieran
There are only two ways to fix this: either change the method to not use reflection or grant your process the security clearance necessary to use reflection. The link I provided in the original answer has enough links to other resources detailing how to get the needed security clearance that I'll leave that to you to follow.
Max Lybbert