Thread.CurrentPrincipal.Identity vs HttpContext.User.Identity

bev*_*qua 14 .net c# asp.net current-principal form-authentication

可能重复:
http.context.user和thread.currentprincipal之间的区别以及何时使用它们?

这两个在ASP.NET应用程序中有什么区别?

我知道在HttpContext.User.Identity用户通过身份验证时设置了FormsAuthentication.但什么时候Thread.CurrentPrincipal.Identity设置?

他们总是保持相同的价值吗?

对于无法访问的应用程序的其他层,这仍然适用HttpContext吗?

Cha*_*thJ 10

HttpContext.User.Identity是您的Web应用程序中当前登录的用户.

Thread.CurrentPrincipal仅适用于<authentication mode = "windows"/>.通常这适用于基于Windows的应用程序(Winforms,WPF ..)

  • 在Forms身份验证中,Thread.CurrentPrincipal可以从HttpContext.User.Identity中取消同步.请参阅[Hanselman]的这篇旧博客(http://www.hanselman.com/blog/SystemThreadingThreadCurrentPrincipalVsSystemWebHttpContextCurrentUserOrWhyFormsAuthenticationCanBeSubtle.aspx).另见这个更新的[stackoverflow](http://stackoverflow.com/a/16693746) (2认同)