예제 #1
0
        public List <Computer> FindByDn(string dn)
        {
            CredentialRepository credential = new CredentialRepository(_mySQLContext);

            credential.DN = dn;
            return(FindAll(credential));
        }
예제 #2
0
 public List <Group> GetGroups(CredentialRepository credential, string userDn)
 {
     try
     {
         DirectoryEntry    dirEntry = new DirectoryEntry(credential.Path, credential.User, credential.Pass);
         DirectorySearcher search   = new DirectorySearcher(dirEntry);
         search.Filter = ("distinguishedName=" + userDn);
         SearchResult result = search.FindOne();
         if (result != null)
         {
             GroupRepository groupRepo = new GroupRepository(_mySQLContext);
             List <Group>    groupList = new List <Group>();
             Group           group     = new Group();
             for (int i = 0; i < result.Properties["memberOf"].Count; i++)
             {
                 group = groupRepo.FindByDnOne((String)result.Properties["memberOf"][i]);
                 groupList.Add(group);
             }
             return(groupList);
         }
         else
         {
             return(null);
         }
     }
     catch (DirectoryServicesCOMException e)
     {
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         return(null);
     }
 }
예제 #3
0
 public bool ResetPassBySamName(CredentialRepository credential, string samName, string newPass)
 {
     try
     {
         DirectoryEntry    dirEntry = new DirectoryEntry(credential.Path, credential.User, credential.Pass);
         DirectorySearcher search   = new DirectorySearcher(dirEntry);
         search.Filter = ("samaccountname=" + samName);
         SearchResult result = search.FindOne();
         if (result != null)
         {
             DirectoryEntry user = result.GetDirectoryEntry();
             user.Password = newPass;
             user.CommitChanges();
             dirEntry.Close();
             user.Close();
             return(true);
         }
         else
         {
             return(false);
         }
     }
     catch (Exception e)
     {
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         return(false);
     }
 }
예제 #4
0
        public List <OrganizationalUnit> FindByDn(string dn)
        {
            CredentialRepository credential = new CredentialRepository(_mySQLContext);

            credential.DN = dn;
            return(FindAll(credential));
        }
예제 #5
0
 public User Enable(CredentialRepository credential, string samName)
 {
     try
     {
         DirectoryEntry    dirEntry = new DirectoryEntry(credential.Path, credential.User, credential.Pass);
         DirectorySearcher search   = new DirectorySearcher(dirEntry);
         search.Filter = ("samaccountname=" + samName);
         SearchResult result = search.FindOne();
         if (result != null)
         {
             DirectoryEntry newUser = result.GetDirectoryEntry();
             SetEnable(newUser);
             newUser.CommitChanges();
             dirEntry.Close();
             newUser.Close();
             return(FindBySamName(credential, samName));
         }
         else
         {
             Console.WriteLine("\r\nUser not identify:\r\n\t");
             return(null);
         }
     }
     catch (Exception e)
     {
         if (e.Message.Equals("The object already exists."))
         {
             return(null);
         }
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         return(null);
     }
 }
예제 #6
0
 public bool Delete(OrganizationalUnit orgUnit)
 {
     try
     {
         CredentialRepository credential = new CredentialRepository(_mySQLContext);
         credential.DN = orgUnit.DistinguishedName;
         DirectoryEntry    dirEntry = new DirectoryEntry(credential.Path, credential.User, credential.Pass);
         DirectorySearcher search   = new DirectorySearcher(dirEntry);
         search.Filter = ("distinguishedname=" + orgUnit.DistinguishedName);
         DirectoryEntry orgUnitFind = (search.FindOne()).GetDirectoryEntry();
         if (orgUnitFind != null)
         {
             orgUnitFind.DeleteTree();
             return(true);
         }
         else
         {
             return(false);
         }
     }
     catch (System.DirectoryServices.DirectoryServicesCOMException e)
     {
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         return(false);
     }
 }
