private static bool AccountHasRight(IntPtr policyHandle, byte[] accountSid, string rightName) { IntPtr zero = IntPtr.Zero; int rightsCount = 0; int ntStatus = System.ServiceProcess.NativeMethods.LsaEnumerateAccountRights(policyHandle, accountSid, out zero, out rightsCount); if (ntStatus == -1073741772) { return false; } if (ntStatus != 0) { throw new Win32Exception(SafeNativeMethods.LsaNtStatusToWinError(ntStatus)); } try { IntPtr ptr = zero; for (int i = 0; i < rightsCount; i++) { System.ServiceProcess.NativeMethods.LSA_UNICODE_STRING_withPointer structure = new System.ServiceProcess.NativeMethods.LSA_UNICODE_STRING_withPointer(); Marshal.PtrToStructure(ptr, structure); char[] destination = new char[structure.length / 2]; Marshal.Copy(structure.pwstr, destination, 0, destination.Length); string strA = new string(destination, 0, destination.Length); if (string.Compare(strA, rightName, StringComparison.Ordinal) == 0) { return true; } ptr = (IntPtr) (((long) ptr) + Marshal.SizeOf(typeof(System.ServiceProcess.NativeMethods.LSA_UNICODE_STRING))); } } finally { SafeNativeMethods.LsaFreeMemory(zero); } return false; }
private static bool AccountHasRight(IntPtr policyHandle, byte[] accountSid, string rightName) { IntPtr zero = IntPtr.Zero; int rightsCount = 0; int ntStatus = System.ServiceProcess.NativeMethods.LsaEnumerateAccountRights(policyHandle, accountSid, out zero, out rightsCount); if (ntStatus == -1073741772) { return(false); } if (ntStatus != 0) { throw new Win32Exception(SafeNativeMethods.LsaNtStatusToWinError(ntStatus)); } try { IntPtr ptr = zero; for (int i = 0; i < rightsCount; i++) { System.ServiceProcess.NativeMethods.LSA_UNICODE_STRING_withPointer structure = new System.ServiceProcess.NativeMethods.LSA_UNICODE_STRING_withPointer(); Marshal.PtrToStructure(ptr, structure); char[] destination = new char[structure.length / 2]; Marshal.Copy(structure.pwstr, destination, 0, destination.Length); string strA = new string(destination, 0, destination.Length); if (string.Compare(strA, rightName, StringComparison.Ordinal) == 0) { return(true); } ptr = (IntPtr)(((long)ptr) + Marshal.SizeOf(typeof(System.ServiceProcess.NativeMethods.LSA_UNICODE_STRING))); } } finally { SafeNativeMethods.LsaFreeMemory(zero); } return(false); }