Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在.NETCore2中使用ACL?_C#_Asp.net Core_.net Core_Acl - Fatal编程技术网

C# 如何在.NETCore2中使用ACL?

C# 如何在.NETCore2中使用ACL?,c#,asp.net-core,.net-core,acl,C#,Asp.net Core,.net Core,Acl,ASP.NET核心MVC 2 我的web应用程序将托管在IIS 8(Windows Server 2012 R2)上。我需要使用一些文件和目录的ACL。我附加了我的项目包,但我仍然有一些问题。。。我在微软网站上也看到了一些关于这个主题的信息。但是我没有看到代码示例 我知道如何通过.NETFramework使用ACL,但我不知道如何在.NETCore2中使用ACL。这是我的代码和我的问题(我对在.NET Framework中工作但在.NET Core 2中不工作的代码进行了注释): 类FileSe

ASP.NET核心MVC 2

我的web应用程序将托管在IIS 8(Windows Server 2012 R2)上。我需要使用一些文件和目录的ACL。我附加了我的项目包,但我仍然有一些问题。。。我在微软网站上也看到了一些关于这个主题的信息。但是我没有看到代码示例

我知道如何通过.NETFramework使用ACL,但我不知道如何在.NETCore2中使用ACL。这是我的代码和我的问题(我对在.NET Framework中工作但在.NET Core 2中不工作的代码进行了注释):

FileSecurity
的JetBrains Rider IDE向我显示了这样的代码源:

// Decompiled with JetBrains decompiler
// Type: System.Security.AccessControl.FileSecurity
// Assembly: System.IO.FileSystem.AccessControl, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// MVID: 95551778-530B-4B9F-8EB6-1D54F85B3C4B
// Assembly location: /usr/share/dotnet/shared/Microsoft.NETCore.App/2.0.5/System.IO.FileSystem.AccessControl.dll

namespace System.Security.AccessControl
{
  [SecurityCritical]
  public sealed class FileSecurity : FileSystemSecurity
  {
    public FileSecurity()
    {
      throw new PlatformNotSupportedException(SR.PlatformNotSupported_AccessControl);
    }

    public FileSecurity(string fileName, AccessControlSections includeSections)
    {
      throw new PlatformNotSupportedException(SR.PlatformNotSupported_AccessControl);
    }
  }
}
这并不令人鼓舞

可以在.NET Core 2中使用ACL吗? 如果“是”,那么如何在.NETCore2中获得文件的文件安全性?
另外,如果回答“是”,那么如何在.NET Core 2中设置文件的访问规则?

我没有在.NET Core 2.2中尝试过这一点,但在.NET Core 3.0中,我成功地使用了以下内容。必须使用System.Security.AccessControl声明
并引用Jonas在上面的评论中提到的NuGet包。很好的一点是,如果您是多目标的,那么这段代码与.NET标准2.0和.NET Framework 2.0-4.x兼容

// Setup which parts of the ACL will be modified
AccessControlSections includeSections = AccessControlSections.Access | AccessControlSections.Group | AccessControlSections.Owner;

// FileSecurity sec = File.GetAccessControl(fileName, includeSections);
FileSecurity sec = new FileSecurity(fileName, includeSections);

// ==> Change file security (sec) here

// File.SetAccessControl(fileName, sec);
new FileInfo(fileName).SetAccessControl(sec);

使用FileInfo类的SetAccessControl方法,但请记住,您需要足够的权限。您找到了吗???@ScriptKitty.Net Core2没有
FileInfo.SetAccessControl()
method。实际上出现了一个
SetAccessControl()
当我从NuGet添加
System.IO.FileSystem.AccessControl
包,然后使用System.Security.AccessControl添加
时,FileInfo的实例方法。这是使用.NET Core 2.2实现的。您是否设法解决了此问题?如果该类存在于.NET Core中,它是否应该在所有平台上运行?
// Setup which parts of the ACL will be modified
AccessControlSections includeSections = AccessControlSections.Access | AccessControlSections.Group | AccessControlSections.Owner;

// FileSecurity sec = File.GetAccessControl(fileName, includeSections);
FileSecurity sec = new FileSecurity(fileName, includeSections);

// ==> Change file security (sec) here

// File.SetAccessControl(fileName, sec);
new FileInfo(fileName).SetAccessControl(sec);