This documentation is archived and is not being maintained.

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.

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
Show: