本文整理汇总了C#中System.Security.AccessControl.FileSecurity.AddAccessRule方法的典型用法代码示例。如果您正苦于以下问题:C# FileSecurity.AddAccessRule方法的具体用法?C# FileSecurity.AddAccessRule怎么用?C# FileSecurity.AddAccessRule使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.AccessControl.FileSecurity
的用法示例。
在下文中一共展示了FileSecurity.AddAccessRule方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExecuteOnFile
protected override void ExecuteOnFile(FileInfo file)
{
FileSecurity fileSec = new FileSecurity(file.FullName, AccessControlSections.Access);
Log(Level.Info, Resources.AddAccessRuleAdding, Rights, NTAccount, file.FullName);
FileSystemAccessRule newRule = new FileSystemAccessRule(new NTAccount(NTAccount), Rights, AccessControlType);
fileSec.AddAccessRule(newRule);
file.SetAccessControl(fileSec);
}
示例2: Main
static void Main(string[] args)
{
DirectorySecurity ds = new DirectorySecurity();
ds.AddAccessRule(new FileSystemAccessRule("Rafa&Pri", FileSystemRights.Read, AccessControlType.Allow));
string newFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Rafa&Pri");
Directory.CreateDirectory(newFolder, ds);
FileSecurity fs = new FileSecurity();
fs.AddAccessRule(new FileSystemAccessRule("Rafa&Pri", FileSystemRights.FullControl, AccessControlType.Allow));
string newFile = Path.Combine(newFolder, "Data.dat");
File.Create(newFile, 100, FileOptions.None, fs);
}
示例3: AllowFileAccess
public static bool AllowFileAccess(string path, FileSystemRights rights)
{
try
{
var security = new System.Security.AccessControl.FileSecurity();
var usersSid = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
security.AddAccessRule(new FileSystemAccessRule(usersSid, rights, AccessControlType.Allow));
File.SetAccessControl(path, security);
return true;
}
catch
{
return false;
}
}
示例4: SetAccessControl
public void SetAccessControl()
{
string tempFile = Path.GetTempFileName();
FileInfo fi = new FileInfo(tempFile);
FileSecurity expected = fi.GetAccessControl(AccessControlSections.All);
ExtendedFileInfo efi = new ExtendedFileInfo(tempFile);
Assert.IsNotNull(efi);
FileSecurity fileSecurity = new FileSecurity();
fileSecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));
efi.SetAccessControl(fileSecurity);
Assert.AreNotEqual(expected.GetSecurityDescriptorBinaryForm(), efi.GetAccessControl().GetSecurityDescriptorBinaryForm());
FileSecurity actualFileSecurity = File.GetAccessControl(tempFile);
AuthorizationRuleCollection rules = actualFileSecurity.GetAccessRules(true, true, typeof(NTAccount));
foreach (AuthorizationRule rule in rules)
{
FileSystemAccessRule accessRule = (FileSystemAccessRule)rule;
if (accessRule.IdentityReference.Value == "Everyone")
{
Assert.IsTrue(accessRule.AccessControlType == AccessControlType.Allow);
Assert.IsTrue(accessRule.FileSystemRights == FileSystemRights.FullControl);
}
}
fi.SetAccessControl(expected);
}
示例5: GetSecureFileStream
public static FileStream GetSecureFileStream(string path, int bufferSize, FileOptions options)
{
if (path == null)
throw new ArgumentNullException("path");
if (bufferSize <= 0)
throw new ArgumentOutOfRangeException("bufferSize");
if ((options &
~(FileOptions.Asynchronous | FileOptions.DeleteOnClose | FileOptions.Encrypted | FileOptions.RandomAccess |
FileOptions.SequentialScan | FileOptions.WriteThrough)) != FileOptions.None)
throw new ArgumentOutOfRangeException("options");
new FileIOPermission(FileIOPermissionAccess.Write, path).Demand();
SecurityIdentifier user = WindowsIdentity.GetCurrent().User;
FileSecurity fileSecurity = new FileSecurity();
fileSecurity.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.FullControl, AccessControlType.Allow));
fileSecurity.SetAccessRuleProtection(true, false);
fileSecurity.SetOwner(user);
// Attempt to create a unique file three times before giving up.
// It is highly improbable that there will ever be a name clash,
// therefore we do not check to see if the file first exists.
for (int attempt = 0; attempt < 3; attempt++)
{
try
{
return new FileStream(Path.Combine(path, Path.GetRandomFileName()), FileMode.CreateNew,
FileSystemRights.FullControl, FileShare.None, bufferSize, options, fileSecurity);
}
catch (IOException)
{
if (attempt == 2)
throw;
}
}
// This code can never be reached.
// The compiler thinks otherwise.
throw new IOException();
}
示例6: GetSecuritySettings
static private FileSecurity GetSecuritySettings() {
FileSecurity security = new FileSecurity();
security.SetAccessRuleProtection(true, false);
security.AddAccessRule(
(FileSystemAccessRule) security.AccessRuleFactory(
new NTAccount(
WindowsIdentity.GetCurrent().Name),
// Full control
-1,
false,
InheritanceFlags.None,
PropagationFlags.None,
AccessControlType.Allow));
return security;
}
示例7: ProjectCanNotBeOpened
public void ProjectCanNotBeOpened()
{
string projectFile = null;
IdentityReference identity = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
FileSystemAccessRule rule = new FileSystemAccessRule(identity, FileSystemRights.Read, AccessControlType.Deny);
FileSecurity security = null;
try
{
// Does not have .sln or .vcproj extension so loads as project
projectFile = Microsoft.Build.Shared.FileUtilities.GetTemporaryFile();
security = new FileSecurity(projectFile, System.Security.AccessControl.AccessControlSections.All);
security.AddAccessRule(rule);
File.SetAccessControl(projectFile, security);
ProjectRootElement p = ProjectRootElement.Open(projectFile);
}
catch (PrivilegeNotHeldException)
{
throw new InvalidProjectFileException("Running unelevated so skipping the scenario.");
}
finally
{
if (security != null)
{
security.RemoveAccessRule(rule);
}
File.Delete(projectFile);
Assert.AreEqual(false, File.Exists(projectFile));
}
}
示例8: SolutionCanNotBeOpened
public void SolutionCanNotBeOpened()
{
string solutionFile = null;
string tempFileSentinel = null;
IdentityReference identity = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
FileSystemAccessRule rule = new FileSystemAccessRule(identity, FileSystemRights.Read, AccessControlType.Deny);
FileSecurity security = null;
try
{
tempFileSentinel = Microsoft.Build.Shared.FileUtilities.GetTemporaryFile();
solutionFile = Path.ChangeExtension(tempFileSentinel, ".sln");
File.Copy(tempFileSentinel, solutionFile);
security = new FileSecurity(solutionFile, System.Security.AccessControl.AccessControlSections.All);
security.AddAccessRule(rule);
File.SetAccessControl(solutionFile, security);
ProjectRootElement p = ProjectRootElement.Open(solutionFile);
}
catch (PrivilegeNotHeldException)
{
throw new InvalidProjectFileException("Running unelevated so skipping this scenario.");
}
finally
{
if (security != null)
{
security.RemoveAccessRule(rule);
}
File.Delete(solutionFile);
File.Delete(tempFileSentinel);
Assert.AreEqual(false, File.Exists(solutionFile));
}
}
示例9: CopyAccessControlList
/// <summary>
/// Copies the Access Control List (ACL) from one file to another and specify additional ACL rules on the destination file.
/// </summary>
/// <param name="pathToSourceFile">The path to the source file.</param>
/// <param name="pathToDestinationFile">The path to the destination file.</param>
/// <param name="additionalFileSystemAccessRules">An array of <see cref="FileSystemAccessRule"/>. The additional ACLs.</param>
public static void CopyAccessControlList(string pathToSourceFile, string pathToDestinationFile, FileSystemAccessRule[] additionalFileSystemAccessRules)
{
if (additionalFileSystemAccessRules == null)
{
throw new ArgumentNullException("additionalFileSystemAccessRules");
}
CheckFilePathParameter("pathToSourceFile", pathToSourceFile);
CheckFilePathParameter("pathToDestinationFile", pathToDestinationFile);
FileSecurity sourceFileSecurity = File.GetAccessControl(pathToSourceFile);
FileSecurity destinationFileSecurity = new FileSecurity();
byte[] securityDescriptor = sourceFileSecurity.GetSecurityDescriptorBinaryForm();
destinationFileSecurity.SetSecurityDescriptorBinaryForm(securityDescriptor);
foreach (FileSystemAccessRule fileSystemAccessRule in additionalFileSystemAccessRules)
{
destinationFileSecurity.AddAccessRule(fileSystemAccessRule);
}
File.SetAccessControl(pathToDestinationFile, destinationFileSecurity);
}
示例10: SetReadAccessOnDefinitionFile
public static void SetReadAccessOnDefinitionFile(string definitionName, string user)
{
string filePathName = ScheduledJobStore.GetFilePathName(definitionName, "ScheduledJobDefinition");
FileSecurity fileSecurity = new FileSecurity(filePathName, AccessControlSections.Access);
FileSystemAccessRule fileSystemAccessRule = new FileSystemAccessRule(user, FileSystemRights.Read, AccessControlType.Allow);
fileSecurity.AddAccessRule(fileSystemAccessRule);
File.SetAccessControl(filePathName, fileSecurity);
}
示例11: TakeOwnership
public static void TakeOwnership(string path)
{
try {
using (var user = WindowsIdentity.GetCurrent())
{
var ownerSecurity = new FileSecurity();
ownerSecurity.SetOwner(user.User);
File.SetAccessControl(path, ownerSecurity);
var accessSecurity = new FileSecurity();
accessSecurity.AddAccessRule(new FileSystemAccessRule(user.User, FileSystemRights.FullControl, AccessControlType.Allow));
File.SetAccessControl(path, accessSecurity);
}
} catch(UnauthorizedAccessException) {
EndProcessAdmin();
}
}
示例12: SetReadAccessOnDefinitionFile
/// <summary>
/// Set read access on provided definition file for specified user.
/// </summary>
/// <param name="definitionName">Definition name</param>
/// <param name="user">Account user name</param>
public static void SetReadAccessOnDefinitionFile(
string definitionName,
string user)
{
string filePath = GetFilePathName(definitionName, DefinitionFileName);
// Get file security for existing file.
FileSecurity fileSecurity = new FileSecurity(
filePath,
AccessControlSections.Access);
// Create rule.
FileSystemAccessRule fileAccessRule = new FileSystemAccessRule(
user,
FileSystemRights.Read,
AccessControlType.Allow);
fileSecurity.AddAccessRule(fileAccessRule);
// Apply rule.
File.SetAccessControl(filePath, fileSecurity);
}
示例13: RestrictAdminAccess
private void RestrictAdminAccess(string path)
{
FileSecurity fileSecurity = new FileSecurity();
fileSecurity.SetAccessRuleProtection(true, false);
SecurityIdentifier securityIdentifier = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
FileSystemRights fileSystemRight = FileSystemRights.FullControl;
AccessControlType accessControlType = AccessControlType.Allow;
FileSystemAccessRule fileSystemAccessRule = new FileSystemAccessRule(securityIdentifier, fileSystemRight, accessControlType);
fileSecurity.AddAccessRule(fileSystemAccessRule);
File.SetAccessControl(path, fileSecurity);
}
示例14: setFileSecurity
public void setFileSecurity(String path)
{
FileSecurity fs;
FileSystemAccessRule fsRule = new FileSystemAccessRule(self.Name, FileSystemRights.FullControl, AccessControlType.Allow);
foreach (String file in Directory.GetFiles(path, "*.*", SearchOption.AllDirectories))
{
try
{
fs = new FileSecurity(file, AccessControlSections.Access);
fs.AddAccessRule(fsRule);
File.SetAccessControl(file, fs);
File.SetAttributes(file, FileAttributes.Normal);
fs.SetOwner(self.Owner);
}
catch (Exception err)
{
}
FileInfo finfo = new FileInfo(file);
finfo.Attributes = FileAttributes.Normal;
FileSecurity sec = finfo.GetAccessControl();
sec.SetAccessRule(fsRule);
sec.SetOwner(self.Owner);
Application.DoEvents();
try
{
File.Delete(file);
Application.DoEvents();
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
Application.DoEvents();
}
}
示例15: AlphaFS___CreateWithFileSecurity
public void AlphaFS___CreateWithFileSecurity()
{
Console.WriteLine("File.Create()");
string pathExpected = Path.GetTempPath("AlphaFS CreateWithFileSecurityExpected");
string pathActual = Path.GetTempPath("AlphaFS CreateWithFileSecurityActual");
File.Delete(pathExpected);
File.Delete(pathActual);
FileSecurity expectedFileSecurity = new FileSecurity();
expectedFileSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, AccessControlType.Allow));
expectedFileSecurity.AddAuditRule(new FileSystemAuditRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.Read, AuditFlags.Success));
using (new Alphaleonis.Win32.Security.PrivilegeEnabler(Alphaleonis.Win32.Security.Privilege.Security))
{
using (FileStream s1 = System.IO.File.Create(pathExpected, 4096, FileOptions.None, expectedFileSecurity))
using (FileStream s2 = File.Create(pathActual, 4096, FileOptions.None, expectedFileSecurity))
{
}
}
string expectedFileSecuritySddl = System.IO.File.GetAccessControl(pathExpected).GetSecurityDescriptorSddlForm(AccessControlSections.All);
string actualFileSecuritySddl = System.IO.File.GetAccessControl(pathActual).GetSecurityDescriptorSddlForm(AccessControlSections.All);
Assert.AreEqual(expectedFileSecuritySddl, actualFileSecuritySddl);
File.Delete(pathExpected, true);
File.Delete(pathActual, true);
}