예제 #7
0
 public bool RemoveUser(string userDn, string groupDn)
 {
     try
     {
         CredentialRepository credential = new CredentialRepository(_mySQLContext);
         credential.DN = groupDn;
         DirectoryEntry dirEntry = new DirectoryEntry(credential.Path, credential.User, credential.Pass);
         if (dirEntry != null)
         {
             dirEntry.Properties["member"].Remove(userDn);
             dirEntry.CommitChanges();
             dirEntry.Close();
             return(true);
         }
         else
         {
             return(false);
         }
     }
     catch (DirectoryServicesCOMException e)
     {
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         return(false);
     }
 }
예제 #8
0
        public List <OrganizationalUnit> FindAll()
        {
            ConfigurationRepository config     = new ConfigurationRepository(_mySQLContext);
            CredentialRepository    credential = new CredentialRepository(_mySQLContext);

            credential.DN = config.GetConfiguration("DefaultDN");
            return(FindAll(credential));
        }
예제 #9
0
 public OrganizationalUnit Update(OrganizationalUnit orgUnit)
 {
     try
     {
         CredentialRepository credential = new CredentialRepository(_mySQLContext);
         credential.DN = orgUnit.DistinguishedName;
         DirectoryEntry    dirEntry = new DirectoryEntry(credential.Path, credential.User, credential.Pass);
         DirectorySearcher search   = new DirectorySearcher(dirEntry);
         search.Filter = ("ou=" + orgUnit.SamAccountName);
         SearchResult result = search.FindOne();
         if (result != null)
         {
             DirectoryEntry newOrganizationalUnit = result.GetDirectoryEntry();
             if (!String.IsNullOrWhiteSpace(orgUnit.Description))
             {
                 newOrganizationalUnit.Properties["description"].Value = orgUnit.Description;
             }
             if (!String.IsNullOrWhiteSpace(orgUnit.City))
             {
                 newOrganizationalUnit.Properties["l"].Value = orgUnit.City;
             }
             if (!String.IsNullOrWhiteSpace(orgUnit.State))
             {
                 newOrganizationalUnit.Properties["st"].Value = orgUnit.State;
             }
             if (!String.IsNullOrWhiteSpace(orgUnit.State))
             {
                 newOrganizationalUnit.Properties["street"].Value = orgUnit.Street;
             }
             if (!String.IsNullOrWhiteSpace(orgUnit.State))
             {
                 newOrganizationalUnit.Properties["postalcode"].Value = orgUnit.PostalCode;
             }
             if (!String.IsNullOrWhiteSpace(orgUnit.State))
             {
                 newOrganizationalUnit.Properties["c"].Value = orgUnit.Country;
             }
             if (!String.IsNullOrWhiteSpace(orgUnit.State))
             {
                 newOrganizationalUnit.Properties["managedBy"].Value = orgUnit.Manager;
             }
             newOrganizationalUnit.CommitChanges();
             dirEntry.Close();
             newOrganizationalUnit.Close();
             return(FindByName(credential, orgUnit.Name));
         }
         else
         {
             Console.WriteLine("\r\nUser not identify:\r\n\t");
             return(null);
         }
     }
     catch (System.DirectoryServices.DirectoryServicesCOMException e)
     {
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         return(null);
     }
 }
예제 #10
0
        public Group FindByDnOne(string dn)
        {
            CredentialRepository credential = new CredentialRepository(_mySQLContext);

            credential.DN = dn;
            List <Group> g = FindAll(credential);

            return((Group)g[0]);
        }
예제 #11
0
        public User FindByDnOne(string dn)
        {
            CredentialRepository credential = new CredentialRepository(_mySQLContext);

            credential.DN = dn;
            List <User> users = FindAll(credential);
            User        user  = (User)users[0];

            return(user);
        }
