I've found that both My.User.Name and My.User.CurrentPrincipal.Identity.Name both return the Windows l开发者_StackOverflow社区ogin ID of the user logged in to the PC. However, is there a difference between the two? Will there ever be a case where they won't return the same value?
The information on the My.User
MSDN page has the best summary of these values and when they differ
- http://msdn.microsoft.com/en-us/library/a3da82hw(VS.80).aspx
In summary though. The biggest difference occurs between Windows and Web Applications.
For Windows applications, this property provides the same functionality as the CurrentPrincipal property. For Web applications, this property provides the same functionality as the User property of the object returned by the Current property.
If you are in the context of a System.Web.UI.Page
, then:
Me.User
returnsMe.Context.User
;Me.Context
returnsHttpContext.Current
So they are indeed the same thing.
Oops! Read that a bit too quickly. My
is a "magic shortcut" to some common contextual objects, and User
is one of them. It is still the same as the expanded form.
精彩评论