【问题标题】:Checking for shared folder write access for current user检查当前用户的共享文件夹写入权限
【发布时间】:2011-08-12 06:20:14
【问题描述】:

我有以下方法来检查当前用户对给定网络位置的写入权限

DirectorySecurity shareSecurity = new DirectoryInfo(this.GetFileServerRootPath).GetAccessControl();

foreach (FileSystemAccessRule fsRule in shareSecurity.GetAccessRules(true, true, typeof(NTAccount)))
{
    // check write permission for current user
    if (AccessControlType.Allow == fsRule.AccessControlType &&
            FileSystemRights.Write == (fsRule.FileSystemRights & FileSystemRights.Write))
    {
        if (null != fsRule.IdentityReference &&
            fsRule.IdentityReference.Value == WindowsIdentity.GetCurrent().Name)
        {
            return true;
        }
    }
}
return false;

但问题是当授予用户组文件夹权限时,上述方法失败。

我不想通过写文件来检查权限并决定写访问权限。

有没有办法在IdentityReference.Value 中找到当前用户?或解决此问题的建议?

【问题讨论】:

  • 我猜它返回 false 由于这部分:fsRule.IdentityReference.Value == WindowsIdentity.GetCurrent().Name
  • 是的,当fsRule.IdentityReference.Value 是一个组时,我需要检查用户是否是该组的成员

标签: c# permissions c#-2.0


【解决方案1】:

这可能对你有用:

FileIOPermission writePermission = new FileIOPermission(FileIOPermissionAccess.Write, this.GetFileServerRootPath);

try
{
    writePermission.Demand();
    return true;
}
catch (SecurityException s)
{
    return false;
}

只是好奇 - 为什么不尝试/捕获您的写操作?

【讨论】:

    【解决方案2】:

    也许您应该在该目录上使用 DirectoryInfo 来获取其安全策略。

    【讨论】:

      猜你喜欢
      • 2018-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-22
      • 2019-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多