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



我一直在尝试使用Windows身份验证来模拟当前已通过身份验证的用户,该用户正在访问IIS 7上托管的网站,但当我尝试访问单独服务器上的文件时,登录请求仍会显示在服务器的事件日志中,其中包含我试图以ANONYMOUS LOGON:身份访问的文件

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#与.NET框架4)

string path = @"\server1projecttest.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.Prinipal.WindowsIdentity().Name,它会显示出我要模拟的正确域/用户凭据,即我试图访问的文件的权限。

当我通过不在IIS服务器上的浏览器访问web应用程序时,它会要求提供windows凭据,然后当我点击按钮运行上述代码时,登录框会弹出,要求提供windows信息,并且无论我传递什么,都会不断弹出。每次尝试都会在server1上创建一个新事件,并连接ANONYMOUS LOGON。

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

错误为:

Access to the path '\serverprojecttest.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 '\server1projecttest.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.

如有任何指导,我们将不胜感激!

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

最新更新