473,765 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
SecurityIdentif ier.IsAccountSi d() 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.Manageme nt.ManagementCl ass 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.Manageme nt.ManagementCl ass mc = new
System.Manageme nt.ManagementCl ass("Win32_User Account");

System.Manageme nt.ManagementOb jectCollection objColl =
mc.GetInstances ();
foreach (System.Managem ent.ManagementO bject obj in objColl)
{
String name = obj.GetProperty Value("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 4347
Hello,

you can use the LookupAccountSi d or LookupAccountNa me function. These will
translate accountnames to sids and vice versa.

And you get the accountType:
[DllImport("adva pi32.dll", SetLastError = true, CharSet =
CharSet.Unicode )]
[return: MarshalAs(Unman agedType.Bool)]
public static extern bool LookupAccountSi d(
[In] string systemName,
[In, MarshalAs(Unman agedType.LPArra y)] byte[] sid,
[Out] StringBuilder name,
[In, Out] ref uint nameLength,
[Out] StringBuilder referencedDomai nName,
[In, Out] ref uint referencedDomai nNameLength,
[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\Adminis trators
/// </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.goo glegroups.com.. .
Hi,

I wanted a reliable way of detecting if a given NTAccount object
represents a user account or group account. I was using
SecurityIdentif ier.IsAccountSi d() 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.Manageme nt.ManagementCl ass 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.Manageme nt.ManagementCl ass mc = new
System.Manageme nt.ManagementCl ass("Win32_User Account");

System.Manageme nt.ManagementOb jectCollection objColl =
mc.GetInstances ();
foreach (System.Managem ent.ManagementO bject obj in objColl)
{
String name = obj.GetProperty Value("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
1857
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 used opera till now, but it seems very buggy piece of software !! I have one very annoyng problem, fighting already ~4 hours. I found that if I use something like this in table (test are
3
1258
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 switched to this mode? I can detect if the OS is XP but not if they have changed to this display mode. Thanks (once again). Martin
2
3040
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 they successfuly download. My problem revolves around detecting a successful download, the steps I take to handle the download are as follows:
3
3612
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 still able to detect the Shockwave Player and the version number when using Firefox/Netscape, but not with IE. I have my own detection script that I use, but I've also used the detection scripts supplied from Macromedia, but it still won't...
79
3786
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 relevant name owner? If I make a whisky and call it "Jack Daniels", I most probably will have some serious legal problems. "Mozilla" partially appeared because NCSA stopped them from using "Mosaic" in the UA string. Is it some different...
2
8983
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 -- System.Security.Principal.NTAccount ntAccount = new System.Security.Principal.NTAccount(@"<domain>\Administrators"); System.Security.Principal.SecurityIdentifier sid =
1
24184
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 has been made in any control? I'm thinking of a situation in which a number of records have been entered on a form and the user is cycling through them to check the data entered. I want to be able to detect this and update certain information...
4
1372
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 beginning to implement syslog() and openlog() in my scripts and there are some suddle differences in how PHP logs to event viewer in Windows and syslog in everything else. About the most reliable method I have found in detecting Windows is a simple check...
2
2877
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 using onclick but I wonder if this is the correct approach, what if a user changes the status of the checkbox using the keyboard, will onclick be triggered? I know I might sound a little picky here but I believe those people who created DHTML...
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10008
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9837
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8833
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.