【发布时间】: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