Last post Aug 08, 2014 10:59 AM by csharpgreg
Aug 06, 2014 02:50 PM|Mark_F|LINK
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
Dim de = New DirectoryEntry("WinNT://" + Environment.UserDomainName + "/" + Environment.UserName)
'return Environment.UserDomainName + "/" + Environment.UserName;
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?
Aug 06, 2014 05:13 PM|csharpgreg|LINK
Hello Mark_F! Thanks for your post!
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);
Aug 07, 2014 10:09 AM|Mark_F|LINK
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
This doesnt work. Its telling me fullName is inaccessablile.
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