I need to get the SID of a computer (not the SID of a computer account) in C #. The computer is listed as the host name, it is not necessarily the local computer, and it can be a domain computer or a workgroup computer. I use this helper class to call the LookupAccountName API function:
private static class Helper { internal enum SID_NAME_USE { SidTypeUser = 1, SidTypeGroup, SidTypeDomain, SidTypeAlias, SidTypeWellKnownGroup, SidTypeDeletedAccount, SidTypeInvalid, SidTypeUnknown, SidTypeComputer } [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern bool LookupAccountName( string systemName, string accountName, byte[] sid, ref int sidLen, System.Text.StringBuilder domainName, ref int domainNameLen, out SID_NAME_USE peUse); public static SecurityIdentifier LookupAccountName( string systemName, string accountName, out string strDomainName, out SID_NAME_USE accountType) { const int ERROR_INSUFFICIENT_BUFFER = 122; int lSidSize = 0; int lDomainNameSize = 0;
and using it as follows:
Helper.SID_NAME_USE accountType; string refDomain; SecurityIdentifier sid = Helper.LookupAccountName("falcon.mydomain.local", "falcon", out refDomain, out accountType);
My only problem is that this does not work if the computer is the primary domain controller (in this case I need to get the domain SID).
c # windows sid
TomΓ‘Ε‘ Holan
source share