This project has moved. For the latest updates, please go here.

Medium Trust Web

Mar 16, 2011 at 3:37 PM

Hi,

I wish to use EPPLUS on a web hosted site, but there is a medium trust level and I receive the error: That assembly does not allow partially trusted callers

There is any workaround ?

Thank you.

Mar 17, 2011 at 11:46 AM

Ok some update on this issue.

I've compiled the entire library with [assembly: AllowPartiallyTrustedCallers] and modified the web.config with  <add assembly="EPPlus, Version=2.8.0.1, Culture=Neutral, PublicKeyToken=ea159fdaa78159a1"  /> but the result is always System.Security.Permissions.SecurityPermission on Area MyComputer.

I've done the same thing with other library (NPOI) and the trick works.

Is really EPPLUS only be used in full trust ?

Some guru can help me ?

Coordinator
Mar 17, 2011 at 7:01 PM

Hi,

I have not tried to run the component in medium trust level, so I actually don't know.

I had a quick look at the classes that were not allowed and could not see anyone that I use.

When do you get the exception? When the library is loaded?

Jan

Mar 17, 2011 at 9:17 PM

Hi Jan,

the exception is raised as soon as I call

using (ExcelPackage pck = new ExcelPackage)

The full log detail is:

System.Security.Permissions.SecurityPermission ---> System.Security.SecurityException: System.Security.Permissions.SecurityPermission in OfficeOpenXml.ExcelPackage.ConstructNewFile(String password)

The type of the first permission that failed was: System.Security.Permissions.SecurityPermission

Set <trust level="Medium" originUrl=".*" /> in web.config to reproduce the issue.

Ivano.

Coordinator
Mar 18, 2011 at 2:32 PM

Ok, I found the problem (or at least two of them).

I use some API function in the catch block to verifiy if the package is encrypted. In the ExcelWorksheet.ConstructNewFile...

if (password == null && EncryptedPackageHandler.StgIsStorageFile(File.FullName) == 0)

 and in the ExcelWorksheet.Load method....

if (Password == null && EncryptedPackageHandler.StgIsStorageILockBytes(eph.GetLockbyte((MemoryStream)_stream)) == 0)

You can comment these checks out or wrap them in a new method and it will work.
I'll add a fix for this in the next version.
Jan
Mar 18, 2011 at 4:15 PM

Hi Jan,

I've tried your fix and It works like a charm  !!

Thank you for the great great job !!

Ivano

May 23, 2011 at 9:29 AM

I think I've found an other issue on medium trust when the ExcelPackage is created from a memoryStream.

Look at this code:

byte[] buf = new byte[this.uplUsers.PostedFile.InputStream.Length + 1];
//Read file from control to buffer
this.uplUsers.PostedFile.InputStream.Read(buf, 0, buf.Length);
//Create memory stream
MemoryStream memStream = new MemoryStream();
//Write buffer to memory stream
memStream.Write(buf, 0, buf.Length);memStream.Position = 0;

ExcelPackage pck = new ExcelPackage(memStream);  <--- This line raise an exception: System.Security.Permissions.SecurityPermission

Can be solved ?

Thanks, Ivano