samsmithnz Posted October 28, 2003 Posted October 28, 2003 Does anybody happen to know how to query ActiveDirectory to find the currently logged on user? I'm having some difficulty... thanks Sam Quote Thanks Sam http://www.samsmith.co.nz
samsmithnz Posted October 28, 2003 Author Posted October 28, 2003 (edited) I'm managed to find some C# code, but I'd like some help converting it to VB.NET. And heres what I have so far in the VB.NET. It compiles but doesn't 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 Dim p As WindowsPrincipal = Thread.CurrentPrincipal txtOutput.Text = GetFullName(p.Identity.Name) This is the C# 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) 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: Imports System.DirectoryServices Imports System.Security.Principal Imports System.Collections.Specialized Imports System.Threading Thanks Sam Edited October 28, 2003 by samsmithnz Quote Thanks Sam http://www.samsmith.co.nz
*Gurus* Derek Stone Posted October 28, 2003 *Gurus* Posted October 28, 2003 Dim p As WindowsPrincipal = DirectCast(Thread.CurrentPrincipal, WindowsPrincipal) Quote Posting Guidelines
samsmithnz Posted October 28, 2003 Author Posted October 28, 2003 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? Quote Thanks Sam http://www.samsmith.co.nz
*Gurus* Derek Stone Posted October 28, 2003 *Gurus* Posted October 28, 2003 You need to ensure the current application domain is configured to use Windows security. Dim app As AppDomain = AppDomain.CurrentDomain app.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal) Quote Posting Guidelines
samsmithnz Posted October 29, 2003 Author Posted October 29, 2003 Thanks that seems to have done the trick! Quote Thanks Sam http://www.samsmith.co.nz
Leaders dynamic_sysop Posted October 29, 2003 Leaders Posted October 29, 2003 did you try this way also? .... Dim strUser As Security.Principal.WindowsIdentity = Security.Principal.WindowsIdentity.GetCurrent() MessageBox.Show(strUser.Name) Quote
samsmithnz Posted October 30, 2003 Author Posted October 30, 2003 did you try this way also? .... 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? Quote Thanks Sam http://www.samsmith.co.nz
Leaders dynamic_sysop Posted October 30, 2003 Leaders Posted October 30, 2003 that gives you the currently logged on user:-\ which i understood you were looking for, but i guess you've got it going a different way :) Quote
samsmithnz Posted October 30, 2003 Author Posted October 30, 2003 that gives you the currently logged on user:-\ which i understood you were looking for, but i guess you've got it going a different way :) Oh, right... this looks like a much quicker way though... but I'll 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... Quote Thanks Sam http://www.samsmith.co.nz
samsmithnz Posted October 30, 2003 Author Posted October 30, 2003 What does that do and why do you think I need it? This works, except that I'm trying to use it for an ASP.NET page.... so it retrieves the logon name for the server :) Doh! Quote Thanks Sam http://www.samsmith.co.nz
Leaders dynamic_sysop Posted October 30, 2003 Leaders Posted October 30, 2003 well i'm 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... 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 Quote
samsmithnz Posted October 30, 2003 Author Posted October 30, 2003 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 Quote Thanks Sam http://www.samsmith.co.nz
Administrators PlausiblyDamp Posted October 30, 2003 Administrators Posted October 30, 2003 BDEV\IWAM_CORPDEV1 - Domain = BDEV, IWAM_CORPDEV1 - usser account NTLM - Logged on via NT LanManager authentication. 2808 - The token for the logon session. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Leaders dynamic_sysop Posted October 30, 2003 Leaders Posted October 30, 2003 like this ... username >> IWAM_CORPDEV1 << authentication ( NT ) >> NTLM << token for network access >> 2808 << hope it helps a bit :) Edit: :eek: beaten to the post :p Quote
samsmithnz Posted October 30, 2003 Author Posted October 30, 2003 (edited) 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), I'm going to try and port it to ASP.NET... Edited October 30, 2003 by samsmithnz Quote Thanks Sam http://www.samsmith.co.nz
el_chingon Posted October 30, 2003 Posted October 30, 2003 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 user's machine with 98, it does not obtain the logged in user's name. Does anyone know of another way to get the logged in user's name in 98? Quote
samsmithnz Posted November 3, 2003 Author Posted November 3, 2003 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... Quote Thanks Sam http://www.samsmith.co.nz
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.