我在 VS 2005 中创建了一个空白测试应用程序作为 ASP.NET 应用程序。MSDN说
默认情况下,ASP.NET 不使用模拟,您的代码使用 ASP.NET 应用程序的进程标识运行。
我有以下 web.config
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true" defaultLanguage="c#" />
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows"/>
<identity impersonate="false"/>
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
</system.web>
</configuration>
因此,就像文章建议的那样,模拟似乎已被禁用。
我的 aspx 默认为空白,代码隐藏为
namespace TestWebapp
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine(String.Format("Before1: Current Princupal = {0}", Thread.CurrentPrincipal.Identity.Name));
WindowsImpersonationContext ctx = WindowsIdentity.Impersonate(IntPtr.Zero);
try
{
int a = 0;
System.Diagnostics.Debug.WriteLine(String.Format("After: Current Princupal = {0}", Thread.CurrentPrincipal.Identity.Name));
} finally
{
ctx.Undo();
}
}
}
}
当我重新加载页面时,我得到以下调试输出:
[5288] 之前 1:当前主体 = 域\用户 [5288] 之后:当前主体 = 域\用户
输出与
<identity impersonate="false"/>
该网站使用默认应用程序池,并且该池设置为使用 NETWORK SERVICE 帐户用于其工作进程。我确定应用程序使用它应该使用的 web.config,并且 w3p.exe 工作进程正在网络服务下运行。
在这种情况下有什么问题?
谢谢!
@Edit:Rob,感谢您的提示!$user 快捷方式告诉我一切都在按我的预期发生:通过模拟,我有运行用户 NT AUTHORITY\NETWORK SERVICE 的进程,并且线程在 WindowsIdentity.Impersonate(IntPtr.Zero) 和“No Token.Thread”之前具有 DOMAIN\User不冒充。” 后。但是 Thread.CurrentPrincipal.Identity.Name 和 HttpContext.Current.User.Identity.Name 在这两个地方仍然给我 DOMAIN\User 。
@Edit:我发现要更改 Thread.CurrentPrincipal 和 HttpContext.Current.User 我必须手动进行:
Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
HttpContext.Current.User = Thread.CurrentPrincipal;
我不确定这里有什么意义,但无论如何。我现在对 sharepoint 共享服务管理用户配置文件权限有疑问,但这是另一个问题。