在 Windows XP 中获取用户的组名 - 使用任何可能的方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/486942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:53:56  来源:igfitidea点击:

Getting a user's group name in Windows XP - using any way possible

windowswindows-xp

提问by DrHazzard

I've posted a similar question about how to do this in JNDI, but in THIS post I am wondering how to do it using ANY INTERFACE.

我已经发布了一个关于如何在 JNDI 中执行此操作的类似问题,但在这篇文章中,我想知道如何使用任何接口来执行此操作。

I'd like to be able to retrieve a user's group name. When I say "group" I mean the group on the computer. Like "administrator" or "user."

我希望能够检索用户的组名。当我说“组”时,我指的是计算机上的组。如“管理员”或“用户”。

Can I do it through the command line, application, dll, or interface of some sort?

我可以通过命令行、应用程序、dll 或某种界面来完成吗?

Does anyone know how this might be done? Has anyone done this? Is it an easy task?

有谁知道如何做到这一点?有没有人做过这个?这是一项容易的任务吗?

回答by DrHazzard

From the command line:

从命令行:

net user <username>

or if they are on a domain

或者如果他们在一个域上

net user <username> /domain

Towards the bottom are 2 sections, Local Group Memberships and Global Group Memberships.

底部是 2 个部分,本地组成员资格和全局组成员资格。

Note: a user may have alot of groups (in my case at work a total of 8!)

注意:一个用户可能有很多组(在我的例子中总共有 8 个!)

回答by Mitch Wheat

Here's an example in VB (should be straightforward to convert to another language):

这是 VB 中的一个示例(转换为另一种语言应该很简单):

Dim User as IADsUser
Dim Group as IADsGroup
Dim UserDomain as String
Dim UserName as String

UserDomain = "Target_User_Domain"
UserName = "Target_User_Name"
Set User = GetObject("WinNT://" & UserDomain & "/" & UserName & ",user")

For Each Group in User.Groups
   Debug.Print Group.NameNext

回答by mrtaikandi

If you want to find out groups that the current logged in user is member of you can use

如果您想找出当前登录用户所属的组,您可以使用

System.Security.Principal.WindowsIdentity.GetCurrent().Groups

UPDATE

更新

If you want to get all of available groups, I thinkyou have to use Windows APIs. But in order to check if current user is in a specific group, you can use the following code.

如果您想获得所有可用的组,我认为您必须使用 Windows API。但是为了检查当前用户是否在特定组中,您可以使用以下代码。

WindowsIdentity currentUser = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(currentUser);
bool isInGroup = principal.IsInRole("{Group Name}");

If you want to check against built-in windows groups you can use WindowsBuiltInRole enum in IsInRole method.

如果要检查内置 Windows 组,可以在 IsInRole 方法中使用 WindowsBuiltInRole 枚举。