Support for File Security in .NET Core

后端 未结 3 2187
眼角桃花
眼角桃花 2021-02-14 10:10

We were porting a .NET 4.0 class Library to .NET Core 1.1 and struck with an issue of very limit support for file Security and permissions in .NET Core CLR.

We were tryin

3条回答
  •  旧时难觅i
    2021-02-14 10:41

    Wow, so much information out there and even though the documentation says that in .NET Core 3.1 you cannot do DirectoryInfo.SetAccessRule, it compiled and worked!

    UPDATE: Aha! The documentation says this IS supported and it works. https://docs.microsoft.com/en-us/dotnet/api/system.io.filesystemaclextensions?view=dotnet-plat-ext-3.1 DOES have a SetAccessControl method

    Be sure to add the System.IO.FileSystem.AccessControl NuGet package.

    Here's what I had in .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(, ds);
    

    And here's what it is working in .NET Core 3.1. Only the last line is different:

    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(), ds);
    

提交回复
热议问题