예제 #12
0
 public Computer Create(Computer computer)
 {
     try
     {
         CredentialRepository credential = new CredentialRepository(_mySQLContext);
         credential.DN = computer.DistinguishedName;
         DirectoryEntry    dirEntry = new DirectoryEntry(credential.Path, credential.User, credential.Pass);
         DirectorySearcher search   = new DirectorySearcher(dirEntry);
         search.Filter = ("samaccountname=" + computer.SamAccountName);
         SearchResult result = search.FindOne();
         if (result == null)
         {
             DirectoryEntry newComputer = dirEntry.Children.Add("CN=" + computer.SamAccountName, ObjectApplication.Category.computer.ToString());
             if (!String.IsNullOrWhiteSpace(computer.SamAccountName))
             {
                 newComputer.Properties["samAccountName"].Value = computer.SamAccountName.ToUpper() + "$";
             }
             if (!String.IsNullOrWhiteSpace(computer.Description))
             {
                 newComputer.Properties["description"].Value = computer.Description;
             }
             if (!String.IsNullOrWhiteSpace(computer.DnsHostName))
             {
                 newComputer.Properties["dnshostname"].Value = computer.DnsHostName;
             }
             if (!String.IsNullOrWhiteSpace(computer.Location))
             {
                 newComputer.Properties["location"].Value = computer.Location;
             }
             if (true)
             {
                 var computerType = unchecked ((int)(ComputerType.PASSWD_NOTREQD | ComputerType.WORKSTATION_TRUST_ACCOUNT));
                 newComputer.Properties["userAccountControl"].Value = (ComputerType.PASSWD_NOTREQD | ComputerType.WORKSTATION_TRUST_ACCOUNT);
             }
             newComputer.CommitChanges();
             dirEntry.Close();
             newComputer.Close();
             return(FindBySamName(credential, computer.Name));
         }
         else
         {
             Console.WriteLine("\r\nUser not identify:\r\n\t");
             return(null);
         }
     }
     catch (Exception e)
     {
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         if (e.Message.Equals("The object already exists.\r\n"))
         {
             Console.WriteLine("\r\nO Usuario ja existe no contexto:\r\n\t" + e.GetType() + ":" + e.Message);
         }
         return(null);
     }
 }
예제 #13
0
 public Computer FindBySamName(CredentialRepository credential, string samName)
 {
     try
     {
         return(FindOne(credential, "samAccountName", samName));
     }
     catch (System.DirectoryServices.DirectoryServicesCOMException e)
     {
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         return(null);
     }
 }
예제 #14
0
 public OrganizationalUnit FindByName(CredentialRepository credential, string name)
 {
     try
     {
         return(FindOne(credential, "name", name));
     }
     catch (System.DirectoryServices.DirectoryServicesCOMException e)
     {
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         return(null);
     }
 }
예제 #15
0
 public OrganizationalUnit FindByOu(string dn, string ou)
 {
     try
     {
         CredentialRepository credential = new CredentialRepository(_mySQLContext);
         credential.DN = dn;
         return(FindOne(credential, "ou", ou));
     }
     catch (System.DirectoryServices.DirectoryServicesCOMException e)
     {
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         return(null);
     }
 }
예제 #16
0
 public Computer FindBySamName(string dn, string samName)
 {
     try
     {
         CredentialRepository credential = new CredentialRepository(_mySQLContext);
         credential.DN = dn;
         return(FindOne(credential, "SamAccountName", samName));
     }
     catch (Exception e)
     {
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         return(null);
     }
 }
예제 #17
0
 public Group FindByEmail(string dn, string email)
 {
     try
     {
         CredentialRepository credential = new CredentialRepository(_mySQLContext);
         credential.DN = dn;
         return(FindOne(credential, "mail", email));
     }
     catch (Exception e)
     {
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         return(null);
     }
 }
예제 #18
0
 private OrganizationalUnit FindOne(CredentialRepository credential, string campo, string valor)
 {
     try
     {
         DirectoryEntry    dirEntry = new DirectoryEntry(credential.Path, credential.User, credential.Pass);
         DirectorySearcher search   = new DirectorySearcher(dirEntry);
         search.Filter = "(" + campo + "=" + valor + ")";
         var orgUnit = GetResult(search.FindOne());
         if ((orgUnit.DistinguishedName == null) && (orgUnit.SamAccountName == null))
         {
             return(null);
         }
         return(orgUnit);
         //CredentialRepository credential = new CredentialRepository(_mySQLContext);
         //ServerRepository sr = new ServerRepository(_mySQLContext);
         //OrganizationalUnit organizationalUnit = new OrganizationalUnit();
         //credential.DN = dn;
         //DirectoryEntry dirEntry = new DirectoryEntry(credential.Path, credential.User, credential.Pass);
         //DirectorySearcher search = new DirectorySearcher(dirEntry);
         //search.Filter = "(" + campo + "=" + valor + ")";
         //search.SearchScope = SearchScope.Subtree;
         //search.PropertiesToLoad.Add("name");
         //search.PropertiesToLoad.Add("displayName");
         //search.PropertiesToLoad.Add("description");
         //search.PropertiesToLoad.Add("samaccountname");
         //search.PropertiesToLoad.Add("managedby");
         //search.PropertiesToLoad.Add("adspath");
         //search.PropertiesToLoad.Add("l");
         //search.PropertiesToLoad.Add("st");
         //search.PropertiesToLoad.Add("postalcode");
         //search.PropertiesToLoad.Add("c");
         //search.PropertiesToLoad.Add("mail");
         //search.PropertiesToLoad.Add("objectsid");
         //search.PropertiesToLoad.Add("whenchanged");
         //search.PropertiesToLoad.Add("whencreated");
         //search.PropertiesToLoad.Add("distinguishedname");
         //search.PropertiesToLoad.Add("street");
         //search.PropertiesToLoad.Add("iscriticalsystemobject");
         //search.PropertiesToLoad.Add("cn");
         //organizationalUnit = GetResult(search.FindOne());
         //return organizationalUnit;
     }
     catch (System.DirectoryServices.DirectoryServicesCOMException e)
     {
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         return(null);
     }
 }
예제 #19
0
 public User ChangeGroup(User user, Group newGroup, Group oldGroup)
 {
     try
     {
         CredentialRepository credential = new CredentialRepository(_mySQLContext);
         credential.DN = user.DistinguishedName;
         AddUserToGroup(user, newGroup);
         RemoveUserToGroup(user, oldGroup);
         return(FindBySamName(credential, user.SamAccountName));
     }
     catch (Exception e)
     {
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         return(null);
     }
 }
예제 #20
0
 public User FindBySamName(CredentialRepository credential, string samName)
 {
     try
     {
         var user = FindOne(credential, "samAccountName", samName);
         if (user != null)
         {
             return(user);
         }
         return(null);
     }
     catch (Exception e)
     {
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         return(null);
     }
 }
예제 #21
0
 public Computer Update(Computer computer)
 {
     try
     {
         CredentialRepository credential = new CredentialRepository(_mySQLContext);
         credential.DN = computer.DistinguishedName;
         DirectoryEntry    dirEntry = new DirectoryEntry(credential.Path, credential.User, credential.Pass);
         DirectorySearcher search   = new DirectorySearcher(dirEntry);
         search.Filter = ("samaccountname=" + computer.SamAccountName);
         SearchResult result = search.FindOne();
         if (result != null)
         {
             DirectoryEntry newComputer = result.GetDirectoryEntry();
             if (!String.IsNullOrWhiteSpace(newComputer.Name))
             {
                 newComputer.Rename("cn=" + newComputer.Name);
             }
             if (!String.IsNullOrWhiteSpace(computer.Description))
             {
                 newComputer.Properties["description"].Value = computer.Description;
             }
             if (!String.IsNullOrWhiteSpace(computer.DnsHostName))
             {
                 newComputer.Properties["dnshostname"].Value = computer.DnsHostName;
             }
             if (!String.IsNullOrWhiteSpace(computer.Location))
             {
                 newComputer.Properties["location"].Value = computer.Location;
             }
             newComputer.CommitChanges();
             dirEntry.Close();
             newComputer.Close();
             return(FindBySamName(credential, computer.SamAccountName));
         }
         else
         {
             Console.WriteLine("\r\nUser not identify:\r\n\t");
             return(computer);
         }
     }
     catch (Exception e)
     {
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         return(computer);
     }
 }
