开发者

Different Security Permissions for Multiple Assemblies on Same AppDomain

开发者 https://www.devze.com 2023-03-26 15:16 出处:网络
Is it possible to load multiple assemblies into a new AppDomain and apply different PermissionSet to each?

Is it possible to load multiple assemblies into a new AppDomain and apply different PermissionSet to each?

Say, allow one of the assemblies to write to disk by granting it an unrestricted FileIOPermission and denying such permission to the other(s).

If it's possible. How?

Update

P.S. I'm creating instances of types out of DLL's not executing exes, so I'm using Load and CreateInstanceAndUnwrap instead of ExecuteAssembly.

Update

I tried (and failed) providing evidence with the load method with the following code:

Dim domain As AppDomain = AppDomain.CreateDomain("AssembliesDomain")
Dim protectedSet As New PermissionSet(PermissionState.None)

protectedSet.AddPermission(New SecurityPermission(SecurityPermissionFlag.Execution))
protectedSet.AddPermission(New IsolatedStorageFilePermission(PermissionState.Unrestricted))
protectedSet.PermitOnly()

domain.Load(protectedAssembly, New Evidence(Nothing, {protectedSet}))
domain.Load(unprotectedAssembly, New Evidence(Nothing, {protectedSet}))

Console.WriteLine(domain.CreateInstanceAndUnwrap(protectedAssembly, protectedAssembly & ".Actions").Sum(1, 2))
Console.WriteLine(domain.CreateInstanceAndUnwrap(un开发者_Go百科protectedAssembly, unprotectedAssembly & ".Actions").Sum(1, 2))
Console.ReadLine()


Add this line (to get around the exception you're getting):

protectedSet.AddPermission(new UIPermission(PermissionState.Unrestricted)); 

Helpful article: http://www.reliablesoftware.com/articles/UnderstandingSecurityActions.html


You could either specify custom Evidence object when using the Assembly.Load method to instantiate one of your custom Assemblies, or have a look here

0

精彩评论

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