public ActionResult MoveInAD(string computerName, string ADPath) { const int ldapErrorInvalidCredentials = 0x31; ViewBag.ComputerName = computerName; ViewBag.CurrentOUDN = ADPath; string server = Domain.GetDomain() + ":636"; string domain = Domain.GetDomain(); SecureString secure = AdminAccounts.GetAdminAccount3().SecurePassword; if (String.IsNullOrWhiteSpace(ADPath)) { ADPath = ADPaths.GetDefaultPath(); } try { using (var ldapConnection = new LdapConnection(server)) { var networkCredential = new NetworkCredential(AdminAccounts.GetAdminAccount3().UserName, secure, Domain.GetDomain()); ldapConnection.SessionOptions.SecureSocketLayer = true; ldapConnection.AuthType = AuthType.Negotiate; ldapConnection.Bind(networkCredential); string DistinguishedName = GetObjectDistinguishedName(objectClass.computer, returnType.distinguishedName, computerName, Domain.GetDomain()); List <string> DNList = DistinguishedName.Split(',').ToList(); string finalOU = DNList.Skip(1).Take(1).First(); string finalOUName = finalOU.Substring(3, finalOU.Length - 3); ViewBag.CurrentOU = finalOUName; List <string> NewDNList = ADPath.Split(',').ToList(); string parentOU = NewDNList.Skip(1).Take(1).First(); string parentOUName = parentOU.Substring(3, parentOU.Length - 3); string parentOUDN = string.Join(",", NewDNList.Skip(1).ToArray()); ViewBag.ParentOUDN = parentOUDN; List <string> Children = EnumerateOU(ADPath); ViewBag.Children = Children; } } catch (LdapException ldapException) { if (ldapException.ErrorCode.Equals(ldapErrorInvalidCredentials)) { } } return(View()); }
// GET api/computerfromou/5 public List <string> Get(string id) { List <string> ComputerNames = new List <string>(); string ADPath = id; const int ldapErrorInvalidCredentials = 0x31; string server = Domain.GetDomain() + ":636"; SecureString secure = AdminAccounts.GetAdminAccount3().SecurePassword; if (String.IsNullOrWhiteSpace(ADPath)) { ADPath = ADPaths.GetDefaultPath(); } try { using (var ldapConnection = new LdapConnection(server)) { var networkCredential = new NetworkCredential(AdminAccounts.GetAdminAccount3().UserName, secure, Domain.GetDomain()); ldapConnection.SessionOptions.SecureSocketLayer = true; ldapConnection.AuthType = AuthType.Negotiate; ldapConnection.Bind(networkCredential); foreach (var child in EnumerateOU(ADPath)) { ComputerNames.Add(child.Split(',').ToList().Take(1).First().Substring(3, child.Split(',').ToList().Take(1).First().Length - 3)); } } } catch (LdapException ldapException) { if (ldapException.ErrorCode.Equals(ldapErrorInvalidCredentials)) { } } return(ComputerNames); }
// GET api/ou/5 public OUInfo Get(string id) { List <OU> ListOfContainers = new List <OU>(); string ADPath = id; const int ldapErrorInvalidCredentials = 0x31; string server = Domain.GetDomain() + ":636"; string domain = Domain.GetDomain(); SecureString secure = AdminAccounts.GetAdminAccount3().SecurePassword; if (String.IsNullOrWhiteSpace(ADPath)) { ADPath = ADPaths.GetDefaultPath(); } try { using (var ldapConnection = new LdapConnection(server)) { var networkCredential = new NetworkCredential(AdminAccounts.GetAdminAccount3().UserName, secure, Domain.GetDomain()); ldapConnection.SessionOptions.SecureSocketLayer = true; ldapConnection.AuthType = AuthType.Negotiate; ldapConnection.Bind(networkCredential); List <string> NewDNList = ADPath.Split(',').ToList(); string parentOU = NewDNList.Skip(1).Take(1).First(); string parentOUName = parentOU.Substring(3, parentOU.Length - 3); string parentOUDN = string.Join(",", NewDNList.Skip(1).ToArray()); OU ParentOU = new OU() { ShortName = "..", FullName = parentOUDN }; ListOfContainers.Add(ParentOU); foreach (var child in EnumerateOU(ADPath)) { OU ChildOU = new OU() { ShortName = child.Split(',').ToList().Take(1).First(), FullName = child }; ListOfContainers.Add(ChildOU); } } } catch (LdapException ldapException) { if (ldapException.ErrorCode.Equals(ldapErrorInvalidCredentials)) { } } return(new OUInfo { CurrentContainer = ADPath, ListOfContainers = ListOfContainers }); }