I have a file that requires a password being written to it. A process reads a password from a file and writes the password to the file so the user that the process runs under requires access to the file. However I wish to block access to everyone else. Currently I've tried the following:
WindowsIdentity privilegedUser = WindowsIdentity.GetCurrent();
SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
FileSecurity acl = new FileSecurity();
acl.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.Read, AccessControlType.Deny));
acl.PurgeAccessRules(privilegedUser.User);
acl.AddAccessRule(new FileSystemAccessRule(privilegedUser.User, FileSystemRights.Modify,
AccessControlType.Allow));
File.SetAccessControl(_mirrorConfigFilePath, acl);
However when the process attempts to read the file I'm getting an UnauthorizedAccessException. I think its because the first rule to deny read access to everyone gets precedence. Anyone any ideas how I can resolve this? Or am I even right in my thinking here?!