예제 #22
0
 private void RemoveUserToGroup(User user, Group group)
 {
     try
     {
         CredentialRepository credential = new CredentialRepository(_mySQLContext);
         credential.DN = user.DistinguishedName;
         DirectoryEntry    dirEntry = new DirectoryEntry(credential.Path, credential.User, credential.Pass);
         DirectorySearcher search   = new DirectorySearcher(dirEntry);
         PrincipalContext  pc       = new PrincipalContext(ContextType.Domain, user.SamAccountName, credential.Path);
         GroupPrincipal    addGroup = GroupPrincipal.FindByIdentity(pc, group.SamAccountName);
         addGroup.Members.Remove(pc, IdentityType.UserPrincipalName, user.SamAccountName);
         addGroup.Save();
     }
     catch (System.DirectoryServices.DirectoryServicesCOMException e)
     {
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
     }
 }
예제 #23
0
 public List <User> GetUsers(string groupDn)
 {
     try
     {
         CredentialRepository credential = new CredentialRepository(_mySQLContext);
         credential.DN = groupDn;
         DirectoryEntry dirEntry = new DirectoryEntry(credential.Path, credential.User, credential.Pass);
         if (dirEntry != null)
         {
             return(null);
         }
         return(null);
     }
     catch (DirectoryServicesCOMException e)
     {
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         return(null);
     }
 }
예제 #24
0
 private Computer FindOne(CredentialRepository credential, string campo, string valor)
 {
     try
     {
         DirectoryEntry    dirEntry = new DirectoryEntry(credential.Path, credential.User, credential.Pass);
         DirectorySearcher search   = new DirectorySearcher(dirEntry);
         search.Filter = "(" + campo + "=" + valor + ")";
         var computer = GetResult(search.FindOne());
         if ((computer.DistinguishedName == null) && (computer.SamAccountName == null))
         {
             return(null);
         }
         return(computer);
     }
     catch (Exception e)
     {
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         return(null);
     }
 }
예제 #25
0
 private List <OrganizationalUnit> FindAll(CredentialRepository credential)
 {
     try
     {
         DirectoryEntry            dirEntry = new DirectoryEntry(credential.Path, credential.User, credential.Pass);
         DirectorySearcher         search   = new DirectorySearcher(dirEntry);
         List <OrganizationalUnit> orgList  = new List <OrganizationalUnit>();
         search.Filter = "(objectCategory=organizationalUnit)";
         var orgUnitsResult          = search.FindAll();
         List <SearchResult> results = new List <SearchResult>();
         foreach (SearchResult orgUnitResult in orgUnitsResult)
         {
             orgList.Add(GetResult(orgUnitResult));
         }
         return(orgList);
     }
     catch (Exception e)
     {
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         return(null);
     }
 }
예제 #26
0
 private List <Computer> FindAll(CredentialRepository credential)
 {
     try
     {
         DirectoryEntry    dirEntry     = new DirectoryEntry(credential.Path, credential.User, credential.Pass);
         DirectorySearcher search       = new DirectorySearcher(dirEntry);
         List <Computer>   computerList = new List <Computer>();
         search.Filter = "(&(objectClass=computer))";
         var computersResult         = search.FindAll();
         List <SearchResult> results = new List <SearchResult>();
         foreach (SearchResult computerResult in computersResult)
         {
             computerList.Add(GetResult(computerResult));
         }
         return(computerList);
     }
     catch (Exception e)
     {
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         return(null);
     }
 }
