14

Possible Duplicate:
difference between http.context.user and thread.currentprincipal and when to use them?

What's the difference between these two in an ASP.NET application?

I know the HttpContext.User.Identity is set when the user is authenticated through FormsAuthentication. But when is the Thread.CurrentPrincipal.Identity set?

Do they always hold the same value?

Does that still hold true for other layers of the application that do not have access to a HttpContext?

Community
  • 1
  • 1
bevacqua
  • 43,414
  • 51
  • 157
  • 277

2 Answers2

9

HttpContext.User.Identity is the current logged in user in your web app.

Thread.CurrentPrincipal applies only when the <authentication mode = "windows"/>. Normally this is using with Windows based applications (Winforms,WPF..)

CharithJ
  • 42,659
  • 20
  • 104
  • 124
  • 1
    `Thread.CurrentPrincipal` still works with `mode="forms"`... – bevacqua Jul 25 '11 at 01:06
  • 1
    @Nico : But it doesn't give you the currently login user. It will give you the windows user details instead. So, in this case Thread.CurrentPrincipal doesn't make much sense. – CharithJ Jul 25 '11 at 01:26
  • 2
    In Forms Authentication the Thread.CurrentPrincipal can become unsynched from the HttpContext.User.Identity. See this old blog from [Hanselman](http://www.hanselman.com/blog/SystemThreadingThreadCurrentPrincipalVsSystemWebHttpContextCurrentUserOrWhyFormsAuthenticationCanBeSubtle.aspx). Also see this newer [stackoverflow](http://stackoverflow.com/a/16693746) – subsci Nov 14 '13 at 05:10
-3

if you use the HttpContext.User.Identity equals the Thread.CurrentPrincipal

yapingchen
  • 766
  • 4
  • 4
  • 4
    Not true. If you spin off a thread in the background, HttpContext.User may change whilst the thread is running, depending on how you are running it. – blowdart Jul 25 '11 at 01:37
  • 1
    HttpContext.Current.User will be the current logged in web-user. Thread.CurrentPrincipal will be the principal for whomever is running the worker process (Thread). In the case of a forms/wpf app it makes sense because the user you're running the application under is the one you're interested in. – Daniil T. Sep 16 '16 at 22:07