gpt4 book ai didi

ASP.NET 模拟 NT AUTHORITY\IUSR 但模拟被禁用。 ASP.NET 错误?

转载 作者:行者123 更新时间:2023-12-02 07:42:35 25 4
gpt4 key购买 nike

我有一个在 Windows 7/IIS 7.5 上的“ASP.NET v4.0 Classic”应用程序池中运行的 ASP.NET 4.0 应用程序,该应用程序配置为作为网络服务运行。该应用程序有一个 Application_EndRequest连接到本地 SQL Server 实例的处理程序。 SQL 连接字符串指定 Integrated Security=SSPI 。 Web.config 没有<identity impersonate="true" /> .

当我浏览到http://localhost/TestSite/时,抛出以下异常:

System.Data.SqlClient.SqlException (0x80131904): Login failed for user 'NT AUTHORITY\IUSR'.
...
at System.Data.SqlClient.SqlConnection.Open()
at Global.Application_EndRequest(Object sender, EventArgs e)

当我浏览到 http://localhost/TestSite/default.aspx不会抛出此异常(IIS 中配置的默认文档)或任何其他 .aspx 页面;在这些情况下,应用程序会以“NT AUTHORITY\NETWORK SERVICE”正确连接到 SQL Server,这是一个有效的登录名。

即使禁用模拟,为什么 ASP.NET 仍会在 EndRequest 中模拟“NT AUTHORITY\IUSR”?这是 ASP.NET 中的错误吗?

以下 Global.asax.cs 文件演示了该问题:

public class Global : HttpApplication
{
public Global()
{
this.BeginRequest += delegate { Log("BeginRequest"); };
this.PreRequestHandlerExecute += delegate { Log("PreRequestHandlerExecute"); };
this.PostRequestHandlerExecute += delegate { Log("PostRequestHandlerExecute"); };
this.EndRequest += delegate { Log("EndRequest"); };
}

protected void Application_EndRequest(Object sender, EventArgs e)
{
try
{
using (SqlConnection connection = new SqlConnection("Server=.;Integrated Security=SSPI"))
{
connection.Open();
}
}
catch (Exception ex)
{
Trace.WriteLine(ex);
}
}

private static void Log(string eventName)
{
HttpContext context = HttpContext.Current;
Type impersonationContextType = typeof(HttpContext).Assembly.GetType("System.Web.ImpersonationContext", true);
Trace.WriteLine(string.Format("ThreadId={0} {1} {2} Impersonating={3}",
Thread.CurrentThread.ManagedThreadId,
context.Request.Url,
eventName,
impersonationContextType.InvokeMember("CurrentThreadTokenExists", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetProperty, null, context, null)));
}
}

这是跟踪输出:

ThreadId=3 http://localhost/TestSite/ BeginRequest Impersonating=False
ThreadId=3 http://localhost/TestSite/ PreRequestHandlerExecute Impersonating=False
ThreadId=7 http://localhost/TestSite/default.aspx BeginRequest Impersonating=False
ThreadId=7 http://localhost/TestSite/default.aspx PreRequestHandlerExecute Impersonating=False
ThreadId=7 http://localhost/TestSite/default.aspx PostRequestHandlerExecute Impersonating=False
ThreadId=7 http://localhost/TestSite/default.aspx EndRequest Impersonating=False
ThreadId=7 http://localhost/TestSite/ PostRequestHandlerExecute Impersonating=True
ThreadId=7 http://localhost/TestSite/ EndRequest Impersonating=True
System.Data.SqlClient.SqlException (0x80131904): Login failed for user 'NT AUTHORITY\IUSR'.
...
at System.Data.SqlClient.SqlConnection.Open()
at Global.Application_EndRequest(Object sender, EventArgs e)

请注意,对 TestSite/ 的请求(映射到 DefaultHttpHandler )似乎产生了一个对 TestSite/default.aspx嵌套请求(映射到 ASP.default_aspx )。 ASP.NET 完成处理后 TestSite/default.aspx ,当它恢复处理 TestSite/ 的请求时,它会模拟“NT AUTHORITY\IUSR” .

更新:我已将此问题提交至 Microsoft Connect .

最佳答案

很可能,您的服务器、站点或应用程序的设置已设置为“匿名身份验证”模式导致页面请求以 IUSR 用户身份进行处理。您的应用程序不请求模拟并不重要; IIS 正在强制执行此操作。 (顺便说一句,“模拟”是 Windows 领域的通用术语,指的是假定另一个用户的凭据。它不是 ASP.NET 所特有的。)

一些背景知识:

出于安全原因,IIS 允许您的服务器在不同的系统凭据下处理“匿名”和“经过身份验证”的请求。

现在,在 IIS 7.5 中,如果您同时启用了匿名身份验证和表单例份验证(这是典型的),那么在您的网站用户通过表单登录之前,它会认为您的用户是“匿名”。当您的用户使用 Forms Auth 登录后,它会认为您的用户“已通过身份验证”。

一开始我发现这种行为令人困惑,因为它是 IIS 6.0 的一项更改,IIS 6.0 不支持 Forms 身份验证,并认为所有经过 Forms 身份验证的用户都是匿名的!

如果需要,您可以更改处理匿名请求的身份。我的偏好(听起来也像您的偏好)是它们在与我网站的应用程序池相同的系统凭据下运行。要让 IIS 执行此操作,请执行以下操作:

  1. 打开 IIS 管理器 (inetmgr)
  2. 在“连接”面板中,深入找到您网站的节点并选择它
  3. 在右侧面板的“IIS”组下,双击“身份验证”图标。
  4. 右键点击“匿名身​​份验证”,然后从上下文菜单中选择“编辑...”。
  5. 在弹出对话框中,选择“应用程序池标识”单选按钮。
  6. 点击“确定”。

第 5 步一开始也让我感到困惑,因为我认为“应用程序池身份”意味着“应用程序池伪帐户”(IIS 7.5 中的另一个新功能)。当然,它真正的含义是“应用程序池配置运行的帐户,无论是什么。”

如果您希望该服务器上的所有网站默认采用此行为,甚至只是特定站点下的单个应用程序,您也可以在这些级别配置身份验证选项。只需在“连接” Pane 中选择要配置的节点,然后重复步骤 3-6。

关于ASP.NET 模拟 NT AUTHORITY\IUSR 但模拟被禁用。 ASP.NET 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10517764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com