访问权限
public static void CheckAccess(string TempPath)
{
Dictionary<string, AuthorizationRule> dictionary = new Dictionary<string, AuthorizationRule>();
AuthorizationRuleCollection accessRules = File.GetAccessControl(TempPath).GetAccessRules(true, true, typeof(SecurityIdentifier));
foreach (AuthorizationRule item in accessRules)
{
Logger.Info("SR", $"SetFolderACL Value--- {item.IdentityReference.Value}");
if (!dictionary.ContainsKey(item.IdentityReference.Value))
{
dictionary.Add(item.IdentityReference.Value, item);
}
}
if (dictionary.ContainsKey("S-1-1-0"))
{
FileSystemAccessRule fileSystemAccessRule = (FileSystemAccessRule)dictionary["S-1-1-0"];
if (fileSystemAccessRule.AccessControlType != 0 || !((Enum)(object)fileSystemAccessRule.FileSystemRights).HasFlag((Enum)(object)FileSystemRights.FullControl))
{
if (SetFolderACL(TempPath, "Everyone", FileSystemRights.FullControl, AccessControlType.Allow))
{
Logger.Info("SR", $"Success SetFolderACL--- {TempPath}");
}
else
{
Logger.Error("SR", $"Fail SetFolderACL--- {TempPath}", (Exception)null);
}
}
else
{
Logger.Info("SR", $"{TempPath}文件已拥有everyone权限");
}
}
else if (SetFolderACL(TempPath, "Everyone", FileSystemRights.FullControl, AccessControlType.Allow))
{
Logger.Info("SR", $"Success SetFolderACL {TempPath}");
}
else
{
Logger.Error("SR", $"Fail SetFolderACL {TempPath}", (Exception)null);
}
}
private static void BBCUploadLimit(string BBCUploadPath)
{
try
{
if (Directory.Exists(BBCUploadPath))
{
DirectorySecurity directorySecurity = new DirectorySecurity();
directorySecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.Read, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
directorySecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.Write, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
Directory.SetAccessControl(BBCUploadPath, directorySecurity);
Logger.Info("BBC", $"Success SetFolderACL {BBCUploadPath}");
}
}
catch (Exception ex)
{
Logger.Error("BBC", $"Fail SetFolderACL {BBCUploadPath}", ex);
}
}