【发布时间】: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