views:

189

answers:

1

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.Internet)}
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.

+1  A: 

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).

.

Jeff Sternal
I realise that the lessen is about 3.5, but the MSDN page on migration uses *SecurityZone.Internet* in the code. I just have the impression that it does more than I want, and I don't have the experience to take out the parts I need.
Stijn
Your lesson *may* also be applicable to .NET 4.0, but you haven't provided enough information in your question to say. (And probably can't.) If you continue to try to make it work with 4.0 *while you're learning the material* you risk running into more nasty surprises as you run into language and framework changes. You can still use Visual Studio 2010, you just shouldn't target the 4.0 framework.
Jeff Sternal
Fair point. Thank you for your time.
Stijn