文件未找到异常 IIS7

发布于 2025-01-05 09:18:27 字数 3870 浏览 2 评论 0原文

我有一个 C# Web 应用程序,在 VS2010 中运行得非常好,但是当部署到 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();

I have a C# Web app that runs perfectly fine in VS2010, but when deployed to an IIS7 server, returns the "image not found icon".

The piece of code in question essentially grabs a thumbnail of an image on a network shared location, manipulated and then pushed back out to the webpage.

The web server sits on the same network as the files it is trying to access. All users that access this webpage are all on the same local intranet.

The application is a stored list of network saved resources that are categorized in one way or another.

When I deploy the application, it gives the above two errors that I find in my Application Log. I feel that this is a file permissions error and that the two errors are linked, but I do not know where to change the permissions for this to make the application work properly.

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

However, if I take "T:\Published\Generic.jpg" and plug it into my address bar of IE. It loads the image.

The section of code that handles the image is this:

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

I've tried it with both with and without the MapPath method.

I try to debug the application, but because it is working in VS2010, it is not throwing the exception so I don't know why it is being tossed on the IIS server.

Entire Stack Trace as requested:

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: 

Contents of 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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与风相奔跑 2025-01-12 09:18:27

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

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

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

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

编辑:

显然 IIS AppPool 用户在计算机上下文下运行,即 DOMAIN\Machine$ 。请参阅应用程序池标识

I don't think that network drives are available under the context of a service. You will probably have to use a network share notation (such as \\machine-name\share). Additionally you are running under the default user context (IIS APPPOOL\ASP.NET v4.0), which is more difficult to get working in a network setup. You should change the application pool identity to a network user, and give that user access.

Another option is to impersonate the user accessing the application (assuming you are using Windows Authentication).

You can change the application pool identity by right clicking on the application pool and selecting advanced settings. Identity under the Process Model is the setting to change.

In order to enable impersonation, you can go to the application, and select Authentication feature, enable ASP.NET Impersonation, then click Edit.. and ensure that Authenticated user is selected. Impersonation can also work with a particular user identity by using Specific User in this last dialog box, but that is mainly useful when you want to run in the context of a user that normally cannot run as a service.

EDIT:

Apparently the IIS AppPool users run under the machine context, which is DOMAIN\Machine$. See Application Pool Identities.

下雨或天晴 2025-01-12 09:18:27

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

但是,如果您确实遇到了 FileNotFoundException,这可能不是您的问题,所以请发布整个堆栈跟踪。

The IIS7 worker process runs under its own credentials. It will access the file as the identity that runs the Application Pool under which your website runs. This is usually ApplicationPoolIdentity or NetworkService. You need to grant that user access to the file in question.

But if you are really getting a FileNotFoundException that is probably not your problem, so please post the entire stack trace.

別甾虛僞 2025-01-12 09:18:27

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

I think it is because you are accessing the image usin the mapped drive name.
Instead if using T:\Published\Generic.jpg in IIS virtual directory try UNC name \machineName\Published\Generic.jpg

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文