【问题标题】:Access network file via .NET Web App on IIS 7通过 IIS 7 上的 .NET Web App 访问网络文件
【发布时间】:2013-07-08 14:31:24
【问题描述】:

我一直在尝试使用 Windows 身份验证来模拟正在访问托管在 IIS 7 上的网站的当前经过身份验证的用户,但是当我尝试访问单独服务器上的文件时,登录请求仍然出现在事件中带有我试图以匿名登录身份访问的文件的服务器日志:

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
...

如果我从开发环境或 IIS 服务器上的本地主机直接运行 WebApp,它使用正确的用户凭据并且工作正常。

我的 Web.config 有

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

在 IIS 身份验证中,我禁用了匿名身份验证,启用了 Asp .NET 模拟,并启用了 Windows 身份验证。

在我尝试过的代码中(c# with .NET framework 4)

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

还有

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



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

以及我发现的其他一些不同的建议。

如果我检查 System.Security.Principal.WindowsIdentity.GetCurrent().Name 而代码中没有任何直接模拟行,它会显示我想要模拟的正确域/用户凭据,谁有权访问我正在尝试访问的文件。

当我通过不在 IIS 服务器上的浏览器访问 Web 应用程序时,它会询问 Windows 凭据,然后当我点击按钮运行上述代码时,会弹出登录框,询问 Windows 信息,并不断弹出不管我通过它。每次尝试都会在 server1 上创建一个新事件,并连接 ANONYMOUS LOGON。

根据我的阅读,这可能是一个双跳问题身份验证问题,尽管这些帖子大多是关于 SQL 服务器而不是简单的文件共享。

错误是:

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.

任何指导将不胜感激!

【问题讨论】:

    标签: c# .net iis networking impersonation


    【解决方案1】:

    异常告诉您您无权访问该资源。您有一个运行您的项目的用户。将给定资源的必要权限授予运行项目的用户。如果您不信任该操作系统用户,请使用受信任的用户运行您的项目。

    【讨论】:

    • 当我在 web 服务器上本地运行项目时,windows 身份验证使用我登录的帐户,该帐户对该对象具有权限。但是,当我从另一台计算机访问托管在 Web 服务器上的网站时,它会要求提供凭据,它会通过 Web 服务器的身份验证,但无法访问网络上的文件,并且网络服务器上的日志显示没有凭据路过
    • 一切都与用户权限有关。如果您允许每个人在给定位置访问,您可能会发现您可以做您需要的事情。允许任何人访问是不安全的,但您应该确定谁是受信任的用户,谁需要给定的权限。看我的回答。
    猜你喜欢
    • 2018-05-14
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    相关资源
    最近更新 更多