【问题标题】:How to work with ACL in .NET Core 2?如何在 .NET Core 2 中使用 ACL?
【发布时间】:2018-04-23 07:56:00
【问题描述】:

ASP.NET Core MVC 2

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

我知道如何通过 .NET Framework 使用 ACL,但我不知道如何在 .NET Core 2 中执行此操作。这是我的问题代码(我评论了在 .NET Framework 中有效但没有的代码) t 在 .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? 如果“是”,那么如何在 .NET Core 2 中获取文件的 FileSecurity? 另外,如果“是”,那么如何在 .NET Core 2 中设置文件的访问规则?

【问题讨论】:

  • 使用 FileInfo 类的 SetAccessControl 方法,但请记住您需要足够的权限。
  • 你弄明白了吗??
  • @ScriptKitty,.Net Core2 没有FileInfo.SetAccessControl() 方法。
  • 当我从 NuGet 添加 System.IO.FileSystem.AccessControl 包然后添加 using System.Security.AccessControl; 时,实际上出现了 FileInfo 的 SetAccessControl() 实例方法。这使用 .NET Core 2.2。
  • 你解决了吗?如果该类存在于 .NET Core 中,是否应该在所有平台上运行?

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


【解决方案1】:

我没有在 .NET Core 2.2 上尝试过这个,但是在 .NET Core 3.0 上我已经成功地使用了以下内容。您必须声明 using System.Security.AccessControl; 并引用上面的 cmets 中 Jonas 指出的 System.IO.FileSystem.AccessControl 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 2017-12-16
    • 2021-08-28
    • 2016-11-12
    相关资源
    最近更新 更多