예제 #27
0
 public bool Delete(CredentialRepository credential, Computer computer)
 {
     try
     {
         DirectoryEntry    dirEntry = new DirectoryEntry(credential.Path, credential.User, credential.Pass);
         DirectorySearcher search   = new DirectorySearcher(dirEntry);
         search.Filter = ("SamAccountName=" + computer.SamAccountName);
         DirectoryEntry computerFind = (search.FindOne()).GetDirectoryEntry();
         if (computerFind != null)
         {
             computerFind.DeleteTree();
             return(true);
         }
         else
         {
             return(false);
         }
     }
     catch (Exception e)
     {
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         return(false);
     }
 }
예제 #28
0
 public Group Update(Group group)
 {
     try
     {
         CredentialRepository credential = new CredentialRepository(_mySQLContext);
         credential.DN = group.DistinguishedName;
         DirectoryEntry    dirEntry = new DirectoryEntry(credential.Path, credential.User, credential.Pass);
         DirectorySearcher search   = new DirectorySearcher(dirEntry);
         search.Filter = ("samaccountname=" + group.SamAccountName);
         SearchResult result = search.FindOne();
         if (result != null)
         {
             DirectoryEntry newGroup = result.GetDirectoryEntry();
             newGroup.Properties["info"].Value = "Grupo Editado pela API RESTful  - " + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss");
             if (!String.IsNullOrWhiteSpace(group.Name))
             {
                 newGroup.Rename("cn=" + group.Name);
             }
             if (!String.IsNullOrWhiteSpace(group.DistinguishedName) && (!String.IsNullOrWhiteSpace(group.DisplayName)))
             {
                 newGroup.Properties["displayName"].Value = group.DisplayName;
             }
             if (!String.IsNullOrWhiteSpace(group.EmailAddress))
             {
                 newGroup.Properties["mail"].Value = group.EmailAddress;
             }
             if (!String.IsNullOrWhiteSpace(group.Manager))
             {
                 newGroup.Properties["managedby"].Value = group.Manager;
             }
             if (!String.IsNullOrWhiteSpace(group.Description))
             {
                 newGroup.Properties["description"].Value = group.Description;
             }
             if ((group.System) && (group.Global))
             {//SYSTEM = 0x00000001 |//GLOBAL = 0x00000002,// 1 + 2
                 var groupType = unchecked ((int)(GroupType.SYSTEM | GroupType.GLOBAL));
                 newGroup.Properties["groupType"].Value = groupType;
             }
             else if ((group.System) && (group.DomainLocal))
             {//SYSTEM = 0x00000001 | DOMAIN_LOCAL = 0x00000004 // 1 + 4
                 var groupType = unchecked ((int)(GroupType.SYSTEM | GroupType.DOMAIN_LOCAL));
                 newGroup.Properties["groupType"].Value = groupType;
             }
             else if ((group.System) && (group.Universal))
             {//SYSTEM = 0x00000001 | UNIVERSAL = 0x00000008 // 1 + 8
                 var groupType = unchecked ((int)(GroupType.SYSTEM | GroupType.UNIVERSAL));
                 newGroup.Properties["groupType"].Value = groupType;
             }
             else if ((group.Security) && (group.Global))
             {//SECURITY = 2147483648 | GLOBAL = 0x00000002,// 2147483648 + 2
                 var groupType = unchecked ((int)(GroupType.SECURITY | GroupType.GLOBAL));
                 newGroup.Properties["groupType"].Value = groupType;
             }
             else if ((group.Security) && (group.DomainLocal))
             {                                                       //SECURITY = 2147483648 | DOMAIN_LOCAL = 0x00000004 // 2147483648 + 4
                 var groupType = unchecked ((int)(GroupType.SECURITY | GroupType.DOMAIN_LOCAL));
                 newGroup.Properties["groupType"].Value = groupType; //
             }
             else if ((group.Security) && (group.Universal))
             {                                                       //SECURITY = 2147483648 | UNIVERSAL = 0x00000008 // 2147483648 + 8
                 var groupType = unchecked ((int)(GroupType.UNIVERSAL | GroupType.SECURITY));
                 newGroup.Properties["groupType"].Value = groupType; //-2147483640
             }
             newGroup.CommitChanges();
             dirEntry.Close();
             newGroup.Close();
             return(FindBySamName(credential, group.SamAccountName));
         }
         else
         {
             Console.WriteLine("\r\nUser not identify:\r\n\t");
             return(null);
         }
     }
     catch (System.DirectoryServices.DirectoryServicesCOMException e)
     {
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         return(null);
     }
 }
