同一 AppDomain 上的多个程序集的不同安全权限
是否可以将多个程序集加载到新的 AppDomain 中并对每个程序集应用不同的 PermissionSet
?
比如说,通过授予其中一个程序集不受限制的 FileIOPermission 并拒绝其他程序集的此类权限来允许该程序集写入磁盘。
如果可以的话。如何?
更新
附注我正在从不执行 DLL 的 DLL 中创建类型的实例,因此我使用 Load
和 CreateInstanceAndUnwrap
而不是 ExecuteAssembly
。
更新
我尝试(但失败)使用以下代码使用 load 方法提供证据:
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(unprotectedAssembly, unprotectedAssembly & ".Actions").Sum(1, 2))
Console.ReadLine()
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(unprotectedAssembly, unprotectedAssembly & ".Actions").Sum(1, 2))
Console.ReadLine()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加此行(以解决您遇到的异常):
有用的文章:http://www .reliablesoftware.com/articles/UnderstandingSecurityActions.html
Add this line (to get around the exception you're getting):
Helpful article: http://www.reliablesoftware.com/articles/UnderstandingSecurityActions.html
您可以在使用 Assembly.Load 方法实例化自定义程序集之一时指定自定义 Evidence 对象,或者查看 此处
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