public static string IsDomainJoined() { // returns Compuer Domain if the system is inside an AD (an nothing if it is not) try { NetJoinStatus status = NetJoinStatus.NetSetupUnknownStatus; IntPtr pDomain = IntPtr.Zero; int result = Netapi32.NetGetJoinInformation(null, out pDomain, out status); if (pDomain != IntPtr.Zero) { Netapi32.NetApiBufferFree(pDomain); } if (result == Win32.ErrorSuccess) { // If in domain, return domain name, if not, return empty return(status == NetJoinStatus.NetSetupDomainName ? Environment.UserDomainName : ""); } } catch (Exception ex) { Beaprint.GrayPrint(string.Format(" [X] Exception: {0}\n Trying to check if domain is joined using WMI", ex.Message)); return(IsDomainJoinedWmi()); } return(""); }
public void SetUp() { // test user _testUser = new Netapi32.USER_INFO_1(); _testUser.usri1_name = "WaffleTestUser"; _testUser.usri1_password = Guid.NewGuid().ToString(); _testUser.usri1_priv = 1; _testUser.usri1_home_dir = null; _testUser.comment = "Waffle test user."; _testUser.usri1_script_path = null; int rc = Netapi32.NetUserAdd(null, 1, ref _testUser, 0); Assert.AreEqual(0, rc, new Win32Exception(rc).Message); // computer _computerName = Environment.MachineName; // fqn _testUserFqn = string.Format("{0}\\{1}", _computerName, _testUser.usri1_name); // join status IntPtr pDomain = IntPtr.Zero; rc = Netapi32.NetGetJoinInformation(null, out pDomain, out _joinStatus); Assert.AreEqual(Netapi32.NERR_Success, rc, new Win32Exception(rc).Message); _memberOf = Marshal.PtrToStringAuto(pDomain); Netapi32.NetApiBufferFree(pDomain); }
public static T[] NetServerEnum <T>(ServerTypes serverTypes = ServerTypes.Workstation | ServerTypes.Server, string domain = null, int level = 0) where T : struct { if (level == 0) { level = int.Parse(System.Text.RegularExpressions.Regex.Replace(typeof(T).Name, @"[^\d]", "")); } IntPtr bufptr = IntPtr.Zero; try { int entriesRead, totalEntries; IntPtr resumeHandle = IntPtr.Zero; int ret = Netapi32.NetServerEnum(null, level, out bufptr, MAX_PREFERRED_LENGTH, out entriesRead, out totalEntries, serverTypes, domain, resumeHandle); if (ret == 0) { return(InteropUtil.ToArray <T>(bufptr, entriesRead)); } throw new System.ComponentModel.Win32Exception(ret); } finally { Netapi32.NetApiBufferFree(bufptr); } }
public static T NetServerGetInfo <T>(string serverName, int level = 0) where T : struct { if (level == 0) { level = int.Parse(System.Text.RegularExpressions.Regex.Replace(typeof(T).Name, @"[^\d]", "")); } IntPtr ptr = IntPtr.Zero; try { int ret = Netapi32.NetServerGetInfo(serverName, level, out ptr); if (ret != 0) { throw new System.ComponentModel.Win32Exception(ret); } return((T)Marshal.PtrToStructure(ptr, typeof(T))); } finally { if (ptr != IntPtr.Zero) { Netapi32.NetApiBufferFree(ptr); } } }
public void SetUp() { // computer _computerName = Environment.MachineName; // join status IntPtr pDomain = IntPtr.Zero; Assert.AreEqual(Netapi32.NERR_Success, Netapi32.NetGetJoinInformation(null, out pDomain, out _joinStatus)); _memberOf = Marshal.PtrToStringAuto(pDomain); Netapi32.NetApiBufferFree(pDomain); }
/// <summary> /// Constructor with a computer name. /// </summary> /// <param name="computerName">Computer name.</param> public WindowsComputerImpl(string computerName) { _computerName = computerName; IntPtr pDomain = IntPtr.Zero; try { int rc = Netapi32.NetGetJoinInformation(computerName, out pDomain, out _joinStatus); if (rc == Netapi32.NERR_Success && _joinStatus != Netapi32.NetJoinStatus.NetSetupUnjoined) { _memberOf = Marshal.PtrToStringAuto(pDomain); } } finally { if (pDomain != IntPtr.Zero) { Netapi32.NetApiBufferFree(pDomain); } } }
public static IEnumerable <USER_INFO_3> GetLocalUsers(string computerName) { uint MAX_PREFERRED_LENGTH = unchecked ((uint)-1); // Returns local users // FILTER_NORMAL_ACCOUNT == 2 var users = new List <USER_INFO_3>(); var retVal = Netapi32.NetUserEnum(computerName, 3, 2, out var bufPtr, MAX_PREFERRED_LENGTH, out var entriesRead, out var totalEntries, out var resume); if (retVal != 0) { var errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message; throw new Exception("Error code " + retVal + ": " + errorMessage); } if (entriesRead == 0) { return(users); } var names = new string[entriesRead]; var userInfo = new USER_INFO_3[entriesRead]; var iter = bufPtr; for (var i = 0; i < entriesRead; i++) { userInfo[i] = (USER_INFO_3)Marshal.PtrToStructure(iter, typeof(USER_INFO_3)); users.Add(userInfo[i]); //x64 safe iter = new IntPtr(iter.ToInt64() + Marshal.SizeOf(typeof(USER_INFO_3))); } Netapi32.NetApiBufferFree(bufPtr); return(users); }
public void TearDown() { int rc = Netapi32.NetUserDel(null, _testUser.usri1_name); Assert.AreEqual(0, rc, new Win32Exception(rc).Message); }
public static TenantInfo GetTenantInfo() { //original code from https://github.com/ThomasKur/WPNinjas.Dsregcmd/blob/2cff7b273ad4d3fc705744f76c4bd0701b2c36f0/WPNinjas.Dsregcmd/DsRegCmd.cs string tenantId = null; var retValue = Netapi32.NetGetAadJoinInformation(tenantId, out var ptrJoinInfo); if (retValue == 0) { var joinInfo = (DSREG_JOIN_INFO)Marshal.PtrToStructure(ptrJoinInfo, typeof(DSREG_JOIN_INFO)); var jType = (JoinType)joinInfo.joinType; var did = new Guid(joinInfo.DeviceId); var tid = new Guid(joinInfo.TenantId); var data = Convert.FromBase64String(joinInfo.UserSettingSyncUrl); var userSettingSyncUrl = Encoding.ASCII.GetString(data); var ptrUserInfo = joinInfo.pUserInfo; DSREG_USER_INFO?userInfo = null; var certificateResult = new List <X509Certificate2>(); Guid? uid = null; if (ptrUserInfo != IntPtr.Zero) { userInfo = (DSREG_USER_INFO)Marshal.PtrToStructure(ptrUserInfo, typeof(DSREG_USER_INFO)); uid = new Guid(userInfo?.UserKeyId); var store = new X509Store(StoreName.My, StoreLocation.LocalMachine); store.Open(OpenFlags.ReadOnly); foreach (var certificate in store.Certificates) { if (certificate.Subject.Equals($"CN={did}")) { certificateResult.Add(certificate); } } Marshal.Release(ptrUserInfo); } Marshal.Release(ptrJoinInfo); Netapi32.NetFreeAadJoinInformation(ptrJoinInfo); return(new TenantInfo( jType, did, joinInfo.IdpDomain, tid, joinInfo.JoinUserEmail, joinInfo.TenantDisplayName, joinInfo.MdmEnrollmentUrl, joinInfo.MdmTermsOfUseUrl, joinInfo.MdmComplianceUrl, userSettingSyncUrl, certificateResult, userInfo?.UserEmail, uid, userInfo?.UserKeyName )); } return(null); }