locked
Security Error! RRS feed

  • Question

  • User1777299414 posted

    Hello,

    when i tried to get the Width of an uploaded image on an asp.net web page,

    i got this error:

    //////////////////////////////////////////////////////

    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 83: 
    Line 84:                 Image img = new Image();
    Line 85:                 System.Drawing.Image img1 = System.Drawing.Image.FromFile(f.FileName);
    Line 86: 
    Line 87:                 //img.ImageUrl=f.FileName;

    Source File: d:\hshome\wavecom\skyplaza-egypt.com\SkyAdmin\Promotions.aspx.cs    Line: 85

    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() +58
       System.Drawing.IntSecurity.DemandReadFileIO(String fileName) +67
       System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) +379382
       System.Drawing.Image.FromFile(String filename) +6
       SkyAdmin_Promotions.btn_update_Click(Object sender, EventArgs e) in d:\hshome\wavecom\skyplaza-egypt.com\SkyAdmin\Promotions.aspx.cs:85
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +6785
       System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +242
       System.Web.UI.Page.ProcessRequest() +80
       System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +21
       System.Web.UI.Page.ProcessRequest(HttpContext context) +49
       ASP.skyadmin_promotions_aspx.ProcessRequest(HttpContext context) in App_Web_8xj_fnk2.10.cs:0
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
    


    Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053

     

    ///////////////////////////////////

    here is the code of the upload fn :

    protected void btn_add_Click(object sender, EventArgs e)

    {

    Page.Validate(
    "main");if (!Page.IsValid)

    {

    return;

    }

    else if (!FileUpload1.HasFile)

    {

    lbl_error.Text = "Please select an image to upload!";

    lbl_error.Visible = true;return;

    }

    try

    {

    HttpPostedFile f = FileUpload1.PostedFile;

    Image img = new Image();

    System.Drawing.Image img1 = System.Drawing.Image.FromFile(f.FileName);

    //img.ImageUrl=f.FileName;

    //img.DataBind();

    if (img1.Width > 700)

    {

    lbl_error.Text =
    "The image width must be less than 700px!";

    lbl_error.Visible = true;

    return;

    }

    FileUpload1.SaveAs(Server.MapPath(
    "~/images/PromotionImages/" + FileUpload1.FileName));

    SqlDataSource_items.InsertParameters.Add("imageurl", TypeCode.String, "~/images/PromotionImages/" + FileUpload1.FileName);

    SqlDataSource_items.InsertParameters.Add("date", TypeCode.DateTime, DateTime.Now.Date.ToString());

    SqlDataSource_items.Insert();

    SqlDataSource_items.InsertParameters.Clear();

     

    lbl_error.Text =
    "The data has been added successfuly.";

    lbl_error.Visible = true;

    txt_title.Text = "";

    FreeTextBox1.Text = "";

    chk_display.Checked = false;img_ad.ImageUrl = "";

    img_ad.DataBind();

    img_ad.Visible = false;

    btn_update.Visible = false;

    btn_clear.Visible = false;

    btn_hide.Visible = false;Panel_edit.Visible = false;

    SqlDataSource_items.DataBind();

    GridView1.DataBind();

    }

    catch

    {

    lbl_error.Text =
    "There is an error while inserting data!";lbl_error.Visible = true;

    }

     

    }

    ///

     can anyone help me!!!.

     

     

    <!-- [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.Drawing.IntSecurity.DemandReadFileIO(String fileName) at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) at System.Drawing.Image.FromFile(String filename) at SkyAdmin_Promotions.btn_update_Click(Object sender, EventArgs e) in d:\hshome\wavecom\skyplaza-egypt.com\SkyAdmin\Promotions.aspx.cs:line 85 at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.skyadmin_promotions_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\f6080f33\1a9c53fa\App_Web_8xj_fnk2.10.cs:line 0 at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) --><!-- This error page might contain sensitive information because ASP.NET is configured to show verbose error messages using <customErrors mode="Off"/>. Consider using <customErrors mode="On"/> or <customErrors mode="RemoteOnly"/> in production environments.-->

     

    Sunday, March 1, 2009 10:50 AM

All replies

  • User2137266510 posted

     Hi,

     

    Can u try it by adding following line --

    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]

    just after your namespace/class declaration starts ??

     

    OR 

    I have one doubt that --You are using following line "HttpPostedFile f = FileUpload1.PostedFile;"  before saving your file by ur FileUpload by this line "FileUpload1.SaveAs(Server.MapPath("~/images/PromotionImages/" + FileUpload1.FileName));"

    I think file should be uploaded first then u can use FileUpload1.PostedFile.After saving this file, u can validate if ur image has  width greater than 700PX then delete it if yes.

    Let me know if this is helpful for you.

     

    Regards

    Ravi

    Monday, March 2, 2009 1:46 AM
  • User1777299414 posted

    Hello,

    when i tried to add 

    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]

    i got the following error :

    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 failed.

    Source Error:

    [No relevant source lines]


    Source File: App_Web_9k3jxeuy.8.cs    Line: 0

     ////////////////

    And when i tried the 2nd solution i got the first error.

    plz help me,

    thank u

    Monday, March 2, 2009 6:09 AM