예제 #29
0
 public OrganizationalUnit Create(OrganizationalUnit orgUnit)
 {
     try
     {
         CredentialRepository credential = new CredentialRepository(_mySQLContext);
         credential.DN = orgUnit.DistinguishedName;
         DirectoryEntry    dirEntry = new DirectoryEntry(credential.Path, credential.User, credential.Pass);
         DirectorySearcher search   = new DirectorySearcher(dirEntry);
         search.Filter = ("OU=" + orgUnit.Ou);
         SearchResult result = search.FindOne();
         if (result == null)
         {
             DirectoryEntry newOrganizationalUnit = dirEntry.Children.Add("OU=" + orgUnit.Ou, ObjectApplication.Category.organizationalUnit.ToString());
             if (!String.IsNullOrWhiteSpace(orgUnit.Description))
             {
                 newOrganizationalUnit.Properties["description"].Value = orgUnit.Description;
             }
             if (!String.IsNullOrWhiteSpace(orgUnit.City))
             {
                 newOrganizationalUnit.Properties["l"].Value = orgUnit.City;
             }
             if (!String.IsNullOrWhiteSpace(orgUnit.State))
             {
                 newOrganizationalUnit.Properties["st"].Value = orgUnit.State;
             }
             if (!String.IsNullOrWhiteSpace(orgUnit.Street))
             {
                 newOrganizationalUnit.Properties["street"].Value = orgUnit.Street;
             }
             if (!String.IsNullOrWhiteSpace(orgUnit.PostalCode))
             {
                 newOrganizationalUnit.Properties["postalcode"].Value = orgUnit.PostalCode;
             }
             if (!String.IsNullOrWhiteSpace(orgUnit.Country))
             {
                 newOrganizationalUnit.Properties["c"].Value = orgUnit.Country;
             }
             if (!String.IsNullOrWhiteSpace(orgUnit.Manager))
             {
                 newOrganizationalUnit.Properties["managedBy"].Value = orgUnit.Manager;
             }
             newOrganizationalUnit.CommitChanges();
             dirEntry.Close();
             newOrganizationalUnit.Close();
             return(FindByName(credential, orgUnit.Name));
         }
         else
         {
             Console.WriteLine("\r\nO Unidade Organizacional já existe no contexto:\r\n\t");
             return(null);
         }
     }
     catch (Exception e)
     {
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         if (e.Message.Equals("The object already exists.\r\n"))
         {
             Console.WriteLine("\r\nO Unidade Organizacional já existe no contexto:\r\n\t" + e.GetType() + ":" + e.Message);
         }
         return(null);
     }
 }
