none
System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0 RRS feed

  • Question

  • Hi,
    I developed a website having feature to upload the images to the website hosting server. When I run the website from my machine (website hosted on my machine) and try to upload the image on online hosting server directory then image file uploads successfuly. But when the website deployed on online hosting server and access the website online and try to upload the image file then it throws the following error:

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

     

     

    I think there is some configuration is required in web.config file to get the required security permission.

    Below is the code which written to perform the above mentioned task.

    public static Boolean Upload(string filename,String strURL)

    {

    FileInfo fileInf = new FileInfo(filename);

    FtpWebRequest reqFTP;

    // Create FtpWebRequest object from the Uri provided

    //reqFTP = DirectCast(FtpWebRequest.Create(New Uri("ftp://" + ftpServerIP + "/" + fileInf.Name)), FtpWebRequest)

    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri + strURL + fileInf.Name));

    // Provide the WebPermission Credintials

    reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPassword);

    // By default KeepAlive is true, where the control connection is not closed

    // after a command is executed.

    reqFTP.KeepAlive = false;

    // Specify the command to be executed.

    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

    // Specify the data transfer type.

    reqFTP.UseBinary = true;

    // Notify the server about the size of the uploaded file

    reqFTP.ContentLength = fileInf.Length;

    // The buffer size is set to 2kb

    int buffLength = 2048;

    byte[] buff = new byte[buffLength];

    int contentLen;

    // Opens a file stream (System.IO.FileStream) to read the file to be uploaded

    FileStream fs = fileInf.OpenRead();

    try

    {

    // Stream to which the file to be upload is written

    Stream strm = reqFTP.GetRequestStream();

    // Read from the file stream 2kb at a time

    contentLen = fs.Read(buff, 0, buffLength);

    // Till Stream content ends

    while (contentLen != 0)

    {

    // Write Content from the file stream to the FTP Upload Stream

    strm.Write(buff, 0, contentLen);

    contentLen = fs.Read(buff, 0, buffLength);

    }

    // Close the file stream and the Request Stream

    strm.Close();

    fs.Close();

    return true;

    }

    catch (Exception ex)

    {

    return false;

    Console.Write(ex.Message);

    }

    }

     

    waiting for the response, i urgently need this solution.
    Thanks

     

    Thursday, May 22, 2008 1:07 PM

Answers

  • Hi Jawad,

     

    Exceptions from the System.Security.Permissions namespace are thrown when code access policies are not met.

    To configure a policy you can use "Admin Tools" -> ".Net Framework v?.? Configuration" or caspol.exe. You will probably need to create a new Permissin Set (duplicate existing is simpler) and add File IO permission and assign this to the appropriate Code Group.

    Check out http://msdn.microsoft.com/en-us/library/930b76w0.aspx for more info.

     

    Regards Ebo

    Thursday, May 22, 2008 10:28 PM

All replies

  • You get exception when you try to read file, or when you try to upload it? If file could not be read maybe you should set proper permission for folder your file exists in. In Windows XP web site is running on Network Service account on Server 2003 on asp.Net user account. Maybe you should set proper user rights for directory.
    Thursday, May 22, 2008 1:20 PM
  • Hi Jawad,

     

    Exceptions from the System.Security.Permissions namespace are thrown when code access policies are not met.

    To configure a policy you can use "Admin Tools" -> ".Net Framework v?.? Configuration" or caspol.exe. You will probably need to create a new Permissin Set (duplicate existing is simpler) and add File IO permission and assign this to the appropriate Code Group.

    Check out http://msdn.microsoft.com/en-us/library/930b76w0.aspx for more info.

     

    Regards Ebo

    Thursday, May 22, 2008 10:28 PM