我的問題識別,如果用戶是本地管理員組
我使用PInvoked Windows API函數來驗證,如果用戶是本地管理員組的一部分英寸我正在使用GetCurrentProcess
,OpenProcessToken
,GetTokenInformation
和LookupAccountSid
來驗證用戶是否是本地管理員。
GetTokenInformation
返回一個TOKEN_GROUPS
結構,其中有一個SID_AND_ATTRIBUTES
結構數組。我遍歷集合並比較由LookupAccountSid
返回的用戶名。
我的問題是,本地(或更一般地在我們的內部域),這是按預期工作。內置\ Administrators位於當前進程標記的組成員資格內,並且我的方法返回true。在另一個開發者的另一個域上,函數返回false。
LookupAccountSid
適用於TOKEN_GROUPS
結構的前2次迭代,返回None和Everyone,然後抱怨「A參數不正確」。
什麼會導致只有兩組正常工作?
TOKEN_GROUPS
結構表明有14個組。我假設它是無效的SID。
我已經從PInvoke website上的示例中取得了PInvoked的所有內容。唯一的區別是用LookupAccountSid
我已將Sid
參數從byte[]
更改爲IntPtr
,因爲SID_AND_ATTRIBUTES
也用IntPtr
定義。這是因爲LookupAccountSid
是用PSID定義的嗎?
執行LookupAccountSid的PInvoke
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool LookupAccountSid(
string lpSystemName,
IntPtr Sid,
StringBuilder lpName,
ref uint cchName,
StringBuilder ReferencedDomainName,
ref uint cchReferencedDomainName,
out SID_NAME_USE peUse);
當代碼翻倒
for (int i = 0; i < usize; i++)
{
accountCount = 0;
domainCount = 0;
//Get Sizes
LookupAccountSid(null, tokenGroups.Groups[i].SID, null, ref accountCount, null,
ref domainCount, out snu);
accountName2.EnsureCapacity((int) accountCount);
domainName.EnsureCapacity((int) domainCount);
if (!LookupAccountSid(null, tokenGroups.Groups[i].SID, accountName2, ref accountCount, domainName,
ref domainCount, out snu))
{
//Finds its way here after 2 iterations
//But only in a different developers domain
var error = Marshal.GetLastWin32Error();
_log.InfoFormat("Failed to look up SID's account name. {0}", new Win32Exception(error).Message);
continue;
}
如果需要更多的代碼,讓我知道。任何幫助將不勝感激。
更容易。謝謝! – 2010-06-17 19:58:41