本文介绍了.NET Core 中的文件安全支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在将 .NET 4.0 类库移植到 .NET Core 1.1,但遇到了对 .NET Core CLR 中文件安全性和权限支持非常有限的问题.

We were porting a .NET 4.0 class Library to .NET Core 1.1 and struck with an issue of very limit support for file Security and permissions in .NET Core CLR.

我们试图为文件设置访问控制权限,如下所示,似乎 FileInfo 不再有任何 SetAccessControl 或 GetAccessControl.

We were trying to set the access control permissions to a file as below, and it seems that FileInfo doesn't have any SetAccessControl or GetAccessControl anymore.

 // Get a FileSecurity object that represents the
    // current security settings.
    FileSecurity fSecurity = File.GetAccessControl(fileName);

    // Add the FileSystemAccessRule to the security settings.
    fSecurity.AddAccessRule(new FileSystemAccessRule(account,
        rights, controlType));

    // Set the new access settings.
    File.SetAccessControl(fileName, fSecurity);

目标只是为文件的当前所有者添加执行权.

The goal is just to add execution right to the current owner of a file.

推荐答案

由于使用率低且特定于 Windows,这些 API 尚未包含在 .NET Standard 中.

These APIs haven't been included in .NET Standard due to low use and being Windows specific.

请参阅此处有关其从 .NET Standard 中排除的讨论:https://github.com/dotnet/标准/问题/15

See discussion here regarding its exclusion from .NET Standard: https://github.com/dotnet/standard/issues/15

作为一种解决方法,有一个提供此功能的 NuGet 包:https://www.nuget.org/packages/System.IO.FileSystem.AccessControl/

As a workaround there is a NuGet package which provides this functionality: https://www.nuget.org/packages/System.IO.FileSystem.AccessControl/

还有一个相关问题:如何修改文件访问控制在 .NET Core 中

这篇关于.NET Core 中的文件安全支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 01:27