public static AccountInfo Create(string identityName)
{
Console.WriteLine("CONFIGURATION CONSOLE AccountInfo {0}", identityName);
// check for null.
if (String.IsNullOrEmpty(identityName))
{
return null;
}
StringBuilder builder = new StringBuilder();
// check for SID based query.
if (identityName.StartsWith("S-"))
{
builder.AppendFormat("SID='{0}'", identityName);
}
// search by account name.
else
{
string domain = null;
string name = identityName;
int index = identityName.IndexOf('\\');
if (index != -1)
{
domain = identityName.Substring(0, index);
name = identityName.Substring(index+1);
}
builder.AppendFormat("Name='{0}'", name);
if (!String.IsNullOrEmpty(domain))
{
// check for non-existent domain.
if (String.Compare(domain, System.Net.Dns.GetHostName(), true) != 0)
{
if (String.IsNullOrEmpty(LookupDomainSid(domain)))
{
return null;
}
}
builder.AppendFormat("AND Domain='{0}'", domain);
}
}
SelectQuery query = new SelectQuery("Win32_Account", builder.ToString());
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
try
{
foreach (ManagementObject target in searcher.Get())
{
// filter on SID type.
byte? sidType = target["SIDType"] as byte?;
if (sidType == null || sidType.Value == 3 || sidType.Value > 5)
{
continue;
}
// create account info object.
AccountInfo account = new AccountInfo();
account.Name = target["Name"] as string;
account.SidType = (AccountSidType)sidType.Value;
account.Sid = target["SID"] as string;
account.Domain = target["Domain"] as string;
account.Description = target["Description"] as string;
account.Status = target["Status"] as string;
string caption = target["Caption"] as string;
object InstallDate = target["InstallDate"];
bool? LocalAccount = target["LocalAccount"] as bool?;
return account;
}
}
finally
{
searcher.Dispose();
}
return null;
}