Deny permission to users except the C # compiler

2

I'm doing an application with a very basic little module of self-protection at the level of user security but I do not know how to take away the permission of the current user who does not write in the folder but if the application does, for example: to eliminate the autoprotección the program writes in the configuration file the status of checkbox of self-protection, if the checkbox is enabled the permissions will be activated, if it is disabled by the user from the interface of the program the permissions will be disabled. I got a code but at the time of writing in the program it removes the permissions until the application.

private void HAutoproteccion_OnChange(object sender, EventArgs e){
    var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    configFile.AppSettings.Settings["Check3"].Value = Convert.ToString(HAutoproteccion.Checked);
    configFile.Save(ConfigurationSaveMode.Modified);
    Autoproteccion();
}


public void autoproteccion(){
    if (HAutoproteccion.Checked == true){
        SelectQuery sQuery = new SelectQuery("Win32_UserAccount", "Domain='"
                            + System.Environment.UserDomainName.ToString() + "'");
        // Ve los usuarios habilitados
        ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(sQuery);
        string userName= System.Security.Principal.WindowsIdentity.GetCurrent().Name;
        foreach (ManagementObject mObject in mSearcher.Get()){
            userName = userName.Substring(userName.LastIndexOf("\") + 1);
            if (mObject["Name"].ToString() == userName){
                DirectoryInfo myDirectoryInfo = new DirectoryInfo(Directory.GetCurrentDirectory());
                DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
                string User = System.Environment.UserDomainName + "\" + userName;
                myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.Read, AccessControlType.Deny));
                myDirectoryInfo.SetAccessControl(myDirectorySecurity);
            }
        }
   } else {
       SelectQuery sQuery = new SelectQuery("Win32_UserAccount", "Domain='"
                            + System.Environment.UserDomainName.ToString() + "'");
       ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(sQuery);
       string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
       foreach (ManagementObject mObject in mSearcher.Get()){
           userName = userName.Substring(userName.LastIndexOf("\") + 1);
           if (mObject["Name"].ToString() == userName){
               DirectoryInfo myDirectoryInfo = new DirectoryInfo(Directory.GetCurrentDirectory());
               DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
               string User = System.Environment.UserDomainName + "\" + userName;
               myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.Write, AccessControlType.Allow));
               myDirectoryInfo.SetAccessControl(myDirectorySecurity);
           }
       }
   }
}

The point is that without the permissions write perfect but with the permissions not even the application lets write, some help?

    
asked by moises 05.07.2018 в 23:25
source

1 answer

0

If you want to access a network location within your domain, you can do:

NetworkCredential nc = new NetworkCredential(username, pass, domain);
CredentialCache cache = new CredentialCache();
cache.Add(@"\server", nc, "Basic", nc);
string[] carpetas = System.IO.Directory.GetDirectories("@\server\share");
    
answered by 07.07.2018 в 11:39