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:
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);
}
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.
Member
362 Points
333 Posts
Get Users name instead of computer name
Aug 06, 2014 02:50 PM|Mark_F|LINK
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:
But now, I am getting the computers name in this value:
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:
Do I need to go about this another way now?
Member
670 Points
188 Posts
Re: Get Users name instead of computer name
Aug 06, 2014 05:13 PM|csharpgreg|LINK
Hello Mark_F! Thanks for your post!
Try this:
This example only gets the user's network ID, not the user's full name:
Regards!
Member
362 Points
333 Posts
Re: Get Users name instead of computer name
Aug 07, 2014 10:09 AM|Mark_F|LINK
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.
This doesnt work. Its telling me fullName is inaccessablile.
Member
670 Points
188 Posts
Re: Get Users name instead of computer name
Aug 08, 2014 10:59 AM|csharpgreg|LINK
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!