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>
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…
Ted, what error are you seeing? Do you have a message and a stack trace?
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.
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.
Hello again,
Thank you for the tip. It works..
Looking forward for the blog post for details that I might have missed.
Keep up the good work!
Regards,
Costi.
Hi!
I am having this error in my windows forms designer (VS 2010 RC)
I’ve tried setting NetFx40_LegacySecurityPolicy in my: app.config as well as in the lc.exe.config (what i have found in this post: http://stackoverflow.com/questions/1733252/net-4-0-with-code-access-security-netfx40-legacysecuritypolicy-wont-work)
any other ideas for me??
THANKS! regards gerry
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
Pingback: Tip #20 – Opting Out of Security Changes in .NET 4 in ASP.NET and Custom AppDomains | David DeWinter
Adding
to app.config did not help. However, after adding same configuration options into
%windir%Microsoft.NETFrameworkv4.0.30319Configmachine.config
did the trick. Enjoy.