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

时间:2018-04-23 07:56:01

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

ASP.NET Core MVC 2

我的网络应用程序将托管在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? 如果“是”,那么如何在.NET Core 2中获取文件的FileSecurity? 此外,如果“是”,那么如何在.NET Core 2中设置文件的访问规则?

1 个答案:

答案 0 :(得分:0)

我没有在.NET Core 2.2上尝试过此方法,但是在.NET Core 3.0中,我成功使用了以下方法。您必须声明Authors并引用Jonas在上面的注释中指出的System.IO.FileSystem.AccessControl NuGet程序包。令人高兴的是,如果您是多目标用户,那么此代码将与.NET Standard 2.0和.NET Framework 2.0-4.x兼容。

Please enter the author. 
Joe Smith
Would you like you add an additional author? (Y or N) 
y
Please enter the author. 
Billy Bob
Would you like you add an additional author? (Y or N) 
n
Joe Smith
Billy Bob