Tuesday, December 16, 2008

How to: Determine the User's Domain

You can use the My.User object to get information about the current user. This example shows how to use the My.User.Name property to get the user's domain name if the application uses Windows authentication.

Because the application uses Windows authentication by default, My.User returns the Windows information about the user who started the application.

Example
This example checks if the application uses Windows authentication before parsing the My.User.Name property to determine the domain name.
This example returns an empty string if the application uses custom authentication, because an implementation of custom authentication does not necessarily provide domain information.



Function GetUserDomain() As String

If TypeOf My.User.CurrentPrincipal Is _
Security.Principal.WindowsPrincipal Then
' My.User is using Windows authentication.
' The name format is DOMAIN\USERNAME.
Dim parts() As String = Split(My.User.Name, "\")
Dim domain As String = parts(0)
Return domain
Else
' My.User is using custom authentication.
Return ""
End If
End Function

Source : http://msdn.microsoft.com/en-us/library/ztz70aw9(VS.80).aspx

No comments: