检查当前用户的共享文件夹写入权限

发布于 2024-11-29 03:04:12 字数 844 浏览 3 评论 0原文

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

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中找到当前用户?或解决这个问题的建议?

I have following method to check current user have write access to given network location

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;

but problem is when folder permission given to user group, above method is failed.

I don't want to check the permissions by writing a file and decide the write access permissions.

is there any way to find current user in the IdentityReference.Value? or suggestions to overcome this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尴尬癌患者 2024-12-06 03:04:12

这可能对你有用:

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

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

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

This may work for you:

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

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

Just curious - why not just try/catch your write operation?

断肠人 2024-12-06 03:04:12

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

May be you should use DirectoryInfo on that directory to get its security policies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文