Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Remove access to admin from deleting the file in C#

try
{
       // Way safer than string comparison against "BUILTINAdministrators"
       IdentityReference BuiltinAdministrators = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
       IdentityReference AuthenticatedUsers = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null);

       FileSecurity FileACL = File.GetAccessControl("database.sdf"); // Grab ACL from file

       if (FileACL.GetOwner(typeof(SecurityIdentifier)) != BuiltinAdministrators) // Check if correct owner is set
       {
             FileACL.SetOwner(BuiltinAdministrators); // If not, make it so!
       }

       foreach (FileSystemAccessRule fsRule in FileACL.GetAccessRules(true, true, typeof(SecurityIdentifier)))
       {
             if ((fsRule.FileSystemRights & FileSystemRights.Delete) == FileSystemRights.Delete ||
                    (fsRule.FileSystemRights & FileSystemRights.ChangePermissions) == FileSystemRights.ChangePermissions) // Check if rule grants delete or change permissions
             {
                  FileACL.RemoveAccessRule(fsRule); // If so, nuke it!
             }
       }

       // Add explicit rules
       FileACL.AddAccessRule(new FileSystemAccessRule(BuiltinAdministrators, FileSystemRights.FullControl, AccessControlType.Allow));
       FileACL.AddAccessRule(new FileSystemAccessRule(AuthenticatedUsers, FileSystemRights.Delete, AccessControlType.Deny));
       FileACL.AddAccessRule(new FileSystemAccessRule(AuthenticatedUsers, FileSystemRights.ChangePermissions, AccessControlType.Deny));
       FileACL.AddAccessRule(new FileSystemAccessRule(AuthenticatedUsers, FileSystemRights.Read, AccessControlType.Allow));
       FileACL.AddAccessRule(new FileSystemAccessRule(AuthenticatedUsers, FileSystemRights.Write, AccessControlType.Allow));

       FileACL.SetAccessRuleProtection(true, false); // Enable protection from inheritance, remove existing inherited rules
       File.SetAccessControl("database.sdf", FileACL); // Write ACL back to file
   }
   catch { }
Comment

PREVIOUS NEXT
Code Example
Csharp :: C# Convert 1 range to another 
Csharp :: generate UUID id for my entities 
Csharp :: C# Linq item index 
Csharp :: unity scene switch 
Csharp :: IsInstanceOf nunit 
Csharp :: c# instance class with ilogger 
Csharp :: run dll file 
Csharp :: unique field in class model .net core 
Csharp :: indexof c# 
Csharp :: program.cs entity framework 
Csharp :: xamarin set environment variables 
Csharp :: Send Hotmail/Outlook Email C# (Win/ASP.NET) 
Csharp :: verifyusertokenasync password reset token 
Csharp :: c# out parameter 
Csharp :: c# selenium xunit testing 
Csharp :: wpf binding object get value 
Csharp :: windows forms webbrowser refresh 
Csharp :: c# enum get string value 
Csharp :: c# clear linkList 
Csharp :: get file upload file size in MB c# 
Csharp :: c# max sequence contains no elements 
Csharp :: c# copy bidimensional array 
Csharp :: Palindromic substrings 
Csharp :: unity3d gameobject follow path 
Csharp :: trygetvalue c# 
Csharp :: runtime save scene unity 
Csharp :: linq select 
Csharp :: How can I use Hex color Unity? , give hex color in unity 
Csharp :: linq syntax 
Csharp :: string length f# 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =