ActiveDirectory

samsmithnz

Well-known member
Joined
Jul 22, 2003
Messages
1,038
Location
Boston
Does anybody happen to know how to query ActiveDirectory to find the currently logged on user?

Im having some difficulty...

thanks

Sam
 
Im managed to find some C# code, but Id like some help converting it to VB.NET.

And heres what I have so far in the VB.NET. It compiles but doesnt run! Why not? It gives me the error message "Specified cast is not valid." on the first line (dim p as Windows...etc)

This is the VB.NET converted code
Code:
Dim p As WindowsPrincipal = Thread.CurrentPrincipal
txtOutput.Text = GetFullName(p.Identity.Name)

This is the C# code
Code:
WindowsPrincipal p = Thread.CurrentPrincipal as WindowsPrincipal;
txtOutput.Text = GetFullName(p.Identity.Name);

And heres the entire VB.NET project (except for the windows generated stuff)
Code:
    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

Note that I am also importing these things at the top:
Code:
Imports System.DirectoryServices
Imports System.Security.Principal
Imports System.Collections.Specialized
Imports System.Threading

Thanks

Sam
 
Last edited by a moderator:
I was trying things long that line too, like CType(), but neither Ctype or DirectCast work. They both return the error message "Specified cast is not valid", as does not using any of those functions at all. I get why its happening, they are different types, but why does it work in c# but not vb.net?
 
You need to ensure the current application domain is configured to use Windows security.
Code:
Dim app As AppDomain = AppDomain.CurrentDomain
app.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
 
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)

What does that do and why do you think I need it?
 
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 :)

Oh, right... this looks like a much quicker way though... but Ill still have to query ActiveDirectory, as I have to find the current user, their logon name, Full name and whether or not they belong to a certain admin group...
 
Originally posted by samsmithnz
What does that do and why do you think I need it?

This works, except that Im trying to use it for an ASP.NET page.... so it retrieves the logon name for the server :)

Doh!
 
well im not sure how asp.net would do it, but if you want a bit more info on Security.Principle.WindowsIdentity , i knocked this up to show an example...
Code:
        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
 
Hey thanks!

I ran it up and it produces something like this:

What does it all mean?

BDEV\IWAM_CORPDEV1
NTLM
2808 is logged on with an authenticated account
 
BDEV\IWAM_CORPDEV1 - Domain = BDEV, IWAM_CORPDEV1 - usser account
NTLM - Logged on via NT LanManager authentication.

2808 - The token for the logon session.
 
Damn, I really need to get this working somehow. I found an ASP example that uses ADODB (not exactly the managed code example I need), Im going to try and port it to ASP.NET...
 
Last edited by a moderator:
Is there any reason that the Security.Principal.WindowsIdentity.GetCurrent() would not work with windows 98? It appears to work just fine when I debug and run in XP, but running my app on a users machine with 98, it does not obtain the logged in users name. Does anyone know of another way to get the logged in users name in 98?
 
Still stuck.

I found a good example for logging on and finding all the groups the user belongs too, but all I need now is an object that will return the current user (not the server user), that I can use in this ASP.NET to show items that belong to them.

Someone must have donw this before...
 
Back
Top