Imports System.Management
Private Function Authenticated() As Boolean
Dim queryCollection As ManagementObjectCollection
Dim co As New ConnectionOptions()
co.Username = UserName 'user name from user
co.Password = Password 'string password entered by user
Dim path As New System.Management.ManagementPath()
path.Server = ConnectionServer 'server to use for authentication
path.Path = "\\" & ConnectionServer & "\root\cimv2"
Dim ms As New System.Management.ManagementScope(path, co)
Dim oq As New System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem") 'dummy query to test rights
Dim query As New ManagementObjectSearcher(ms, oq)
Try
queryCollection = query.Get()
Catch ex1 As System.UnauthorizedAccessException
Return False 'ouch nt authentication failed
Catch ex As Exception
MsgBox("Error: NT Verification Failed", MsgBoxStyle.Critical, "Error Connecting to Server For Authentication")
Application.Exit()
End Try
Return True
End Function
Imports System
Imports System.Threading
Imports System.Security.Principal
Imports Microsoft.VisualBasic
Public Class Class1
Public Shared Sub Main()
'Get the current identity and put it into an identity object.
Dim MyIdentity As WindowsIdentity = WindowsIdentity.GetCurrent()
'Put the previous identity into a principal object.
Dim MyPrincipal As New WindowsPrincipal(MyIdentity)
'Principal values.
Dim PrincipalName As String = MyPrincipal.Identity.Name
Dim PrincipalType As String = MyPrincipal.Identity.AuthenticationType
Dim PrincipalAuth As String = MyPrincipal.Identity.IsAuthenticated.ToString()
'Identity values.
Dim IdentName As String = MyIdentity.Name
Dim IdentType As String = MyIdentity.AuthenticationType
Dim IdentIsAuth As String = MyIdentity.IsAuthenticated.ToString()
Dim ISAnon As String = MyIdentity.IsAnonymous.ToString()
Dim IsG As String = MyIdentity.IsGuest.ToString()
Dim IsSys As String = MyIdentity.IsSystem.ToString()
Dim Token As String = MyIdentity.Token.ToString()
'Print the values.
Console.WriteLine("Principal Values for current thread:")
Console.WriteLine(ControlChars.CrLf + ControlChars.CrLf + "Principal Name: {0}", PrincipalName)
Console.WriteLine("Principal Type: {0}", PrincipalType)
Console.WriteLine("Principal IsAuthenticated: {0}", PrincipalAuth)
Console.WriteLine(ControlChars.CrLf + ControlChars.CrLf + "Identity Values for current thread:")
Console.WriteLine("Identity Name: {0}", IdentName)
Console.WriteLine("Identity Type: {0}", IdentType)
Console.WriteLine("Identity IsAuthenticated: {0}", IdentIsAuth)
Console.WriteLine(ControlChars.CrLf + ControlChars.CrLf + "Identity IsAnonymous: {0}", ISAnon)
Console.WriteLine("Identity IsGuest: {0}", IsG)
Console.WriteLine("Identity IsSystem: {0}", IsSys)
Console.WriteLine("Identity Token: {0}", Token)
End Sub
End Class
Here’s what they say the output will look like…
Principal Values for current thread:
Principal Name: MYDOMAIN\myuseraccount
Principal Type: NTLM
Principal IsAuthenticated: True
Identity Values for current thread:
Identity Name: MYDOMAIN\myuseraccount
Identity Type: NTLM
Identity IsAuthenticated: True
Identity IsAnonymous: False
Identity IsGuest: False
Identity IsSystem: False
Identity Token: 276