0

I'm working on an application which as an interface requiring the user's name to be provided as components (first, middle, last).

When setting up a user in AD the user dialog has text boxes for (first, middle, last) and then combines these into a display name.

I can retrieve these parts using System.DirectoryServices.AccountManagement by doing something like the following:

UserPrincipal userPrinciple = UserPrincipal.Current;
Name.GivenName = userPrinciple.GivenName;
Name.MiddleName = userPrinciple.MiddleName;
Name.FamilyName = userPrinciple.Surname;

Now unfortunately, UserPrincipal throws an exception if the system is disconnected from the domain. In that situation I fall back to GetUserNameEx.

[DllImport("secur32.dll", CharSet = CharSet.Auto)]
public static extern bool GetUserNameEx(int nameFormat, StringBuilder userName,  ref uint userNameSize);

StringBuilder fullname = new StringBuilder(1024);
uint size = (uint)fullname.Capacity;
GetUserNameEx(3, fullname, ref size)     

Here I am left to fend for myself and break the full name into its components. Does anyone know a way to get the components when the system is disconnected from the domain?

Similarly, if the system is not part of a domain and local accounts are being used I resort to WMI.

string UserName = Environment.UserName;
string query = "SELECT * FROM Win32_UserAccount Where Name=\"" + UserName + "\"";
ManagementScope mgmtScope = new ManagementScope("\\\\.\\Root\\CIMv2");
ObjectQuery oQuery = new ObjectQuery(query);
ManagementObjectSearcher mgmtSearch = new ManagementObjectSearcher(mgmtScope, oQuery);
ManagementObjectCollection objCollection = mgmtSearch.Get();
foreach (ManagementObject mgmtObject in objCollection)
{
    fullName = (string)mgmtObject["FullName"];
}

I am again left to break the name up on my own. Does anyone know a way to get the components when the system is in a workgroup using local accounts?

When I looked at the local user management dialog it appears to have some differences from the AD user dialog. It appears to lack the text boxes for providing the (first, middle, last) names and only has a full name text box.

denver
  • 2,863
  • 2
  • 31
  • 45

1 Answers1

1

From Win32_ComputerSystem you can figure out whether machine is a workstation or member server. Once you have this information and machine is in Workstation, you can always make WMI call to get account details otherwise call AD/LDAP objects for information. This is just a snippet of code, you have to provide structure to it

query = new ObjectQuery(@"Select * from Win32_ComputerSystem");
                        searcher = new ManagementObjectSearcher(scope, query); searcher.Options.Timeout = new TimeSpan(0, 0, wbemConnectFlagUseMaxWait);
                        ManagementObjectCollection qWin32_ComputerSystem = searcher.Get();
                        foreach (ManagementObject item in qWin32_ComputerSystem)
                        {
                            windows_domain_role = item["DomainRole"].ToString();
                            if (windows_domain_role == "0") { windows_domain_role = "Standalone Workstation"; }
                            if (windows_domain_role == "1") { windows_domain_role = "Workstation"; }
                            if (windows_domain_role == "2") { windows_domain_role = "Standalone Server"; }
                            if (windows_domain_role == "3") { windows_domain_role = "Member Server"; }
                            if (windows_domain_role == "4") { windows_domain_role = "Backup Domain Controller"; }
                            if (windows_domain_role == "5") { windows_domain_role = "Primary Domain Controller"; }
                           }

While fetching information from Win32_UserAccount, fire query w.r.t to domain so that you will have more control on information.

Select * from Win32_UserAccount Where Domain = <machine name>
Amit Shakya
  • 1,396
  • 12
  • 27
  • Thanks for the tip on determining if a system is in a domain. Very useful, but doesn't address the question. +1 – denver Jun 14 '16 at 19:07
  • Then I did not understand the question properly. When you say disconnected from domain, what does it mean? it is never a part of domain or it was part of domain. If it is not part of domain, you are left with only one option i.e. WMI. I tried to give that as an answer. – Amit Shakya Jun 14 '16 at 19:21
  • What the question is asking is how to get the first and last names. The UserPrincipal provides those to you when you are on a domain and connected. The other methods only provide the display name. – denver Jun 14 '16 at 21:01
  • Disconnected from the domain means that it is part of the domain, but disconnected from the network. In other words it is unable to reach the AD server and is working with cached credentials. – denver Jun 14 '16 at 21:02