HttpContext.Current.User.Principal vs WindowsIdentity.GetCurrent()

Lie*_*oen 11 asp.net iis windows-authentication

在HttpContext.Current.User.Principal和WindowsIdentity.GetCurrent()之间打开Windows身份验证和身份模拟的asp.NET环境有什么区别?

Kyl*_*Mit 14

根据这个论坛WindowsIdentity.GetCurrent().NameUser.Identity.Name:

  • User.Identity.Name 表示从IIS传递的标识.
  • WindowsIdentity.GetCurrent().Name 是线程运行的标识.

根据您在IIS中的应用程序身份验证设置,它们将返回不同的值:

| Anonymous | Impersonate | User.Identity.Name | WindowsIndentiy.GetCurrent()  |
|-----------|-------------|--------------------|-------------------------------|
| Yes       | True        | Empty String       | IUSR_<machineName>            |
| Yes       | False       | Empty String       | NT Authority\Network Service  |
| No        | True        | domain\user        | domain\user                   |
| No        | False       | domain\user        | NT Authority\Network Service  |
Run Code Online (Sandbox Code Playgroud)

传奇:

  • 域\用户将显示为:
    • Active Directory的域\用户
    • 本地帐户的machineName\userName
  • NT AUTHORITY\Network服务将显示为:
    • 适用于Windows Server或ASP.NET的NT Authority\Network Service
    • 适用于Windows XP的machineName\ASPNET_WP