【问题标题】:FileNotFoundException IIS7FileNotFoundException IIS7
【发布时间】:2012-02-13 23:22:18
【问题描述】:

我有一个在 VS2010 中运行良好的 C# Web 应用程序,但是当部署到 IIS7 服务器时,返回“找不到图像图标”。

有问题的代码实际上是在网络共享位置上抓取图像的缩略图,对其进行操作,然后将其推回网页。

Web 服务器与其尝试访问的文件位于同一网络上。所有访问该网页的用户都在同一个本地内网。

该应用程序是以一种或另一种方式分类的网络保存资源的存储列表。

当我部署应用程序时,它给出了我在应用程序日志中发现的上述两个错误。感觉这是文件权限错误,而且这两个错误是有联系的,但是不知道在哪里修改这个权限才能让应用正常工作。

Exception information: 
Exception type: FileNotFoundException 
Exception message: T:\Published\Generic.jpg

但是,如果我将“T:\Published\Generic.jpg”插入到我的 IE 地址栏中。它会加载图像。

处理图像的代码部分是这样的:

System.Drawing.Image img;
img = System.Drawing.Image.FromFile(MapPath(Request.QueryString["File"].ToString()));

我已经尝试过使用和不使用 MapPath 方法。

我尝试调试应用程序,但是因为它在 VS2010 中运行,所以它没有抛出异常,所以我不知道为什么它会被扔到 IIS 服务器上。

根据要求进行整个堆栈跟踪:

Event code: 3005 
Event message: An unhandled exception has occurred. 
Event time: 13/02/2012 4:16:26 PM 
Event time (UTC): 13/02/2012 11:16:26 PM 
Event ID: 1f01693f71a2443790a8d83ba06a88a4 
Event sequence: 12 
Event occurrence: 1 
Event detail code: 0 

Application information: 
Application domain: /LM/W3SVC/2/ROOT-3-129736485835718008 
Trust level: Full 
Application Virtual Path: / 
Application Path: C:\inetpub\wwwroot\
Machine name: XXXXXX

Process information: 
Process ID: 10768 
Process name: w3wp.exe 
Account name: IIS APPPOOL\ASP.NET v4.0 

Exception information: 
Exception type: FileNotFoundException 
Exception message: T:\Published\Generic.jpg
at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)
at imagedrawer.Page_Load(Object sender, EventArgs e)
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)



Request information: 
Request URL: http://localhost/imagedrawer.aspx?File=T:\Published\Generic.jpg 
Request path: /imagedrawer.aspx 
User host address: ::1 
User:  
Is authenticated: False 
Authentication Type:  
Thread account name: IIS APPPOOL\ASP.NET v4.0 

Thread information: 
Thread ID: 64 
Thread account name: IIS APPPOOL\ASP.NET v4.0 
Is impersonating: False 
Stack trace:    at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)
at imagedrawer.Page_Load(Object sender, EventArgs e)
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


Custom event details: 

imagedrawer.aspx的内容:

 System.IO.MemoryStream ms = new System.IO.MemoryStream();
        System.Drawing.Image img;

        img = System.Drawing.Image.FromFile(MapPath(Request.QueryString["File"].ToString()));


        if (img.Height > 80 || img.Width > 80)
        {
            System.Drawing.RectangleF RF = new System.Drawing.RectangleF();
            RF.X = 0;
            RF.Y = 0;
            RF.Height = (img.Height < 80) ? img.Height : 80;
            RF.Width = (img.Width < 80) ? img.Width : 80;
            System.Drawing.Bitmap bmthumb = (System.Drawing.Bitmap)img.Clone();
            System.Drawing.Bitmap bmCrop = bmthumb.Clone(RF, bmthumb.PixelFormat);
            img = (System.Drawing.Image)bmCrop;
        }
        img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);


        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Request.QueryString["File"].ToString());
        Response.AddHeader("Content-Length", ms.ToArray().Length.ToString());
        Response.ContentType = "image/jpeg";
        Response.BinaryWrite(ms.ToArray());
        Response.End();
        img.Dispose();

【问题讨论】:

    标签: c# asp.net iis-7


    【解决方案1】:

    我认为网络驱动器在服务上下文中不可用。您可能必须使用网络共享表示法(例如\\machine-name\share)。此外,您在默认用户上下文 (IIS APPPOOL\ASP.NET v4.0) 下运行,这在网络设置中更难工作。您应该将应用程序池标识更改为网络用户,并授予该用户访问权限。

    另一种选择是模拟访问应用程序的用户(假设您使用的是 Windows 身份验证)。

    您可以通过右键单击应用程序池并选择高级设置来更改应用程序池标识。流程模型下的身份是要更改的设置。

    为了启用模拟,您可以转到应用程序,选择身份验证功能,启用 ASP.NET 模拟,然后单击编辑.. 并确保选择了已验证用户。模拟也可以通过在最后一个对话框中使用特定用户来处理特定用户身份,但这主要在您希望在通常不能作为服务运行的用户的上下文中运行时有用。

    编辑:

    显然 IIS AppPool 用户在机器上下文下运行,即DOMAIN\Machine$。见Application Pool Identities

    【讨论】:

    • @JKM,当您使用 IE 访问共享时,IE 会使用您的凭据。如果 IE 工作,它只是证明您有权访问共享。但是,您在 IIS/ASP.NET 上的应用程序在应用程序池标识下运行(如 Guvante 所说),因此发生的任何错误都表明池标识无权访问该共享(必须使用 UNC 路径,每 support.microsoft.com/kb/257174) .您需要授予池身份访问共享的权限,或将池身份更改为可以访问共享的帐户。下次请多注意用户上下文。
    • 将解决方案部署到目标机器并设置正确的应用程序池服务后,它不再出现此错误。
    【解决方案2】:

    IIS7 工作进程在其自己的凭据下运行。它将以运行您的网站所在的应用程序池的身份访问该文件。这通常是ApplicationPoolIdentityNetworkService。您需要授予该用户对相关文件的访问权限。

    但如果你真的得到FileNotFoundException,那可能不是你的问题,所以请发布整个堆栈跟踪。

    【讨论】:

    • @JKM 我最好的猜测是驱动器号T 在服务器上没有正确映射。
    • @JKM 我建议使用 UNC 路径,例如 \\servername\folder\file.name,而不是映射驱动器。
    • 我已将其映射到共享驱动器,我认为通常也是如此,但是当我获取地址并将其弹出到 IE 中时。图像显示出来,这告诉我它已正确映射到服务器上。这就是让我困惑的部分。
    • UNC 路径名给我同样的错误。我都试过了,认为它也可能是这样的。
    • @JKM 当you 将它放入浏览器时,它会显示you 是否为you 映射了该驱动器。该网站不会像您一样运行。尝试按照我上面的建议通过 UNC 路径访问它。
    【解决方案3】:

    我认为这是因为您正在使用映射的驱动器名称访问图像。 相反,如果在 IIS 虚拟目录中使用 T:\Published\Generic.jpg,请尝试 UNC 名称 \machineName\Published\Generic.jpg

    【讨论】:

    • 你可以通过输入图片的URL来浏览图片吗?无需通过您的 imagedrawer.aspx?
    猜你喜欢
    • 2012-12-22
    • 2011-12-10
    • 2013-12-09
    • 2011-06-09
    • 2016-04-05
    • 2018-07-11
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多