我的 C# 应用程序如何测试用户是否具有“阅读”权限? 访问网络共享?

发布于 2024-07-17 20:13:14 字数 312 浏览 6 评论 0原文

我正在开发一个胖客户端应用程序,该应用程序经常遇到访问网络共享的“问题”。 在对服务器进行任何 IO 之前,我的应用程序会测试共享(通常采用 \\server\share$ 形式)是否存在。 这对于检测客户端与服务器失去连接的情况非常有效,但仍然存在一些奇怪的情况,其中存在隐藏共享,但用户无权从共享内读取内容。 有人可以分享(没有双关语)测试当前用户是否可以读取共享上的文件所需的 C# 代码吗? 我应该查询共享的 ACL 或共享中的文件吗? 如果共享为空怎么办? 如果用户是混合环境中的本地非管理员(XP Pro 工作站、Novell 网络上没有域的 Windows 2003 服务器)怎么办?

I work on a thick-client app that often runs into "issues" accessing network shares. Before doing any IO with the server, my app tests whether the share (usually of the form \\server\share$) exists. This works fine for detecting those scenarios in which the client has lost its connection to the server, but there are still those odd scenarios where the hidden share exists but the user does not have the rights to read from the within the share. Can someone share (no pun intended) the C# code required to test whether the current user can read files on a share? Should I be querying the share's ACL or the files within the share? What if the share is empty? What if the user is a local non-admin in a mixed environment (XP Pro workstation, Windows 2003 server without a domain on a Novell network)?

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

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

发布评论

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

评论(2

菩提树下叶撕阳。 2024-07-24 20:13:14

最简单的方法就是直接执行(例如尝试读取文件)。 正如 Jared 提到的,没有办法确保您将来能够阅读(网络故障、权限更改等)。

就代码而言,您可以使用 DirectoryInfo类进行一些尝试的答案:

        string remotePath = @"\\server\share$";
        bool haveAccess = false;

        DirectoryInfo di = new DirectoryInfo(remotePath);
        if (di.Exists)
        {
            try
            {
                // you could also call GetDirectories or GetFiles
                // to test them individually
                // this will throw an exception if you don't have 
                // rights to the directory, though
                var acl = di.GetAccessControl();
                haveAccess = true;
            }
            catch (UnauthorizedAccessException uae)
            {
                if (uae.Message.Contains("read-only"))
                {
                    // seems like it is just read-only
                    haveAccess = true;
                }
                // no access, sorry
                // do something else...
            }
        }

上面的代码有很多缺点(例如硬编码的“只读”测试),但这只是一个用于说明您可以做什么的示例。 DirectoryInfo 还有一些其他帮助程序方法,可用于列出文件夹中的文件。 如果您无权访问,这些方法将引发 UnauthorizedAccessException 异常,您可以使用该异常来测试访问失败的原因。 查看 GetAccessControl 上的信息,了解有关它引发的异常的更多详细信息。

The easiest way is to just do it (i.e. try to read a file, for example). As Jared mentioned, there is no way to make sure that you will be able to read in the future (network failure, change of permissions, etc).

As far as code goes, you could use the DirectoryInfo class for some attempts at an answer:

        string remotePath = @"\\server\share$";
        bool haveAccess = false;

        DirectoryInfo di = new DirectoryInfo(remotePath);
        if (di.Exists)
        {
            try
            {
                // you could also call GetDirectories or GetFiles
                // to test them individually
                // this will throw an exception if you don't have 
                // rights to the directory, though
                var acl = di.GetAccessControl();
                haveAccess = true;
            }
            catch (UnauthorizedAccessException uae)
            {
                if (uae.Message.Contains("read-only"))
                {
                    // seems like it is just read-only
                    haveAccess = true;
                }
                // no access, sorry
                // do something else...
            }
        }

There are many shortcomings in the above code (such as the hard-coded "read-only" test), but it is just an example used to illustrate what you could do. DirectoryInfo has a few other helper methods that you can use to list the files in the folder. If you don't have access, the methods will throw an UnauthorizedAccessException exception which you can use to test why the access failed. Check out the info on GetAccessControl for further details on the exceptions it throws.

世俗缘 2024-07-24 20:13:14

确定您是否曾经拥有从共享读取的权限的第一个最可靠方法是尝试

  1. 从共享中读取
  2. 处理读取时可能发生的错误,并考虑失败的尝试

不幸的是,虽然基于根据您的描述,您正在尝试确定您是否拥有共享的读取权限。 没有办法可靠地确定这一点。

无论有多少 ACL、目录等...您在查看完它们后都可能会通过多种机制失去对共享的访问权限。 最明显的一个是网络份额下降。 您所能确定的是您曾经拥有该共享的权限。

The #1 most reliable way to determine if you used to have permission to read from the share is to

  1. Try and read from the share
  2. Handle errors that could occur while reading and consider that a failed attempt

Unfortunately though based on your description you are trying to determine if you will have read permission to the share. There is no way to reliably determine this.

No matter how many ACLs, directories, etc ... you look at the moment you're done looking at them you could lose access to the share via any number of mechanisms. The most obvious one is the network share going down. All you can determine is that you used to have permission to the share.

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