1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
   |  
            try
            {
                string userName = textBox3.Text;
                DirectoryInfo dInfo = new DirectoryInfo(_path);
                DirectorySecurity dSecurity = dInfo.GetAccessControl();
 
                string ldapReq = @"(&(objectCategory=user)(samAccountName=" + userName + "))";
                string ldapRoot = "LDAP://domain.net/DC=DOMAIN,DC=NET";
 
                DirectoryEntry sroot = new DirectoryEntry(ldapRoot, "login", "password");
                DirectorySearcher searcher = new DirectorySearcher(sroot, ldapReq, new string[] { "member" }, SearchScope.Subtree);
 
                SearchResultCollection sr = searcher.FindAll();
 
                if (sr.Count > 0)
                {
                    NTAccount acct = new NTAccount(userName);
                    IdentityReference id = acct.Translate(typeof(SecurityIdentifier));
                    FileSystemAccessRule rule = new FileSystemAccessRule(id, FileSystemRights.ReadAndExecute | FileSystemRights.Synchronize, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow);
 
                    dSecurity.AddAccessRule(rule);
                    dInfo.SetAccessControl(dSecurity);
                    m_parent.UserViewFill();
                    this.Close();
                }
 
                else
                {
                    MessageBox.Show("Utilisateur non trouvé !", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            } | 
Partager