开发者

.NET 4.0 AppDomain: obsoleted Evidence

开发者 https://www.devze.com 2022-12-28 10:51 出处:网络
I am a .NET student and currently we are learning about Application Domains. We were given the following example code (for .NET 3.5). As expected, it throws a SecurityException. Note: TestApp.exe is a

I am a .NET student and currently we are learning about Application Domains. We were given the following example code (for .NET 3.5). As expected, it throws a SecurityException. Note: TestApp.exe is added as a reference in the project.

Dim file As String = "TestApp.exe"
Dim hostEvidence As Object() = {New Zone(SecurityZone.Intern开发者_Go百科et)}
Dim appDomainEvidence As Evidence = New Evidence(hostEvidence, Nothing)
Dim d As AppDomain = AppDomain.CreateDomain("MyDomain", appDomainEvidence)
d.ExecuteAssembly(file)

When trying to run this in VS2010 under .NET 4.0 I run into a problem. First it shows a warning

'Public Sub New(hostEvidence() As Object, assemblyEvidence() As Object)' is obsolete: 'This constructor is obsolete. Please use the constructor which takes arrays of EvidenceBase instead.'.

I change the type of hostEvidence to EvidenceBase() and the warning is gone. However, when trying to run the application it gives an error.

This method implicitly 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.

I have viewed the page, followed the link to How to: Run Partially Trusted Code in a Sandbox and read http://blogs.msdn.com/shawnfa/archive/2009/05/27/coding-with-security-policy-in-net-4-0-implicit-uses-of-cas-policy.aspx but I'm having trouble understanding all of this.

The code example on MSDN is quite big compared to what I currently have, so any help with changing my code so it works without adding other stuff, will be very appreciated.


As it says in the link you provided, .NET is no longer supporting the policy portion of the code access security framework, as of version 4.0.

In other words, your lesson is about .NET 3.5 and does not pertain to the 4.0 framework. The solution is to revert to your original code and configure your project to target the 3.5 framework (you can still use Visual Studio 2010).

.

0

精彩评论

暂无评论...
验证码 换一张
取 消