我在 VS 2005 中创建了一个空白测试应用程序作为 ASP.NET 应用程序。 MSDN says那个
By default, ASP.NET does not use impersonation, and your code runs using the ASP.NET application's process identity.
我有以下 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>
看来模拟已被禁用,就像 the article 一样正在建议。
我的 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] Before1: Current Princupal = DOMAIN\User [5288] After: Current Princupal = DOMAIN\User
输出与
相同<identity impersonate="false"/>
该网站使用默认应用程序池,并且该池设置为其工作进程使用 NETWORK SERVICE 帐户。 我确信应用程序使用它应该使用的 web.config,并且 w3p.exe 工作进程正在网络服务下运行。
这种情况下可能出现什么问题?
谢谢!
@编辑:罗布,谢谢你的提示! $user 快捷方式显示一切都按照我的预期发生:通过模拟,我的进程运行用户 NT AUTHORITY\NETWORK SERVICE,并且线程在 WindowsIdentity.Impersonate(IntPtr.Zero) 和“无 token 。线程”之前具有 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 共享服务管理用户配置文件权限方面遇到问题,但这是另一个问题。
最佳答案
我想我理解你的问题。
继续前进之前需要了解的事情
应用程序运行时存在不同的安全上下文。就像 System.Security.Principal.WindowsIdentity.GetCurrent().Name 以及您上面提到的,即 System.Threading.Thread.CurrentPrincipal.Identity.Name p>
在 Web 应用程序中,
System.Threading.Thread.CurrentPrincipal.Identity
始终由HttpContext.Current.User.Identity
提供。
说到你的重点了。如果您想修改 System.Threading.Thread.CurrentPrincipal.Identity,请修改您的身份验证机制最初提供的 HttpContext.Current.User.Identity。
关于尽管禁用模拟,但 ASP.NET aspx 页面代码仍以模拟方式运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/247550/