c# - 如何在 .NET Core 2 中使用 ACL?

标签 c# asp.net-core .net-core acl

ASP.NET 核心 MVC 2

我的 Web 应用程序将托管在 IIS 8 (Windows Server 2012 R2) 上。我需要使用一些文件和目录的 ACL。我附上System.IO.FileSystem.AccessControl打包到我的项目,但我仍然有一些问题......我还在 Microsoft 网站上看到了一些关于这个主题的信息 here .但是我没有看到代码示例。

我知道如何通过 .NET Framework 使用 ACL,但我不知道如何在 .NET Core 2 中使用它。这是我的代码和我的问题(我评论了在 .NET Framework 中工作但不工作的代码无法在 .NET Core 2 中工作):

using System;
using System.IO;

// NuGet package: System.IO.FileSystem.AccessControl
using System.Security.AccessControl;
using System.Security.Principal;

namespace ACL_Sandbox
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var fileName = "data.txt";
                File.Create(fileName);

                // How to get the FileSecurity of the file in .NET Core 2?
                FileSecurity sec = null; // File.GetAccessControl(true,true,typeof(NTAccount));

                AuthorizationRuleCollection rules = sec.GetAccessRules(true, true, null);

                foreach (FileSystemAccessRule rule in rules)
                {
                    Console.WriteLine("AccessControlType: {0}", rule.AccessControlType);
                    Console.WriteLine("FileSystemRights: {0}", rule.FileSystemRights);
                    Console.WriteLine("IdentityReference.Value: {0}", rule.IdentityReference.Value);
                    Console.WriteLine("\n=======\n");
                }

                var sid = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
                string usersAccount = sid.Translate(typeof(NTAccount)).ToString();
                FileSystemAccessRule newRule = new FileSystemAccessRule(usersAccount, 
                    FileSystemRights.ExecuteFile, AccessControlType.Allow);
                sec.AddAccessRule(newRule);

                // How to set the access rule for the file in .NET Core 2?
                // File.SetAccessRule(sec);

            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: {0}", ex.Message);
            }

            Console.WriteLine("Press any key for exit...");
            Console.ReadKey();
        }
    }
}

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? 如果"is",那么如何在 .NET Core 2 中获取文件的 FileSecurity? 另外,如果"is",那么如何在 .NET Core 2 中设置文件的访问规则?

最佳答案

我还没有在 .NET Core 2.2 上尝试过这个,但是在 .NET Core 3.0 上我已经成功地使用了以下内容。您必须声明 using System.Security.AccessControl; 并引用 System.IO.FileSystem.AccessControl Jonas 在上面的评论中指出的 NuGet 包。好消息是,如果您是多目标的,那么此代码将与 .NET Standard 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);

关于c# - 如何在 .NET Core 2 中使用 ACL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49976006/

相关文章:

c# - 为什么在 c# 中使用 "yield"关键字的延迟执行会出现不同的运行时行为?

c# - 我如何模拟 IQueryable<T>

asp.net - 如何在 ASP.NET 中使用多个授权方案颁发相应的 Bearer 和 Cookie 标识?

c# - 根据服务方法/Web API 请求验证 Blazor 中的字段

asp.net-core - AKKA.NET 在 ASP.NET 中还是在控制台应用程序中?

c# - 更改 ViewModel 对象不会反射(reflect)对 View 的更改

c# - 在 ASP.NET 自承载 Web API 上配置 SSL

asp.net-core - 如何在 F# Asp Core Web API 中序列化 F# 可区分的联合类型

ubuntu - nginx上的多个网络核心应用程序同一个域

c# - 如何根据属性合并两个对象列表并将重复项合并到新对象中