Question

Assuming a fairly general use case in an ASP.NET Web Forms application, where should I be retrieving my principal + identity?

I'm aware that there are generally two ways to go about it:

  • HttpContext.Current.User
  • Thread.CurrentPrincipal

I'm speculating that using HttpContext will run the risk of a current context not existing (i.e. HttpContext.Current == null) on some edge cases, but I more often see code that use this instead of Thread.CurrentPrincipal (which I assume is safer, as it should always exist?).

Why is this? What implications am I getting myself into if I pick one or the other?

Was it helpful?

Solution

Rep whoring here I come :)

Mr. Hanselman has already blogged about this here: http://www.hanselman.com/blog/SystemThreadingThreadCurrentPrincipalVsSystemWebHttpContextCurrentUserOrWhyFormsAuthenticationCanBeSubtle.aspx

The basic idea is that 99% of the time.. they are the same.. although it's possible to change them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top