System.UnauthorizedAccessException对一个简单的ASP.Net File.IO操作

时间:2009-06-24 10:21:51

标签: c# asp.net

在ASP.NET(托管)中,我需要一个简单的文件IO操作来从文件中读取数据,然后强制下载。真的很简单。

执行以下代码时,我不断获得System.UnauthorizedAccessException

System.IO.FileStream fs = 
      new System.IO.FileStream(path, System.IO.FileMode.Open);

在本地它工作正常,但当我上传到共享主机帐户时,我得到上述例外。

奇怪的是,如果我输入文件的完整路径,在浏览器中,我可以看到并使用该文件。

5 个答案:

答案 0 :(得分:1)

正如其他人提到的那样,这很可能是因为ASP进程没有访问文件目录的安全权限。

如果您可以通过浏览器访问该文件,也许您可​​以使用HttpWebRequest而不是File.Open来读取该文件。

如果您没有管理员控制服务器,这将是有意义的。

以下是一些使用HttpWebRequest的示例代码,如果它有帮助:

    /// <summary>
    /// Submits a request to the specified url and returns the text response, or string.Empty if the request failed.
    /// </summary>
    /// <param name="uri">The url to submit the request to.</param>
    /// <returns>The text response from the specified url, or string.Empty if the request failed.</returns>
    protected string getPageResponse(string url)
    {
        //set the default result
        string responseText = string.Empty;

        //create a request targetting the url
        System.Net.WebRequest request = System.Net.HttpWebRequest.Create(url);
        //set the credentials of the request
        request.Credentials = System.Net.CredentialCache.DefaultCredentials;

        //get the response from the request
        System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
        //get the response code (format it as a decimal)
        string statusCode = response.StatusCode.ToString("d");

        //only continue if we had a succesful response (2XX or 3XX)
        if (statusCode.StartsWith("2") || statusCode.StartsWith("3"))
        {
            //get the response stream so we can read it
            Stream responseStream = response.GetResponseStream();
            //create a stream reader to read the response
            StreamReader responseReader = new StreamReader(responseStream);
            //read the response text (this should be javascript)
            responseText = responseReader.ReadToEnd();
        }

        //return the response text of the request
        return responseText;
    }

答案 1 :(得分:1)

您应该使用IsolatedStorage进行文件操作。

What Is Isolated Storage?

运行具有有限权限的代码有很多好处,因为存在掠夺者,他们会在用户身上制造病毒和间谍软件。 .NET Framework有几种机制来处理运行权限最少的用户。因为大多数应用程序必须以持久的方式处理它们的某些状态(不使用数据库或其他方法),所以有一个地方来存储可以安全使用的信息而不必测试应用程序是否有用足够的权限将数据保存到硬盘驱动器。该解决方案是隔离存储旨在提供的解决方案。

通过使用隔离存储来保存数据,您可以访问存储信息的安全位置,而无需让用户授予对文件系统中特定文件或文件夹的访问权限。使用独立存储的主要好处是,无论应用程序是在部分,有限还是完全信任下运行,您的应用程序都将运行。

您可以参考IsolatedStorageFileStream

答案 2 :(得分:0)

您的问题可能是ASP.NET进程需要对指定路径的访问权限。

答案 3 :(得分:0)

您的asp.net应用程序在名为ASPNET的用户的安全上下文中运行,因此,为了访问系统上的任何资源,必须授予该用户访问这些资源的权限。

答案 4 :(得分:0)

您在path变量中传递的路径是什么?您说如果您将路径放入浏览器并且可以访问该文件,则表明该路径包含http://...。要使用System.IO.FileStream,您需要传递具有驱动器号或UNC路径的路径。

也许你应该这样做:

// if file is in root of site...
string path = Server.MapPath("/myfile.txt"); 
// or if file is in /myfiles folder    
string path = Server.MapPath("/myfiles/myfile.txt"); 
System.IO.FileStream fs = 
      new System.IO.FileStream(path, System.IO.FileMode.Open);