public DOMAIN_PASSWORD_INFORMATION GetDomainPasswordInformation(SecurityIdentifier domainSid) { if (domainSid == null) { throw new ArgumentNullException(nameof(domainSid)); } var sid = new byte[domainSid.BinaryLength]; domainSid.GetBinaryForm(sid, 0); Check(Samlib.SamOpenDomain(_handle, DOMAIN_ACCESS_MASK.DOMAIN_READ_PASSWORD_PARAMETERS, sid, out IntPtr domain)); var info = IntPtr.Zero; try { Check(Samlib.SamQueryInformationDomain(domain, DOMAIN_INFORMATION_CLASS.DomainPasswordInformation, out info)); return((DOMAIN_PASSWORD_INFORMATION)Marshal.PtrToStructure(info, typeof(DOMAIN_PASSWORD_INFORMATION))); } finally { Samlib.SamFreeMemory(info); Samlib.SamCloseHandle(domain); } }
public IEnumerable <string> EnumerateDomains() { int cookie = 0; while (true) { var status = Samlib.SamEnumerateDomainsInSamServer(_handle, ref cookie, out IntPtr info, 1, out int count); if (status != NTSTATUS.STATUS_SUCCESS && status != NTSTATUS.STATUS_MORE_ENTRIES) { Check(status); } if (count == 0) { break; } var us = (UNICODE_STRING)Marshal.PtrToStructure(info + IntPtr.Size, typeof(UNICODE_STRING)); Samlib.SamFreeMemory(info); yield return(us.ToString()); us.Buffer = IntPtr.Zero; // we don't own this one } }