简体   繁体   English

通过IIS 7上的.NET Web App访问网络文件

[英]Access network file via .NET Web App on IIS 7

I have been trying to use Windows Authentication to impersonate the current authenticated user who is accessing the web site hosted on IIS 7, but when I try to access a file on a separate server the log in requests are still appearing in the event log of the server with the file I am trying to access as ANONYMOUS LOGON: 我一直试图使用Windows身份验证来模拟正在访问IIS 7上托管的网站的当前经过身份验证的用户,但是当我尝试访问单独服务器上的文件时,登录请求仍会出现在Windows Server 2003的事件日志中。我试图以匿名登录访问该文件的服务器:

An account was successfully logged on.

Subject:
    Security ID:        NULL SID
    Account Name:       -
    Account Domain:     -
    Logon ID:       0x0

Logon Type:         3

New Logon:
    Security ID:        ANONYMOUS LOGON
    Account Name:       ANONYMOUS LOGON
    Account Domain:     NT AUTHORITY
    Logon ID:       0x1e47ea9
...

If I run the WebApp from either the dev enviorment or on local host on the IIS server directly it uses the correct user credentials and works just fine. 如果我从开发环境或直接在IIS服务器上的本地主机上运行WebApp,它将使用正确的用户凭据,并且运行良好。

my Web.config has 我的Web.config有

<authentication mode="Windows" />
    <identity impersonate="true" />

In IIS Authentication I have Anonymous authentication disabled, Asp .NET impersonation enabled, and windows authentication enabled. 在IIS身份验证中,我禁用了匿名身份验证,启用了Asp .NET模拟,并启用了Windows身份验证。

In the code I have tried (c# with .NET framework 4) 在我尝试过的代码中(使用.NET Framework 4的C#)

string path = @"\\server1\project\test.csv";
            ((WindowsIdentity)HttpContext.Current.User.Identity).Impersonate();
            StreamWriter sw = File.AppendText(path);

also

using (HostingEnvironment.Impersonate(WindowsIdentity.GetCurrent().Token))
{
//code
}



using (HostingEnvironment.Impersonate())
{
//code
}

and a couple other various suggestions I've found posted. 以及我发现的其他一些建议。

If I check System.Security.Principal.WindowsIdentity.GetCurrent().Name without any direct impersonation lines in the code it shows up with the correct domain/user credentials that I want to be impersonating, who has the rights to the file I am trying to reach. 如果我在代码中没有任何直接模拟行的情况下检查System.Security.Principal.WindowsIdentity.GetCurrent()。Name,它将显示我要模拟的正确域/用户凭据,谁对该文件具有权限试图达到。

When I access the web app via a browser not on the IIS server, it asks for windows credentials and then when I hit a button to run the above code the log in box pops up, asks for windows information, and keeps popping up no matter what I pass it. 当我通过不在IIS服务器上的浏览器访问该Web应用程序时,它会要求Windows凭据,然后当我按下按钮来运行上述代码时,将弹出“登录”框,询问Windows信息,并且无论如何一直弹出我通过了什么。 Each attempt creates a new event on server1 with the ANONYMOUS LOGON connecting. 每次尝试都会在server1上使用ANONYMOUS LOGON连接创建一个新事件。

From what I read it might be a double hop issue authentication issue, though mostly those posts are regarding SQL servers rather than a simple file share. 根据我的阅读,这可能是一个双跳问题身份验证问题,尽管大多数这些帖子都是关于SQL Server而不是简单的文件共享的。

The error is: 错误是:

Access to the path '\\server\project\test.csv' is denied.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.UnauthorizedAccessException: Access to the path '\\server1\project\test.csv' is denied. 

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user. 

To grant ASP.NET access to a file, right-click the file in File Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

Any guidance would be appreciated! 任何指导将不胜感激!

The exception tells you that you do not have the right to access the resource. 该异常告诉您您无权访问资源。 You have a user which runs your project. 您有一个运行项目的用户。 Give the necessary privileges to the given resource to the user which runs the project. 将给定资源的必要特权授予运行项目的用户。 If you do not trust that user of the OS, run your project with a trusted user. 如果您不信任该操作系统的用户,请与受信任的用户一起运行项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何允许 .NET Core Web 应用访问网络驱动器? - How to allow a .NET Core web app access to network drives? 从asp.net应用程序访问网络共享。 在Visual Studio中工作,但在IIS中工作,为什么? - Access a network share from an asp.net app. Works in Visual Studio but not in IIS, why? 如何在IIS 6.0中正确启用匿名访问并通过c#console app使用MVC Web API? - How to properly enable Anonymous Access in IIS 6.0 & consume an MVC Web API via c# console app? ASP.NET Core 2.1 App在本地构建和运行成功,但无法通过Web Deploy发布到IIS - ASP.NET Core 2.1 App succeeds in Building and Running Locally but fails to Publish to IIS via Web Deploy 在IIS上更新.NET Core Web App - Update .net core web app on iis IIS的.Net Web Application app启动工作流程 - .Net Web Application app startup workflow for IIS 授予IIS Web应用程序访问网络驱动器上文件的权限 - Give IIS web application permission to access files on network drive ASP.Net“访问路径&#39;&#39;被拒绝。”文件上传控制在Web服务器(IIS7)中不起作用 - ASP.Net “Access to the path '' is denied.”File Upload Control not Working in Web Server (IIS7) IIS应用程序池访问网络MSMQ C# - IIS app pool access network MSMQ c# 使用ASP.net MVC将文件上传到Azure iis Web应用程序失败 - Uploading file to azure iis web app using ASP.net mvc fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM