Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between HttpContext.Current.User and HttpContext.User

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.

like image 840
user3576106 Avatar asked Sep 29 '22 09:09

user3576106


1 Answers

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.

like image 70
John Saunders Avatar answered Oct 19 '22 08:10

John Saunders