예제 #30
0
 public User Update(User user)
 {
     try
     {
         CredentialRepository credential = new CredentialRepository(_mySQLContext);
         credential.DN = user.DistinguishedName;
         DirectoryEntry    dirEntry = new DirectoryEntry(credential.Path, credential.User, credential.Pass);
         DirectorySearcher search   = new DirectorySearcher(dirEntry);
         search.Filter = ("samaccountname=" + user.SamAccountName);
         SearchResult result = search.FindOne();
         if (result != null)
         {
             DirectoryEntry newUser = result.GetDirectoryEntry();
             newUser.Properties["info"].Value = "Usuário Editado pela API RESTful  - " + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss");
             if (!String.IsNullOrWhiteSpace(user.FirstName))
             {
                 newUser.Properties["givenName"].Value = user.FirstName;
             }
             if (!String.IsNullOrWhiteSpace(user.Surname))
             {
                 newUser.Properties["sn"].Value = user.Surname;
             }
             if (!String.IsNullOrWhiteSpace(user.Inicials))
             {
                 newUser.Properties["initials"].Value = user.Inicials;
             }
             if (!String.IsNullOrWhiteSpace(user.DisplayName))
             {
                 newUser.Properties["displayName"].Value = (user.DisplayName + " - " + user.EmployeeID);
             }
             if (!String.IsNullOrWhiteSpace(user.Title))
             {
                 newUser.Properties["title"].Value = user.Title;
             }
             if (!String.IsNullOrWhiteSpace(user.EmailAddress))
             {
                 newUser.Properties["mail"].Value = user.EmailAddress;
                 newUser.Properties["userPrincipalName"].Value = user.EmailAddress;
             }
             if (!String.IsNullOrWhiteSpace(user.Country))
             {
                 newUser.Properties["c"].Value = user.Country;
             }
             if (!String.IsNullOrWhiteSpace(user.Description))
             {
                 newUser.Properties["description"].Value = user.Description;
             }
             if (!String.IsNullOrWhiteSpace(user.Company))
             {
                 newUser.Properties["company"].Value = user.Company;
             }
             if (!String.IsNullOrWhiteSpace(user.Departament))
             {
                 newUser.Properties["department"].Value = user.Departament;
                 newUser.Properties["o"].Value          = user.Departament;
             }
             if (!String.IsNullOrWhiteSpace(user.MobilePhone))
             {
                 newUser.Properties["mobile"].Value = user.MobilePhone;
             }
             if (!String.IsNullOrWhiteSpace(user.OfficePhone))
             {
                 newUser.Properties["telephoneNumber"].Value = user.OfficePhone;
             }
             if (!String.IsNullOrWhiteSpace(user.Manager))
             {
                 newUser.Properties["manager"].Value = user.Manager;
             }
             if (!String.IsNullOrWhiteSpace(user.EmployeeID))
             {
                 newUser.Properties["employeeID"].Value = user.EmployeeID;
             }
             if (!String.IsNullOrWhiteSpace(user.Office))
             {
                 newUser.Properties["physicalDeliveryOfficeName"].Value = user.Office;
             }
             if (!String.IsNullOrWhiteSpace(user.State))
             {
                 newUser.Properties["st"].Value = user.State;
             }
             if (!String.IsNullOrWhiteSpace(user.City))
             {
                 newUser.Properties["l"].Value = user.City;
             }
             if (!String.IsNullOrWhiteSpace(user.StreetAddress))
             {
                 newUser.Properties["streetAddress"].Value = user.StreetAddress;
             }
             if (!String.IsNullOrWhiteSpace(user.PostalCode))
             {
                 newUser.Properties["postalCode"].Value = user.PostalCode;
             }
             newUser.CommitChanges();
             if (user.Enabled)
             {// 0 ou 1
                 SetEnable(newUser);
             }
             if (user.PasswordNotRequired)
             {
             }
             if (user.PasswordNeverExpires)
             {
             }
             if (user.CanNotChangePassword)
             {
             }
             if (user.ChangePasswordAtLogon)
             {
             }
             newUser.CommitChanges();
             if (!String.IsNullOrWhiteSpace(user.Name))
             {
                 newUser.Rename("cn=" + user.Name);
                 newUser.CommitChanges();
             }
             dirEntry.Close();
             newUser.Close();
             return(FindBySamName(credential, user.SamAccountName));
         }
         else
         {
             Console.WriteLine("\r\nUser not identify:\r\n\t");
             return(null);
         }
     }
     catch (Exception e)
     {
         if (e.Message.Equals("The object already exists."))
         {
             return(null);
         }
         Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);
         return(null);
     }
 }