如何在.NET Core中修改文件访问控制

Fab*_*Fab 16 c# file-permissions .net-core

我正在尝试更改.NET Core中文件的权限.但是,似乎FileInfo已经没有SetAccessControl了.

// Create a new FileInfo object.
FileInfo fInfo = new FileInfo(FileName);

// Get a FileSecurity object that represents the 
// current security settings.
FileSecurity fSecurity = fInfo.GetAccessControl();

// Add the FileSystemAccessRule to the security settings. 
fSecurity.AddAccessRule(new FileSystemAccessRule(Account,
                                                Rights,
                                                ControlType));

// Set the new access settings.
fInfo.SetAccessControl(fSecurity);
Run Code Online (Sandbox Code Playgroud)

目标只是将执行权添加到文件的当前所有者(不是Windows或Unix特定功能).

有关如何在.NET Core上执行此操作的任何线索?

Pat*_*son 18

FileSecurity班是现在的一部分System.IO.FileSystem.AccessControl包.NET的核心.不再需要一种File.GetAccessControl方法,因此您需要自己实例化FileSecurity实例.

  • 另外,创建新的.NET Core 2.0库不会包含此程序集.您必须将`System.IO.FileSystem.AccessControl`添加为NuGet包. (6认同)
  • 一个例子将大大帮助。我在任何地方都找不到适用的.net core示例。 (2认同)

imb*_*tjd 13

这时候有两种扩展方法:GetAccessControland SetAccessControl、for FileInfoDirectoryInfoand等。

所以你可以使用var ac = new FileInfo(path).GetAccessControl(),这个表达式在 .NET Framework 和 .Net Core 中都有效。但你仍然需要dotnet add package System.IO.FileSystem.AccessControl.

File.GetAccessControl 在 .NET Core 中不可用。

参考:https : //docs.microsoft.com/dotnet/api/system.io.filesystemaclextensions.getaccesscontrol

  • 应该是公认的答案,所以更容易使用 (3认同)

Fab*_*Fab 11

如何在 Windows 上获取和修改用户组其他权限

我终于实现了Windows文件权限访问:

1.获取文件安全:

      var security = new FileSecurity(fileSystemInfoFullName, 
                AccessControlSections.Owner | 
                AccessControlSections.Group |
                AccessControlSections.Access);
Run Code Online (Sandbox Code Playgroud)

2、获取授权规则:

var authorizationRules = security.GetAccessRules(true, true, typeof(NTAccount));
Run Code Online (Sandbox Code Playgroud)

3.获取owner的授权规则:

var owner = security.GetOwner(typeof(NTAccount));
foreach (AuthorizationRule rule in authorizationRules)
{
    FileSystemAccessRule fileRule = rule as FileSystemAccessRule;
    if (fileRule != null)
    {
        if (owner != null && fileRule.IdentityReference == owner)
        {
             if (fileRule.FileSystemRights.HasFlag(FileSystemRights.ExecuteFile) ||
                fileRule.FileSystemRights.HasFlag(FileSystemRights.ReadAndExecute) ||
                fileRule.FileSystemRights.HasFlag(FileSystemRights.FullControl))
            {
                ownerRights.IsExecutable = true;
            }
        }
        else if (group != null && fileRule.IdentityReference == group)
        {
            // TO BE CONTINUED...
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

4. 为所有者添加规则:

security.ModifyAccessRule(AccessControlModification.Add,
    new FileSystemAccessRule(owner, FileSystemRights.Modify, AccessControlType.Allow),
    out bool modified);
Run Code Online (Sandbox Code Playgroud)

5. 奖金

如何获得groupothers,或...我对等价事物的定义?

var group = security.GetGroup(typeof(NTAccount));

var others = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null)
                 .Translate(typeof(NTAccount));
Run Code Online (Sandbox Code Playgroud)

注意:此代码来自我的开源项目Lx.Shell

  • 这太打击了……为什么他们要把事情搞得这么复杂?为什么不在 DirectoryInfo 和 FileInfo 上使用一些扩展方法? (2认同)

Jon*_*n R 5

文档说这是支持的并且它有效(对我来说)。https://learn.microsoft.com/en-us/dotnet/api/system.io.filesystemaclextensions?view=dotnet-plat-ext-3.1确实有 SetAccessControl 方法

请务必添加System.IO.FileSystem.AccessControlNuGet 包。

这是我在 .NET Framework 中的内容:

var ds = new DirectorySecurity();
ds.AddAccessRule(new FileSystemAccessRule(adminSI, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
ds.SetAccessRuleProtection(true, false); // disable inheritance and clear any inherited permissions

Directory.SetAccessControl(<path to directory>, ds);
Run Code Online (Sandbox Code Playgroud)

以下是它在 .NET Core 3.1 中的工作原理。只有最后一行不同:

var ds = new DirectorySecurity();
ds.AddAccessRule(new FileSystemAccessRule(adminSI, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
ds.SetAccessRuleProtection(true, false); // disable inheritance and clear any inherited permissions

System.IO.FileSystemAclExtensions.SetAccessControl(new DirectoryInfo(<path to directory>), ds);
Run Code Online (Sandbox Code Playgroud)