确定文件是否具有 .NET 中的写访问权限

发布于 2024-07-26 15:43:22 字数 282 浏览 2 评论 0原文

我创建了一个软件产品,其作用类似于 Apache mod_rewrite,但适用于 .NET。 要求之一是支持 RewriteLog 命令,该命令设置日志记录路径。 这工作正常,但我遇到的最大问题是用户设置了 IIS 用户无权访问的路径。

我想通过检查路径的权限以确保写访问权限可用,向用户抛出有关缺乏写访问权限的异常。

我已经想到可以尝试将文件写入硬盘进行测试。 但这看起来很老套,我想找到一种非老套的方法来做到这一点? 有人知道我可以调用一些中等信任安全的 API 来检查文件的写入权限吗?

I have created a software product, that acts like the Apache mod_rewrite, but for .NET. One of the requirements is to support the RewriteLog command which sets a path for logging. This works fine, however the biggest issue I run in to is that users set a path that the IIS user doesn't have access to.

I would like to throw an exception to the user about the lack of write access, by checking the permissions of the path to make sure write access is available.

I have already figured out that I can trying write a file to the hard disk to test. But this seems hacky and I would like to find a non-hacky way of doing this? Anybody know of some API that I can call that is Medium-Trust safe to check the file permissions for write access?

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

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

发布评论

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

评论(1

我不会写诗 2024-08-02 15:43:22

听起来您正在寻找的是 FileIOPermission< /a> (在 System.Security.Permissions 中)类,它允许您在继续之前检查您是否有权限...也就是说,您仍然可以通过捕获异常来检测您没有权限; 并且您仍然应该预期在实际写入操作期间出现异常的可能性。

using System.Security.Permissions;
...
public void CheckForWriteRights(string path)
{
    FileIOPermission permission = new FileIOPermission(FileIOPermissionAccess.Write, path);
    try
    {
        permission.Demand();
        // the application does have Write rights to the path
    }
    catch
    {
        // the application doesn't have rights to the path
    }
}

Sounds like what you're looking for is the FileIOPermission (in System.Security.Permissions) class, which lets you check if you have permissions before proceeding... that said, you still detect that you don't have permissions by catching an exception; and you should still expect the possibility of an exception during the actual write operation.

using System.Security.Permissions;
...
public void CheckForWriteRights(string path)
{
    FileIOPermission permission = new FileIOPermission(FileIOPermissionAccess.Write, path);
    try
    {
        permission.Demand();
        // the application does have Write rights to the path
    }
    catch
    {
        // the application doesn't have rights to the path
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文