DotNetZip Exception: System.Security.Permissions.FileIOPermission

May 27, 2011 at 3:31 AM

Hi Experts,

i'm sorry maybe this topic has been posted for many times, but i just couldn't find any solution for this case.

i'm building a webservice that need to provide a list of images for mobile application to download, but for some reasons i need to zip all images into a zip file and send it to mobile application.

Well after googling i found DotNetZip which is very powerful to handle zip code in C#.

So after i downloaded and test it on my project, i got the exception as below :

{System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
   at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
   at System.Security.CodeAccessPermission.Demand()
   at System.IO.File.GetLastWriteTimeUtc(String path)
   at Ionic.Zip.ZipEntry.Create(String nameInArchive, ZipEntrySource source, Object arg1, Object arg2)
   at Ionic.Zip.ZipEntry.CreateFromFile(String filename, String nameInArchive)
   at Ionic.Zip.ZipFile.AddFile(String fileName, String directoryPathInArchive)
   at Ionic.Zip.ZipFile.AddFile(String fileName)
   at GEOTag.Service.TagService.GetGEOTagsRP(String userName, String longitude, String latitude, String range, String courseId) in D:\dev\GEOTag\trunk\GEOTag.Service\TagService.svc.cs:line 471
The action that failed was:
Demand
The type of the first permission that failed was:
System.Security.Permissions.FileIOPermission
The first permission that failed was:
<IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1"
Read="D:\dev\GEOTag\trunk\GEOTag.Service\Images\9cd11de8-d2ad-4fc5-8fe6-02b9cc9af540.jpg"/>

The demand was for:
<IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1"
Read="D:\dev\GEOTag\trunk\GEOTag.Service\Images\9cd11de8-d2ad-4fc5-8fe6-02b9cc9af540.jpg"/>

The granted set of the failing assembly was:
<PermissionSet class="System.Security.PermissionSet"
version="1">
<IPermission class="System.Security.Permissions.FileDialogPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1"
Access="Open"/>
<IPermission class="System.Security.Permissions.IsolatedStorageFilePermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1"
Allowed="ApplicationIsolationByUser"
UserQuota="512000"/>
<IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1"
Flags="Execution"/>
<IPermission class="System.Security.Permissions.UIPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1"
Window="SafeTopLevelWindows"
Clipboard="OwnClipboard"/>
<IPermission class="System.Security.Permissions.StrongNameIdentityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1"
PublicKeyBlob="0024000004800000940000000602000000240000525341310004000001000100D368A9B3F717497EB8BEC03301F7D91BC80FA684DE99D25B34A733959C9874F4A7F89037176E8D30D3DE28403CE49D306E98C89701C145879A302AD5D2D5A8879C62FEB7B9DCFB4BFFCB012BCADF367D7B42D0D249351B9B9B1EC01277739AF165756C35120B9F982CDEE8A03BD09451E8DE927B83442EB8A95F0A5DBCA19AB1"
Name="Ionic.Zip"
AssemblyVersion="1.9.1.5"/>
<IPermission class="System.Security.Permissions.UrlIdentityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1"
Url="file:///D:/dev/GEOTag/trunk/GEOTag.Service/bin/Ionic.Zip.DLL"/>
<IPermission class="System.Security.Permissions.ZoneIdentityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1"
Zone="Internet"/>
<IPermission class="System.Drawing.Printing.PrintingPermission, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
version="1"
Level="SafePrinting"/>
</PermissionSet>

The assembly or AppDomain that failed was:
Ionic.Zip, Version=1.9.1.5, Culture=neutral, PublicKeyToken=edbe51ad942a3f5c
The method that caused the failure was:
Ionic.Zip.ZipEntry Create(System.String, Ionic.Zip.ZipEntrySource, System.Object, System.Object)
The Zone of the assembly that failed was:
Internet
The Url of the assembly that failed was:
file:///D:/dev/GEOTag/trunk/GEOTag.Service/bin/Ionic.Zip.DLL}

 

It got exception when i added a file into dotnetzip object 

string zipFile = "C:\\testing\\images.zip";
if (File.Exists(zipFile))
{
    File.Delete(zipFile);
}

using (ZipFile zip = new ZipFile())
{
    zip.AddFile(imageList[0].ToString()); <------- exception at this step
    zip.Save(zipFile);
}

i've googled many times but got no solution,

what should i do to make it works?

do i need to add module or configure the PowerShell ?

or do i need to make some configuration on the Web.Config ?

or do i need to add authentication on IIS for IO.Permission ?

i'm really confuse please give me some guide....

 

Thx

Stone

Coordinator
May 27, 2011 at 6:47 PM

It looks like your IIS user lacks Read permission on the indicated JPG files.

To avoid the permissions error, You will need to grant permissions to the appropriate IIS user or group.

May 30, 2011 at 2:58 AM

Hi Cheeso,

Thx for your reply,

I've full permission on JPG files as well,

i can guarantee this thing, because before zip images file, my webservice can create images (from database) into "images folder" as well

so after the app created all images, i've to zip all images into zip file.

and when i addin image into DotNetZip object it prompted this error...

Do i need to configure other things??

 

Thx

Stone

Coordinator
May 31, 2011 at 7:21 PM

The exception stack trace you showed is very clear:  it is a permission denied error when trying to get the lastModified time of a file. 

Therefore your guarantee seems unfounded. That you can *create* new files is not a guarantee that you can read all existing files.

I would double-check your assumptions. The stack trace looks pretty unequivocal to me.

 

Jun 1, 2011 at 2:30 AM

The strange things is i try this on my webserver, it works well

but when i try on my local machine, it got exception.

fyi: my webserver's application data all located on inetpub\wwwroot\...

     but my local machine is located on my D:\ and i just regis it on my IIS.

so i wonder if the problem lies on my IIS, but i already give the full permission to "everyone" on IIS,

thats why i asked everyone if there're any other configuration that i have to set, or its related with Windows PowerShell or any others...

 

Thx :)

Stone

Coordinator
Jun 2, 2011 at 1:30 PM

Powershell? 

I thought your code was running in IIS?

Powershell does have some special security constraints when running scripts from networked drives. But I don't see a powershell error in the stack trace.

I'm afraid I don't understand the problem at all now.  It's not clear to me what you are doing or what problem you are having or how powershell is at all related.

 

 

Jun 3, 2011 at 2:54 AM

Well i'm so sorry to caused your confusion, the reason why i asked whether its related with powershell is because

when i was googling got many people on forums suggest to check the Powershell configuration, thats why i'm asking whether its related from my case or not.

 

OK i'll try to explain more detail...

I'm building a web service which is used for a mobile application to retrieve a list of images from my server database.

for this case, i'm using VS 2008 to build the project and SQL Server 2008 as the DBMS.

I'm registering my web service on my local IIS for local testing and when the testing is done, i'll deploy it to my web server.

Basically i'm just retrieve all my images from database and convert it from binary into JPG files and save it on "Images" root directory.

So the mobile application will just retrieve all images by download based on web URL per image (e.x. www.xxxx.com/Images/test.jpg).

But the mobile often got corrupted image, so we decided to zip all images and mobile only need to retrieve a single file instead of a list image files.

Now the case is i'm encountered the error when i'm doing testing on my local testing, but on my web server it works fine.

 

Thats why i'm quite confuse what is the caused of this error, all code, web.config, configuration of security permission is same from my local and webserver.