大会装载远程计算机上使用Assembly.LoadFrom()会导致SecurityException异常 [英] Assembly loaded using Assembly.LoadFrom() on remote machine causes SecurityException

查看:218
本文介绍了大会装载远程计算机上使用Assembly.LoadFrom()会导致SecurityException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载使用 Assembly.LoadFrom(文件名)组装。当文件名是本地机器上,一切工作正常。然而,当相同的文件(依赖)是在远程网络共享,我收到了 System.Security.SecurityException 那一刻我尝试创建一个新的的SqlConnection 从远程组件:




System.Security.SecurityException:请求类型的权限
'System.Data.SqlClient.SqlClientPermission,System.Data这,
版本2.0.0.0 =文化=中性公钥= b77a5c561934e089'
失败。


< /块引用>

有什么办法吗?


解决方案

您可以加载程序集作为字节和的Assembly.Load加载(字节),也许这个作品。



或者你给应用程序要求。权限



编辑:



我做了一个小测试,它为我工作。下面是一些代码:

 静态字典<大会,字符串> _Paths =新词典<大会,字符串>(); 

静态无效的主要(字串[] args)
{
的AppDomain电流= AppDomain.CurrentDomain;
current.AssemblyResolve + =新ResolveEventHandler(HandleAssemblyResolve);

//此行加载组件并检索所有类型的吧。只有当
//调用GetTypes出现'AssemblyResolve'事件并加载依赖
类型[]类型= LoadAssembly(Assemblies\\MyDLL.dll)GetTypes()。
//下一行是用来测试的权限,我测试了IO-权限
//与反思权限(应使用远程组件时被拒绝)
//另外本次测试包括表单
对象实例的创建= Activator.CreateInstance(类型[0]);
}

私有静态大会LoadAssembly(字符串文件)
{
//将装配
大会结果=的Assembly.Load(File.ReadAllBytes(文件));
//组件的路径添加到字典
_Paths.Add(结果,Path.GetDirectoryName(文件));
返回结果;
}

静态大会HandleAssemblyResolve(对象发件人,ResolveEventArgs参数)
{从全quallified名
字符串名称
//提取文件名= args.Name;
名称= name.Substring(0,name.IndexOf(,));
//将装配
返回LoadAssembly(Path.Combine(_Paths [args.RequestingAssembly],名称+.DLL));
}



的东西很重要:



有可能不具有匹配的名称和文件名的文件,但你可以通过检查文件夹中的所有文件与 AssemblyName.GetAssemblyName(文件)。<解决此/ p>

I am loading an assembly using Assembly.LoadFrom(fileName). When fileName is on the local machine, everything works fine. When, however, the identical file (and dependencies) are on a remote network share, I get a System.Security.SecurityException the moment I try to create a new SqlConnection from the remote assembly:

System.Security.SecurityException: Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

What's the cure?

解决方案

You could load the assembly as bytes and load it with Assembly.Load(bytes), maybe this works.

Or you give the application the requested permission.

Edit:

I made a little test and it worked for me. Here is some code:

static Dictionary<Assembly, String> _Paths = new Dictionary<Assembly, String>();

static void Main(string[] args)
{
    AppDomain current = AppDomain.CurrentDomain;
    current.AssemblyResolve += new ResolveEventHandler(HandleAssemblyResolve);

    // This line loads a assembly and retrieves all types of it. Only when
    // calling "GetTypes" the 'AssemblyResolve'-event occurs and loads the dependency
    Type[] types = LoadAssembly("Assemblies\\MyDLL.dll").GetTypes();
    // The next line is used to test permissions, i tested the IO-Permissions 
    // and the Reflection permissions ( which should be denied when using remote assemblies )
    // Also this test includes the creation of a Form
    Object instance = Activator.CreateInstance(types[0]);
}

private static Assembly LoadAssembly(string file)
{
    // Load the assembly
    Assembly result = Assembly.Load(File.ReadAllBytes(file));
    // Add the path of the assembly to the dictionary
    _Paths.Add(result, Path.GetDirectoryName(file));
    return result;
}

static Assembly HandleAssemblyResolve(object sender, ResolveEventArgs args)
{
    // Extract file name from the full-quallified name
    String name = args.Name;
    name = name.Substring(0, name.IndexOf(','));
    // Load the assembly
    return LoadAssembly(Path.Combine(_Paths[args.RequestingAssembly], name + ".dll"));
}

Something important:

There may be files which do not have a matching name and file name, but you can resolve this by checking all files in folder with AssemblyName.GetAssemblyName(file).

这篇关于大会装载远程计算机上使用Assembly.LoadFrom()会导致SecurityException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