I have the following code in C#:
if (!Directory.Exists(inputFolder))
{
return true;
}
else
{
var me = new DirectoryInfo(inputFolder);
AuthorizationRuleCollection rules = null;
WindowsIdentity identity = null;
try
{
rules = me.GetAccessControl().GetAccessRules(true, true, typeof(SecurityIdentifier));
identity = WindowsIdentity.GetCurrent();
}
catch (UnauthorizedAccessException uae)
{
return false;
}
string userSID = identity.User.Value;
foreach (FileSystemAccessRule rule in rules)
{
if ((rule.FileSystemRights.HasFlag(FileSystemRights.Read) ||
rule.FileSystemRights.HasFlag(FileSystemRights.ReadAttributes) ||
rule.FileSystemRights.HasFlag(FileSystemRights.ReadData) ||
rule.FileSystemRights.HasFlag(FileSystemRights.ReadAndExecute)) && rule.AccessControlType == AccessControlType.Deny)
{
return false;
}
}
return true;
}
And it works correctly in Windows, however, when I try to run this on mac OS X (Visual Studio for Mac) it throws an exception when trying to get the information from the method "GetAccessRules" or even the "GetAccessControl" saying:
"Access Control List (ACL) APIs are part of resource management on Windows and are not supported on this platform."
So here is my question:
Is there an alternative to do this in .net core? Or how should i manage these kind of permissions in .net core but in mac OS X?
I am assuming you are using System.IO.FileSystem.AccessControl
to make GetAccessControl()
work on .NET Core
? Unfortunately these APIs are closely tied to Windows low level APIs and won't work on Unix/Linux systems.
However, there is an alternative. You could give Mono.Posix.NETStandard
a try. Here is the Nuget link.
You can call UnixDirectoryInfo()
and get or set FileAccessPermissions
that way.
Updated
I found the solution and it was the same provided by Ruv in the answer above.
So, installing the Nuget Package Mono.Posix.NETStandard works. Just get the information of the directory/file: var unixDirInfo = new Mono.Unix.UnixDirectoryInfo('path');
Then you can check the permissions like this:
unixDirInfo.canAccess(AccessModes.F_OK) // is a file or directory
unixDirInfo.canAccess(AccessModes.R_OK) // accessible for reading
unixDirInfo.canAccess(AccessModes.W_OK) // accessible for writing
unixDirInfo.canAccess(AccessModes.X_OK) // accessible for executing
or from the FileAccessPermissions:
FileAccessPermissions permissions = unixDirInfo.FileAccessPermissions;
permissions.HasFlag(FileAccessPermissions.UserRead);
permissions.HasFlag(FileAccessPermissions.UserWrite);
permissions.HasFlag(FileAccessPermissions.UserExecute);
permissions.HasFlag(FileAccessPermissions.UserExecute .UserReadWriteExecute);
You can check the FileAccessPermission enum for more information.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.