jjw2015
jjw2015

Reputation: 343

User.Identity vs Thread.CurrentPrincipal.Identity?

There are some pages trying to explain that but I don't find any recent ones, so allow me to ask it again.

In a asp.net MVC 5, what is the difference of User.Identity and Thread.CurrentPrincipal.Identity?

I tried to print its name and claims out and they look like the same to me, maybe I missed something.

Upvotes: 3

Views: 5711

Answers (1)

Rosdi Kasim
Rosdi Kasim

Reputation: 25986

I am sure you have read this: Thread.CurrentPrincipal vs Current.User, there is no point re-explaining the same thing.

The answer to your question is I guess it depends on what application you are developing. If it is web application (ASP.NET MVC for example), then you should favor User.Identity because User.Identity is designed for web application.

Normally there is no difference between them because ASP.NET make sure they are in sync.. (unless you changed them as noted by Scott)

There are some cases where you do not want (nor have access to) System.Web, so you will have to use Thread.CurrentPrincipal.Identity.

Also do note the Scott blog above.. if you are changing one of it, make sure they are in sync.

Upvotes: 4

Related Questions