I am currently working with some legacy code in which the HttpContext.Current.User
property is used within an MVC controller method to perform some basic authorization on the current user. From reading the documentation there is also an HttpContext.User
property and they both seem to indicate that they can get/set the current user. I'm curious if at some levels they are interchangeable and the same, or if there is a key difference between the 2 that would cause unintended issues in terms of authorizing or even recognizing the current user of the web application.
You cannot refer to HttpContext.User
directly, except perhaps inside of a controller. User
is a property of the HttpContext
class. You can do something like this:
HttpContext context = HttpContext.Current;
IPrincipal user = context.User;
that is, you can refer to the property through an instance of the HttpContext
class.
The base Controller
class has a property named HttpContext
. Inside of a controller, if you reference HttpContext.User
, you are referencing base.HttpContext.User
, which will generally (always?) be the same thing as HttpContext.Current.User
, simply because base.HttpContext
will generally (maybe always?) be the same thing as HttpContext.Current
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With