问题描述
在我的.NET Core单元测试项目中,我试图创建一个文件夹,然后限制访问权限。
In my Unit Test project for .NET Core I'm trying to create a folder and then limit access permissions.
到目前为止,我已经实现了此代码的Windows版本:
So far I implemented Windows version of this code:
DirectoryInfo dirInfo = Directory.CreateDirectory(DriveManager.LogicalDrive + ":\\testDir");
DirectorySecurity dirSecurity = new DirectorySecurity(dirInfo.FullName, AccessControlSections.All);
var securityId = System.Security.Principal.WindowsIdentity.GetCurrent().User;
FileSystemAccessRule rule = new FileSystemAccessRule(securityId, FileSystemRights.ListDirectory, AccessControlType.Deny);
dirSecurity.AddAccessRule(rule);
dirInfo.SetAccessControl(dirSecurity);
显然是 WindowsIdentity.GetCurrent()。User
在Unix类型的OS上将无法使用。
我不确定其他功能是否也会不兼容。
Obviously WindowsIdentity.GetCurrent().User
won't work on Unix type OS.I'm not sure whether other functions may be incompatible either.
您能提出通用解决方案(适用于Windows和Unix类型的OS)吗?
Could you propose universal solution (for Windows and Unix type OS)?
预先感谢您的支持。
推荐答案
就像您关心的一样,请阅读 WindowsIdentity。*
API仅是Windows在其他系统上无法替代的功能(每种方法都会抛出PNSE)。临时解决方案可以是 project.json
中的 scripts
部分,也可以是自脚本,因为现在这是解决问题的方法在Linux和OSX上干净的.Net核心
Like you care read here WindowsIdentity.*
API is only Windows Feature with no replacement on other systems (every method will throw PNSE). Temporary solution can be scripts
section in project.json
or self scripting because at this moment is now way to get this in clean .Net core on Linux and OSX
这篇关于.NET Core-目录权限Linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!