{"id":1653,"date":"2009-06-24T14:48:00","date_gmt":"2009-06-24T14:48:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/dotnet\/2009\/06\/24\/enabling-cas-policy-compatibility-mode-for-a-project\/"},"modified":"2021-10-04T15:14:50","modified_gmt":"2021-10-04T22:14:50","slug":"enabling-cas-policy-compatibility-mode-for-a-project","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/enabling-cas-policy-compatibility-mode-for-a-project\/","title":{"rendered":"Enabling CAS Policy Compatibility Mode for a Project"},"content":{"rendered":"<p><P>If you\u2019re migrating a VS project over to VS 2010 Beta1 and you are calling code that is passing Evidence objects to the runtime expecting security policy resolution, or if you\u2019re <A href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.security.codeaccesspermission.deny(VS.100).aspx\">using a permission Deny<\/A>, you may run into the following exception:<\/P>\n<P>NotSupportedException: This method uses CAS policy, which has been obsoleted by the .NET Framework. In order to enable CAS policy for compatibility reasons, please use the legacyCasPolicy configuration switch. Please see http:\/\/go2.microsoft.com\/fwlink\/?LinkId=131738 for more information.<\/P>\n<P>What does this mean? In earlier versions of the .NET Framework, Code Access Security (CAS) Policy was the primary mechanism for defining whether or not certain classes of managed applications were able to perform security sensitive operations or not. While powerful, CAS Policy was extremely difficult to use and manage \u2013 developers and administrators would have to set up and analyze complicated policy trees to determine what their code would be granted. Furthermore, these restrictions could be circumvented by simply using unmanaged code!<\/P>\n<P>We\u2019ve provided cleaner, more appropriate mechanisms for managed code security in .NET Framework 4; those will be described in detail in future posts. CAS Policy has been rendered obsolete as a result of this, hence the exception messages. It is, however, still there for those migrating apps and projects that still require CAS policy in the meantime. <\/P>\n<P>Note that by enabling CAS for your project, you\u2019re subjecting yourself to the CAS security policy decisions of the runtime. If you made any changes (via caspol.exe, etc) to CAS policy in previous versions of the runtime, <I>these will not be reflected in .NET Framework 4 even with CAS policy turned on<\/I>. Your custom policy will have to be reconfigured with the .NET Framework 4 caspol.exe.<\/P>\n<P>If you\u2019re using CAS policy <A href=\"http:\/\/blogs.msdn.com\/shawnfa\/archive\/2009\/05\/27\/coding-with-security-policy-in-net-4-0-implicit-uses-of-cas-policy.aspx\">implicitly<\/A> (for example, an Assembly.Load with Evidence) or <A href=\"http:\/\/blogs.msdn.com\/shawnfa\/archive\/2009\/06\/09\/coding-with-security-policy-in-net-4-part-2-explicit-uses-of-cas-policy.aspx\">explicitly<\/A> (SecurityManager.PolicyHiearchy), check out <A href=\"http:\/\/blogs.msdn.com\/shawnfa\/\">Shawn Farkas\u2019 blog<\/A> for guidance on the new, supported mechanisms for sandboxing and running partial trust code.<\/P>\n<P>Follow these steps to enable CAS Policy for your project:<\/P>\n<P>1. Go to \u201cProject\u201d in the menu bar, then \u201cAdd New Item\u2026\u201d<\/P>\n<P><A href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/10\/2019\/02\/clip_image002_2.jpg\"><IMG title=\"clip_image002\" border=\"0\" alt=\"clip_image002\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/10\/2019\/02\/clip_image002_thumb.jpg\" width=\"488\" height=\"379\"><\/A><\/P>\n<P>2. Select \u201cApplication Configuration File\u201d from the list of items available.<\/P>\n<P><A href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/10\/2019\/02\/clip_image004_2.jpg\"><IMG title=\"clip_image004\" border=\"0\" alt=\"clip_image004\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/10\/2019\/02\/clip_image004_thumb.jpg\" width=\"614\" height=\"450\"><\/A><\/P>\n<P>3. Enable CAS Policy via the following switch:<\/P>\n<P>&lt;configuration&gt;<\/P>\n<P>&lt;runtime&gt;<\/P>\n<P>&lt;legacyCasPolicy enabled=&#8221;true&#8221; \/&gt;<\/P>\n<P>&lt;\/runtime&gt;<\/P>\n<P>&lt;\/configuration&gt;<\/P>\n<P><A href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/10\/2018\/10\/clip_image006_2.jpg\"><IMG title=\"clip_image006\" border=\"0\" alt=\"clip_image006\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/10\/2019\/02\/clip_image006_thumb.jpg\" width=\"583\" height=\"446\"><\/A><\/P>\n<P>4. CAS Policy is now enabled and enforced for this project. <\/P>\n<P>If you\u2019re simply trying to get loads from remote sources (i.e. Assembly.LoadFrom(@&#8221;http:\/\/&#8230;&#8221;)) to work, you don\u2019t need to enable CAS Policy to do this \u2013 replace <B>legacyCasPolicy<\/B> with <B>loadFromRemoteSources<\/B> in the example above and you\u2019ll be able to load from any location. Please do note that <U>these assemblies will be loaded as fully trusted<\/U>! You can find more information on this switch <A href=\"http:\/\/blogs.msdn.com\/shawnfa\/archive\/2009\/06\/08\/more-implicit-uses-of-cas-policy-loadfromremotesources.aspx\">here<\/A>.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you\u2019re migrating a VS project over to VS 2010 Beta1 and you are calling code that is passing Evidence objects to the runtime expecting security policy resolution, or if you\u2019re using a permission Deny, you may run into the following exception: NotSupportedException: This method uses CAS policy, which has been obsoleted by the .NET [&hellip;]<\/p>\n","protected":false},"author":342,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[685],"tags":[],"class_list":["post-1653","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnet"],"acf":[],"blog_post_summary":"<p>If you\u2019re migrating a VS project over to VS 2010 Beta1 and you are calling code that is passing Evidence objects to the runtime expecting security policy resolution, or if you\u2019re using a permission Deny, you may run into the following exception: NotSupportedException: This method uses CAS policy, which has been obsoleted by the .NET [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/1653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/342"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=1653"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/1653\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=1653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=1653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=1653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}