Goal
I have a WCF Service using Windows Authentication. I want to determine the Full name of the user that connected. The user can either be a domain or a local user. For local users, you can view the Full name in [Computer Management (local) > Local Users and Groups > Users > [user] > Full Name].
Almost successful with FindByIdentity
I can use UserPrincipal.FindByIdentity() to retrieve a UserPrincipal object, and then get it from UserPrincipal.DisplayName. The first parameter of FindByIdentity requires specifying ContextType, such as Domain or Machine.
- Calling FindByIdentity with ContextType.Domain and a local user, produces a "Specified Domain Does Not Exist or Could Not Be Contacted" exception
- Calling FindByIdentity with ContextType.Machine and a domain user, produces a UserPrincipal without a filled DisplayName property
So i would like to avoid guessing.
Question
- How can i get the Full name of the connected user?
- Or how can i distinguish whether the connected user is a domain user or a local user?
Code so far
string GetFullName()
{
using (var ctx = new PrincipalContext(ContextType.Domain))
{
string userName = OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name;
UserPrincipal u = UserPrincipal.FindByIdentity(ctx, userName);
return u.DisplayName;
}
}