Solved

'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0 Error

Posted on 2009-07-08
7
1,796 Views
Last Modified: 2012-05-07
I'm getting the following error:

<hr>

Server Error in '/' Application.
--------------------------------------------------------------------------------

Security Exception
Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Source Error:


Line 25:         String dest="./Pictures/" + imgname; //absolute location of the new image created(thumbnail)
Line 26:         Response.Write(dest + "<br />" + src);
Line 27:         System.Drawing.Image image = System.Drawing.Image.FromFile("./OriginalPics/26 weeks_2.JPG");
Line 28: /*
Line 29:         //- Create a System.Drawing.Bitmap with the desired width and height of the thumbnail.
 

Source File: d:\hosting\3520755\html\modifypicture.aspx.cs    Line: 27

Stack Trace:


[SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
   System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0
   System.Security.CodeAccessPermission.Demand() +59
   System.Drawing.IntSecurity.DemandReadFileIO(String fileName) +68
   System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) +398806
   System.Drawing.Image.FromFile(String filename) +6
   modifypicture.Page_Load(Object sender, EventArgs e) in d:\hosting\3520755\html\modifypicture.aspx.cs:27
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +33
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +47
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +7350
   System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +213
   System.Web.UI.Page.ProcessRequest() +86
   System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +18
   System.Web.UI.Page.ProcessRequest(HttpContext context) +49
   ASP.modifypicture_aspx.ProcessRequest(HttpContext context) in App_Web_xau97sfk.0.cs:0
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +358
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +64
<hr>

I'm new to asp.net and not sure what this error means.  I'm trying to inevitably resize an image that is passed to the page from a querystring and am running into this error.  You can see the logic I'm trying to follow through the commented out code.

Right now it doesn't like the line:
System.Drawing.Image image = System.Drawing.Image.FromFile("./OriginalPics/26 weeks_2.JPG");

which was originally displayed as:
System.Drawing.Image image = System.Drawing.Image.FromFile(src);

If there is a better way to do this i'm open to suggestions.

Thanks!
// From aspx file
 

			<div id="welcome">

				<h2>Success!</h2>

				<p>Your image was successfully uploaded!</p>

				<table width="920" border="0">

				<tr>

				<td width="720">

                    <img src="Pictures/<% Response.Write(Convert.ToString(Request.QueryString["fn"])); %>" /></td>

				<td>

				Tools:

				<ul>

				    <li>resize</li>

				</ul>

				</td>

				</tr>

				</table>

			</div>
 

// From cs codebehind file
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Drawing;

//using System.Drawing.Imaging;

using System.Drawing.Drawing2D; 
 

public partial class modifypicture : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        int thumbWidth = 640;  //Max width allowed

        int thumbHeight = 420; //Max height allowed

        decimal heightdiff;

        decimal widthdiff;

        

        string imgloc;

        string imgname = Convert.ToString(Request.QueryString["fn"]);

        imgloc = "./OriginalPics/" + imgname;

        String src = imgloc; //absolute location of source image
 

        String dest="./Pictures/" + imgname; //absolute location of the new image created(thumbnail) 

        Response.Write(dest + "<br />" + src);

        System.Drawing.Image image = System.Drawing.Image.FromFile("./OriginalPics/26 weeks_2.JPG");

/*

        //- Create a System.Drawing.Bitmap with the desired width and height of the thumbnail.

        int srcWidth=image.Width;

        int srcHeight=image.Height;
 

        if (srcWidth > thumbWidth)  //example sw = 800  tw = 640  

        {

            widthdiff = srcWidth/thumbWidth;  // wd = 1.25

                if(srcHeight > thumbHeight)  //example sh = 500 th = 420

                {

                    heightdiff = srcHeight/thumbHeight;  //hd = 1.19

                        if(widthdiff > heightdiff)  // 1.25 > 1.19

                        {

                            thumbHeight = Convert.ToInt32(srcHeight/widthdiff);  // 800 div 1.25  = 640

                            thumbWidth = Convert.ToInt32(srcWidth/widthdiff);    // 500 div 1.25  = 400

                        }

                        else  // say hd was 1.28

                        {

                            thumbHeight = Convert.ToInt32(srcHeight/heightdiff); //800 div 1.28

                            thumbWidth = Convert.ToInt32(srcWidth/heightdiff);   //500 div 1.28

                        }

                }

                else  // ex sw = 800 sh = 400

                {

                    thumbHeight = Convert.ToInt32(srcHeight/widthdiff);  // 800 div 1.25  = 640

                    thumbWidth = Convert.ToInt32(srcWidth/widthdiff);    // 400 div 1.25  = 320

                }

        }

        else  //example sw = 600

        {

            if(srcHeight > thumbHeight)  // sh = 450

            {

                heightdiff = srcHeight/thumbHeight;  // wd = 1.07

                thumbHeight = Convert.ToInt32(srcHeight/heightdiff);  // 600 div 1.07  = 560

                thumbWidth = Convert.ToInt32(srcWidth/heightdiff);    // 450 div 1.07  = 420

            }

            else

            {

                thumbHeight = srcHeight;

                thumbWidth = srcWidth;

            }

        } 

        Response.Write(thumbHeight + " = thumbheight<br />" + thumbWidth + " = thumbWidth<br />" + dest 

            + " = dest<br />" + imgloc + " = imgloc<br />" + src + " = src<br />");

        

        Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);
 

        //- Create a System.Drawing.Graphics object from the Bitmap which we will use to draw the high quality scaled image

        System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
 

        //- Set the System.Drawing.Graphics object property SmoothingMode to HighQuality

        gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality ;
 

        //- Set the System.Drawing.Graphics object property CompositingQuality to HighQuality

        gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
 

        //- Set the System.Drawing.Graphics object property InterpolationMode to High

        gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
 

        //- Draw the original image into the target Graphics object scaling to the desired width and height

        System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);

        gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
 

        //- Save to destination file

        bmp.Save(dest);
 

        //- dispose / release resources

        bmp.Dispose();

        image.Dispose(); 

        */

    }

}

