![已通过代码成功修改用户组的权限,但更改未反映在用户组的“安全”选项卡中](https://linux22.com/image/784052/%E5%B7%B2%E9%80%9A%E8%BF%87%E4%BB%A3%E7%A0%81%E6%88%90%E5%8A%9F%E4%BF%AE%E6%94%B9%E7%94%A8%E6%88%B7%E7%BB%84%E7%9A%84%E6%9D%83%E9%99%90%EF%BC%8C%E4%BD%86%E6%9B%B4%E6%94%B9%E6%9C%AA%E5%8F%8D%E6%98%A0%E5%9C%A8%E7%94%A8%E6%88%B7%E7%BB%84%E7%9A%84%E2%80%9C%E5%AE%89%E5%85%A8%E2%80%9D%E9%80%89%E9%A1%B9%E5%8D%A1%E4%B8%AD.png)
我有 C# 代码(在最后):
- 创建文件
- 打印当前 ACL
- 授予内置用户组对先前创建的文件的“写权限”
- 打印当前修改的 ACL
正如您在控制台输出中看到的,写权限已通过代码成功分配。
我的问题是:为什么文件的安全选项卡没有反映用户组的权限更改?
C# 代码:
var file = "sectest.txt";
File.WriteAllText(file, "File security test.");
var sid = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
string strBuiltInUsersAccount = sid.Translate(typeof(NTAccount)).ToString();
FileSecurity fileSecurity = new FileSecurity(file,
AccessControlSections.Owner |
AccessControlSections.Group |
AccessControlSections.Access);
Console.WriteLine("AFTER CREATE:");
ShowSecurity(fileSecurity); // BUILTIN\Users group doesn't have Write permission
// short: give "builtin\users" write permissions
var fsAccessRule = new FileSystemAccessRule(strBuiltInUsersAccount,
FileSystemRights.Write,
AccessControlType.Allow);
fileSecurity.ModifyAccessRule(AccessControlModification.Add, fsAccessRule, out bool modified);
Console.WriteLine();
Console.WriteLine("AFTER MODIFY:");
ShowSecurity(fileSecurity); // BUILTIN\Users has Write permission
答案1
这只会修改 ACE 中的单个访问规则。然后需要将其保留到文件中。请参阅 File.SetAccessControl(filePath, fileSecurity)
https://learn.microsoft.com/en-us/dotnet/api/system.security.accesscontrol.filesecurity?view=net-7.0