You may encounter situations in which it is crucial to get information about a current Windows user. VB.NET simplifies the process by allowing you to utilize the WindowsIdentity class for this task. In this tip, I demonstrate how you can access information about the current Windows user utilizing the WindowsIdentity class and one of its properties.
The WindowsIdentity class, which is available in the System.Security.Principal namespace, represents some basic information about the Windows account. The WindowsIdentity.GetCurrent method allows you to get the WindowsIdentity object under which the code is executing. In Listing A, I use the WindowsIdentity.GetCurrent method to retrieve the information about the current user.
In the example, I define UserIdetityInfo as a WindowsIdentity class. I also define the string variable strMsg to hold the value of the message that I will show at the end. I set the variable UserIdentityInfo to the value of the current user by utilizing the class's GetCurrent method. And finally, I use different properties of the UserIdentityInfo, concatenate them into a string value (which is stored in strMsg), and display the message.
Miss a tip?
Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!