.NET Core - directory permissions Linux

孤街浪徒 提交于 2020-03-03 04:27:41

问题


In my Unit Test project for .NET Core I'm trying to create a folder and then limit access permissions.

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);

Obviously WindowsIdentity.GetCurrent().User won't work on Unix type OS. I'm not sure whether other functions may be incompatible either.

Could you propose universal solution (for Windows and Unix type OS)?

Thanks in advance for your support.


回答1:


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



来源:https://stackoverflow.com/questions/41599714/net-core-directory-permissions-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!