windows 如何知道用户帐户是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2043253/
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
How to know whether an user account exists
提问by satya
How do I know if an user account exists on my Windows OS (Vista)? I need this information from a stand alone machine that hasn't joined any domain.
I want to know whether an user is a part of a group, for example is a user 'admin' part of 'Administrators' group or not?
我如何知道我的 Windows 操作系统 (Vista) 上是否存在用户帐户?我需要来自未加入任何域的独立机器的此信息。
我想知道用户是否属于某个组,例如用户 'admin' 是否属于 'Administrators' 组?
采纳答案by satya
I have tried the following code and is working fine for me..
我已经尝试了以下代码并且对我来说工作正常..
public bool IsUserMemberOfGroup(string userName, string groupName)
{
bool ret = false;
try
{
DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
DirectoryEntry userGroup = localMachine.Children.Find(groupName, "group");
object members = userGroup.Invoke("members", null);
foreach (object groupMember in (IEnumerable)members)
{
DirectoryEntry member = new DirectoryEntry(groupMember);
if (member.Name.Equals(userName, StringComparison.CurrentCultureIgnoreCase))
{
ret = true;
break;
}
}
}
catch (Exception ex)
{
ret = false;
}
return ret;
}
回答by tyranid
You can work out if a local account exists through the System.Security.Principal
namespace using the following code.
您可以System.Security.Principal
使用以下代码通过命名空间确定本地帐户是否存在。
bool AccountExists(string name)
{
bool bRet = false;
try
{
NTAccount acct = new NTAccount(name);
SecurityIdentifier id = (SecurityIdentifier)acct.Translate(typeof(SecurityIdentifier));
bRet = id.IsAccountSid();
}
catch (IdentityNotMappedException)
{
/* Invalid user account */
}
return bRet;
}
Now getting group membership is slightly harder, you can easily do it for the current user using the WindowsPrinciple.IsInRole
method (creating a principle from the WindowsIdentify.GetCurrent()
method).
现在获得组成员资格稍微困难一些,您可以使用该WindowsPrinciple.IsInRole
方法轻松地为当前用户完成(从该WindowsIdentify.GetCurrent()
方法创建一个原则)。
As pointed out I don't think there is a way of getting anything else without resorting to PInvoke or WMI. So here is a bit of code to check group membership with WMI.
正如所指出的,我不认为有一种方法可以在不诉诸 PInvoke 或 WMI 的情况下获得其他任何东西。所以这里有一些代码来检查 WMI 的组成员身份。
bool IsUserInGroup(string name, string group)
{
bool bRet = false;
ObjectQuery query = new ObjectQuery(String.Format("SELECT * FROM Win32_UserAccount WHERE Name='{0}' AND LocalAccount=True", name));
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection objs = searcher.Get();
foreach (ManagementObject o in objs)
{
ManagementObjectCollection coll = o.GetRelated("Win32_Group");
foreach (ManagementObject g in coll)
{
bool local = (bool)g["LocalAccount"];
string groupName = (string)g["Name"];
if (local && groupName.Equals(group, StringComparison.InvariantCultureIgnoreCase))
{
bRet = true;
break;
}
}
}
return bRet;
}