Opting Out of Security Changes in .NET 4 – #5

I decided to provide another tip today since .NET 4 Beta 1 was released! I definitely like the changes that the security team has made to make permissions easier to understand and to improve enforcement of transparency, but there are breaking changes here that require work you may not be ready for. If you need to revert to the old behavior (e.g. using CAS policy, CLR 2.0 transparency, or the old SecurityActions) in order to prepare for migration, then take a look below.

To enable legacy CAS policy, support for the obsolete SecurityActions, and anything else that can make AppDomains heterogeneous, add the NetFx40_LegacySecurityPolicy element to the runtime element of your configuration file. This will enable the legacy behavior only for the application for which you make the configuration change. 

<configuration>

  <runtime>

    <NetFx40_LegacySecurityPolicy enabled="true" />

  </runtime>

</configuration>

 

To revert to CLR 2.0 transparency, add the System.Security.SecurityRulesAttribute to your assembly and specify the Level1 SecurityRuleSet. (Level1 = CLR 2.0, Level2 = CLR 4.0)

[assembly: SecurityRules(SecurityRuleSet.Level1)]

 

Update June 8, 2009: The configuration switch for enabling legacy CAS policy under .NET 4 Beta 2 has changed to NetFx40_LegacySecurityPolicy, and I’ve updated the post above. In case you are using .NET 4 Beta 1, the switch is legacyCasPolicy, as shown below.

<configuration>

  <runtime>

    <legacyCasPolicy enabled="true" />

  </runtime>

</configuration>

9 thoughts on “Opting Out of Security Changes in .NET 4 – #5

  1. I tried the above when I installed VS 2010 Beta1, and it didnt work.
    I modified the app.config in my project and also the devenv.exe.config – nothing helped.
    I tried both legacyCasPolicy and NetFx40_LegacySecurityPolicy.

    The error wont go away…

  2. Hi there,

    I’m having the same problem here. (ASP.Net Web Application on .Net 4.0 beta2 and a DevExpress control that has no .Net 4.0 support and has CAS).
    I’ve tried with the NetFx40_LegacySecurityPolicy configuration switch but the error I get is the same:

    This method explicitly uses CAS policy, which has been obsoleted by the .NET Framework. In order to enable CAS policy for compatibility reasons, please use the NetFx40_LegacySecurityPolicy configuration switch. Please see http://go.microsoft.com/fwlink/?LinkID=155570 for more information.

  3. Thanks for bringing this up Constantin. You’ll need to set the legacyCasModel attribute on the trust element in your web.config to “true” in order for ASP.NET to run with this setting.

    I will blog about this in more detail later.

  4. i found an solution for my problem :) i included the XML configuration in the devenv.exe.config, and now also the designer is showing the controls like it should.

    so, open:
    C:Program FilesMicrosoft Visual Studio 10.0Common7IDEdevenv.exe.config
    and add:

    to the runtime section

  5. Pingback: Tip #20 – Opting Out of Security Changes in .NET 4 in ASP.NET and Custom AppDomains | David DeWinter

  6. Adding

    to app.config did not help. However, after adding same configuration options into
    %windir%Microsoft.NETFrameworkv4.0.30319Configmachine.config
    did the trick. Enjoy.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>