您是否在尋找特定用戶或所有用戶?
我有一個應用程序通過檢查帳戶名來檢查用戶是否在場 - 它在System.Security.Principal
命名空間中使用SecurityIdentifier
來檢查Sid是否有效。
public bool AccountExists(string name)
{
bool SidExists = false;
try
{
NTAccount Acct = new NTAccount(name);
SecurityIdentifier id = (SecurityIdentifier)Acct.Translate(typeof(SecurityIdentifier));
SidExists = id.IsAccountSid();
}
catch (IdentityNotMappedException)
{
//Oh snap.
}
return SidExists;
}
創建當你NTAccount
對象
NTAccount Acct = new NTAccount("SampleDomain", "SampleName");
編輯
在提及你的評論可以指定域,將這項工作的嗎? Didnt檢查,可能需要處理evaulating的IsAccountSid()
方法之前可能返回null ...
public SecurityIdentifier AccountSID(string myDomain, string myAcct)
{
SecurityIdentifier id;
try
{
NTAccount Acct = new NTAccount(myDomain, myAcct);
id = (SecurityIdentifier)Acct.Translate(typeof(SecurityIdentifier));
}
catch (IdentityNotMappedException)
{
//Oh snap.
}
return id;
}
SecurityIdentifier AcctSID = AccountSID("ExampleDomain", "ExampleName");
if (AcctSID.IsAccountSid())
//Do Something
+1,因爲這似乎是最完整的答案。 – LamonteCristo 2010-10-05 13:38:13
...(並感謝編輯使其更加完整) – LamonteCristo 2010-11-12 19:21:53