473,404 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Detecting if a NTAccount is user or a group

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_UserAccou nt");

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

Jul 13 '06 #1
1 4317
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

"Roshan" <br*****@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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_UserAccou nt");

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
Jul 13 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: raptor | last post by:
hi, how to detect opera..it seems that even opera8 doesnt support xmlhttp fully (.i.e. sendRequestHeader). I ask this 'cause opera seems to mimic IE, at least in the preferences ?! I havent...
3
by: Deano | last post by:
Just noticed that if a user chooses classic mode in XP the display changes to a Windows 2000 look. This messes up some aspects of how my forms look. Is it possible to detect if the user has...
2
by: Sam-Kiwi | last post by:
I've spent the last 6 months developing a pay-per-download website using ASP.NET Users purchase documents and then download them. The intention is that users are only charged for documents...
3
by: regtrashcan | last post by:
I have a webpage that detects whether Shockwave Player is installed and the version number. The javascript/vbscript that I use has worked fine until the latest release of the Shockwave Player. I am...
79
by: VK | last post by:
I wandering about the common proctice of some UA's producers to spoof the UA string to pretend to be another browser (most often IE). Shouldn't it be considered as a trademark violation of the...
2
by: =?Utf-8?B?QWxleGV5IFlha292bGV2?= | last post by:
Hi! Can anyone please explain me why following code gives an SecurityException "System.Security.SecurityException: The name provided is not a properly formed account name."? -- code start -- ...
1
by: wwwords | last post by:
Is there a general method for detecting that a user has changed the record currently visible on a form, whether this is by hitting PgUp or PgDn or clicking on a navigation button, even if no change...
4
by: Schroeder, AJ | last post by:
Hello group, I have a few PHP scripts that poll switches and routers via SNMP. Depending on the user, the scripts can be run from either Windows XP or *ix (FreeBSD, Redhat, etc) I am...
2
by: waleed | last post by:
I'm not really good with javascript, basically what I'm trying to do is that I want to be able to handle when the status of a checkbox is changed (i.e. checked or unchecked), I see everybody is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.