Open in new window

0
Comment
Question by:rondre
  • 4
  • 3
7 Comments
 
LVL 22

Expert Comment

by:prairiedog
ID: 24813490
Is this happening on your production server or local machine?
0
 

Author Comment

by:rondre
ID: 24813734
This is happening on a GoDaddy server that is hosting my page.
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 24813953
It seems to me that your asp.net application does not have enough permission to read a file from a folder. In this case, the folder is "OriginalPics". You should contact GoDaddy's technical support to get permission for your application.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 22

Expert Comment

by:prairiedog
ID: 24813972
Also, if you need to save the resized image, then you will need to make sure your application has "write" permission to the destination folder.
0
 

Author Comment

by:rondre
ID: 24814946
there are 3 total folders that I have given read and write permissions to from the control panel in godaddy.

OriginalPics is the folder that the image is uploaded to - this works fine.  From the upload page I then re-direct to a modifypicture page where I was planning on having tools to mess w/ the image.  The re-direct passes the name of the uploaded file through the url and I'm trying to resize the uploaded image from the previous page and store it in a Pictures folder.  I'm also going to have a 3rd folder called thumbnails as well.  The site is basically going to be for a landscape business where they can upload a picture to have the original size in the OriginalPics folder so they can access that if they need a large image.  There is a page that will use some javascript to do a slideshow and as you'll see from the code the image window will allow a max image size of 640x420 so i have thumbnail sizing (currently just for the 640x420 sizing but will do it for the small thumbnails also).

Hopefully this info will help - do you think that the "permissions" info from the GoDaddy control panel in their "folder options" section is not enough?  I can get a screenshot of that if that will help.

Thanks,
0
 
LVL 22

Accepted Solution

by:
prairiedog earned 500 total points
ID: 24815029
The control panel should set the right permission.

Another thing:

try using an absolute path in System.Drawing.Image.FromFile, instead of a relative path. You can get the actually file path by using Server.MapPath(YourRelativePath), maybe it will fix the error.
0
 

Author Comment

by:rondre
ID: 24816284
You rock prairiedog - had to apply that to both src and dest but the process works perfect now!

thank you so much!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now