Search code examples
c#.netlegacycom+code-access-security

Legacy assembly throws "Request for principal permission failed"


I'm developing against some legacy .NET assemblies that are using System.EnterpriseServices and compiled in .NET Framework 2.0. I'm receiving a "Request for principal permission failed" exception in some scenarios but not in others. For example the code works OK when

  • I run it from an xunit runner test in.net 4.5.1
  • I runt it in as a webservice inside IIS also .net 4.5.1
  • I run it from a windows forms application build against .net 2.0

But it throws an exception when I run it from a Windows forms app .net 4.5.1. In this case I get the mentioned exception and the FirstPermissionThatFailed.
<IPermission class="System.Security.Permissions.PrincipalPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1"><Identity Authenticated="true" Role="1,50000,50750,50751:Execute"/> </IPermission>

I reverse engineered the legacy API and saw that the functions have the following attribute
[PrincipalPermission(SecurityAction.Demand,Role="1,50000,50750,50751:Execute")] I know that the problem is probably caused by major changes that were done to Code Access Security, but I do not know what exactly, so I have 2 questions:

  1. To which actual roles do this roles id's map to 1,50000,50750,50751
  2. Why is the code not triggering the exception when it is called from a resharper xunit test runner?? Like it is running in a different security context??

Thanks!


Solution

  • Adding this to App.config solved the problem:

    <configuration> <startup useLegacyV2RuntimeActivationPolicy="true" /> </configuration>

    I guess Resharper Xunit runner is using it when it loads and executes assemblies and that it's why it worked from tests.