The assembly loaded using Assembly.LoadFrom () on the remote computer raises a SecurityException

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

System.Security.SecurityException: A permission request of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version = 2.0.0.0, Culture = Neutral, PublicKeyToken = b77a5c561934e089' failed.

What kind of treatment?

+1
source share
3 answers

You can 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 did a little test and it worked for me. Here is the 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 that do not have a matching name and file name, but you can solve this problem by checking all the files in the folder using AssemblyName.GetAssemblyName(file) .

+3
source

The assembly loads the LocalIntranet permission set. This restricts certain APIs. Cm:

http://msdn.microsoft.com/en-us/library/03kwzyfc.aspx and http://msdn.microsoft.com/en-us/library/0x4t63kb%28v=vs.80%29.aspx

+1
source

Well, I found a workaround. I could not find a way around the SecurityException problem - therefore, instead of loading the assembly from the remote folder, I just copied the contents of the remote folder at runtime to the local computer and ran from there. Simple, neat and works great. In addition, this is probably the best way to work, so clients disconnect their local copy instead of uploading to the server, and this makes it easier to deploy an updated version of the original. without blocking files.

Caution to everyone who is trying to follow in my footsteps: do not try to copy to a subfolder of your application directory; for some reason this causes dependency errors. Rather, copy to "Environment.GetFolderPath (Environment.SpecialFolder.CommonApplicationData)" or to another folder in which you know that you have write access.

+1
source

Source: https://habr.com/ru/post/912244/


All Articles