c# - 以编程方式向文件夹添加权限

标签 c# security permissions ntfs

我遇到一个问题,我需要为所有经过身份验证的用户添加对文件夹的访问权限,以存储与应用程序相关的设置。我发现这可以用下面的代码来完成...

var Info = new DirectoryInfo(settingsdir);
var Security = Info.GetAccessControl(AccessControlSections.Access);

Security.AddAccessRule(
    new FileSystemAccessRule(
        "Authenticated Users", FileSystemRights.Modify,
        InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
        PropagationFlags.None,
        AccessControlType.Allow));

我发现的问题是,“Authenticated Users”是 Windows 上的一个系统帐户,但是,在不同语言版本的 Windows 上,此帐户名称被翻译,例如在德国,此帐户称为“Authentifizierte Benutzer”。有没有办法知道这个帐户的正确名称(无需遍历每种语言并找到正确的帐户名称)。

最佳答案

我建议您使用 Well Known SID 列表(参见 http://support.microsoft.com/kb/243330)。经过身份验证的用户始终是 SID:S-1-5-11。如果您使用它,它应该与语言无关(但我没有测试过)。

创建一个 SecurityIdentifier并改用它:

var sid = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null); 

Security.AddAccessRule(
   new FileSystemAccessRule(
       sid,
       FileSystemRights.Modify,
       InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
       PropagationFlags.None,
       AccessControlType.Allow));

关于c# - 以编程方式向文件夹添加权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11478917/

相关文章:

c# - 用C#提取HtmlElement "onclick"属性的文本内容

c# - 在 WCF 中,为什么我们说传输是最不安全的选项?

permissions - 如何保存 StorageFile 以供以后使用?

git - 将 .git 目录保存在 Web 根目录中安全吗?

c# - 将某个二维数组索引下的数据移动到一维数组

c# - 1053 Windows 服务错误使用 .NET Core 3.1 Worker 服务

c# - 从 Func<T, bool> 获取 bool 值

PHP 登录和处理 session 安全

asp.net-mvc - 渗透测试您的 MVC 应用程序

sql-server - 只读访问执行SP/获取结果集的作用,无需间接修改db?