locked
Get Users name instead of computer name RRS feed

  • Question

  • User-1066344940 posted

    Hi All,

    I have not had any problems using DirectoryEntry class while developing on my machine locally. I recently moved my webpage to our dev server to finish developing this page. I was pulling the full name of the user into a textbox without any problem before using this code below:

    'Get the developers/users full name from Directory Services to populate a txtBox on page load
        
    Public Shared Function GetFullName() As String
    	Try
    		Dim de = New DirectoryEntry("WinNT://" + Environment.UserDomainName + "/" + Environment.UserName)
    			'return Environment.UserDomainName + "/" + Environment.UserName;
    		Return de.Properties("fullName").Value.ToString()
    	Catch
    		Return Nothing
    	End Try
    End Function

    But now, I am getting the computers name in this value:

    Environment.UserName)

    When I view the value of this in the Watch Window it is my computers name. Therefore, it is not returning my name anymore here:

    de.Properties("fullName").Value.ToString()

    Do I need to go about this another way now?

    Wednesday, August 6, 2014 2:50 PM

Answers

  • User103196646 posted

    Try using UserPrincipal.Current -

    Dim currentADUser  As System.DirectoryServices.AccountManagement.UserPrincipal

    currentADUser = System.DirectoryServices.AccountManagement.UserPrincipal.Current

    Dim firstName As string = currentADUser.GivenName
    Dim lastName As String  = currentADUser.Surname

    Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 8, 2014 10:59 AM

All replies

  • User103196646 posted

    Hello Mark_F! Thanks for your post!

    Try this:

    Add a reference to System.DirectoryServices.AccountManagement.
    
    using (var context = new PrincipalContext(ContextType.Domain))
    {
        var principal = UserPrincipal.FindByIdentity(context, User.Identity.Name);
        var firstName = principal.GivenName;
        var lastName = principal.Surname;
    }

    This example only gets the user's network ID, not the user's full name:

    var windowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent();
    if (windowsIdentity != null)
    {
      var sUser = windowsIdentity.Name;
      console.writeline("Process running as " + sUser);
    }

    Regards!

    Wednesday, August 6, 2014 5:13 PM
  • User-1066344940 posted

    HI CShaprGreg,

    I would love to write this function in C#, its much easier if you ask me, but I need this in VB. Can you assist? I am required to use VB, and I am just learning it. The code converter isnt converting it right.

    Public Shared Function GetName() As String
     Using context = New PrincipalContext(ContextType.Domain)
    	Dim principal = UserPrincipal.FindByIdentity(context, "Name")
    	Dim firstName As string = principal.GivenName
    	Dim lastName As String  = principal.Surname
        Dim fullName As String  = firstName+" "+lastName
    End Using
            Return fullName.ToString()

    This doesnt work. Its telling me fullName is inaccessablile.

    Thursday, August 7, 2014 10:09 AM
  • User103196646 posted

    Try using UserPrincipal.Current -

    Dim currentADUser  As System.DirectoryServices.AccountManagement.UserPrincipal

    currentADUser = System.DirectoryServices.AccountManagement.UserPrincipal.Current

    Dim firstName As string = currentADUser.GivenName
    Dim lastName As String  = currentADUser.Surname

    Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 8, 2014 10:59 AM