samsmithnz
Well-known member
Does anybody happen to know how to query ActiveDirectory to find the currently logged on user?
Im having some difficulty...
thanks
Sam
Im having some difficulty...
thanks
Sam
Dim p As WindowsPrincipal = Thread.CurrentPrincipal
txtOutput.Text = GetFullName(p.Identity.Name)
WindowsPrincipal p = Thread.CurrentPrincipal as WindowsPrincipal;
txtOutput.Text = GetFullName(p.Identity.Name);
Private Sub btnCurrentUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCurrentUser.Click
Dim p As WindowsPrincipal = Thread.CurrentPrincipal
txtOutput.Text = GetFullName(p.Identity.Name)
WindowsPrincipal p = Thread.CurrentPrincipal as WindowsPrincipal;
Response.Write(GetFullName(p.Identity.Name));
End Sub
Private Function GetFullName(ByVal strLogin As String) As String
Dim str As String = ""
Parse the string to check if domain name is present.
Dim idx As Integer = strLogin.IndexOf("\\")
If idx = -1 Then
idx = strLogin.IndexOf("@")
End If
Dim strDomain As String
Dim strName As String
If idx <> -1 Then
strDomain = strLogin.Substring(0, idx)
strName = strLogin.Substring(idx + 1)
Else
strDomain = Environment.MachineName
strName = strLogin
End If
TODO: Not sure about this conversion
DirectoryEntry obDirEntry = null;
Dim obDirEntry As DirectoryEntry
Try
obDirEntry = New DirectoryEntry("WinNT://" + strDomain + "/" + strName)
Dim coll As System.DirectoryServices.PropertyCollection = obDirEntry.Properties
Dim obVal As Object = coll("FullName").Value
str = obVal.ToString()
Catch ex As Exception
str = ""
Trace.Write(ex.Message)
End Try
GetFullName = str
End Function
Imports System.DirectoryServices
Imports System.Security.Principal
Imports System.Collections.Specialized
Imports System.Threading
Dim p As WindowsPrincipal = DirectCast(Thread.CurrentPrincipal, WindowsPrincipal)
Dim app As AppDomain = AppDomain.CurrentDomain
app.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
Dim strUser As Security.Principal.WindowsIdentity = Security.Principal.WindowsIdentity.GetCurrent()
MessageBox.Show(strUser.Name)
Originally posted by dynamic_sysop
did you try this way also? ....
Code:Dim strUser As Security.Principal.WindowsIdentity = Security.Principal.WindowsIdentity.GetCurrent() MessageBox.Show(strUser.Name)
Originally posted by dynamic_sysop
that gives you the currently logged on user:-\ which i understood you were looking for, but i guess youve got it going a different way
Originally posted by samsmithnz
What does that do and why do you think I need it?
Dim user As Security.Principal.WindowsIdentity = Security.Principal.WindowsIdentity.GetCurrent()
Dim strInfo As String
strInfo += user.Name & Environment.NewLine
strInfo += user.AuthenticationType & Environment.NewLine
strInfo += user.Token.ToInt32 & Environment.NewLine
If user.IsAuthenticated Then
MessageBox.Show(strInfo & " is logged on with an authenticated account")
ElseIf user.IsGuest Then
MessageBox.Show(strInfo & " is logged on as a guest")
ElseIf user.IsAnonymous Then
MessageBox.Show(strInfo & " is anonymous")
End If