Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2006

Tip: Looking for answers? Try searching our database.

Detecting if a NTAccount is user or a group

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Roshan - 13 Jul 2006 16:13 GMT
Hi,

I wanted a reliable way of detecting if a given NTAccount object
represents a user account or group account. I was using
SecurityIdentifier.IsAccountSid() method but this doesn't work as I
expected. It returns true for user accounts and also for groups created
by administrator. Right now I am using the WMI class
System.Management.ManagementClass to get a list of all local user
accounts and am iterating over them to see if the given account is a
user account.

       System.Management.ManagementClass mc = new
System.Management.ManagementClass("Win32_UserAccount");

System.Management.ManagementObjectCollection objColl =
mc.GetInstances();
foreach (System.Management.ManagementObject obj in objColl)
{
  String name = obj.GetPropertyValue("Name") as String;
  // Compare name and NTAccount.Value to detect if its a user
}

While this works, it seems unclean and a overtly complex way of doing
things. Does any one know of a clean straight forward way of doing
this?

Thanks,
Roshan
Henning Krause - 13 Jul 2006 17:02 GMT
Hello,

you can use the LookupAccountSid or LookupAccountName function. These will
translate accountnames to sids and vice versa.

And you get the accountType:

       [DllImport("advapi32.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
       [return: MarshalAs(UnmanagedType.Bool)]
       public static extern bool LookupAccountSid(
           [In] string systemName,
           [In, MarshalAs(UnmanagedType.LPArray)] byte[] sid,
           [Out] StringBuilder name,
           [In, Out] ref uint nameLength,
           [Out] StringBuilder referencedDomainName,
           [In, Out] ref uint referencedDomainNameLength,
           [Out] out AccountType usage);

public enum AccountType
   {
       /// <summary>
       /// No account type
       /// </summary>
       None = 0,
       /// <summary>
       /// The account is a user
       /// </summary>
       User,
       /// <summary>
       /// The account is a security group
       /// </summary>
       Group,
       /// <summary>
       /// The account defines a domain
       /// </summary>
       Domain,
       /// <summary>
       /// The account is an alias
       /// </summary>
       Alias,
       /// <summary>
       /// The account is a well-known group, such as
BUILTIN\Administrators
       /// </summary>
       WellknownGroup,
       /// <summary>
       /// The account was deleted
       /// </summary>
       DeletedAccount,
       /// <summary>
       /// The account is invalid
       /// </summary>
       Invalid,
       /// <summary>
       /// The type of the account is unknown
       /// </summary>
       Unknown,
       /// <summary>
       /// The account is a computer account
       /// </summary>
       Computer
   }

Best regards,
Henning Krause

> Hi,
>
[quoted text clipped - 24 lines]
> Thanks,
> Roshan

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.