阅读C#中的共享权限 [英] Reading Share Permissions in C#

查看:204
本文介绍了阅读C#中的共享权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以读取分配给共享文件夹的共享权限?我能够以编程方式读取本地安全设置(在右键单击">属性">安全性"下找到的设置)没有问题.但是,我想知道如何在右键单击">共享和安全性...">权限"下读取权限

Is it possible to read the sharing permissions assigned to a shared folder? I'm able to read in the local security settings programmaticaly (the ones found under Right Click > Properties > Security) no problem. But, I'm wondering how I can read the permissions under Right Click > Sharing and Security... > Permissions

这是我要阅读的权限的图像:

Here is an image of the Permissions I want to read:

这可能吗?如果有帮助,我正在运行XP Pro计算机.

Is this possible? I'm running an XP Pro machine if it helps.

根据我的回答,我能够遍历所有共享,并获得了(即运行程序的人)对该共享的访问权限,但是还没有找到一种阅读方法其他对该共享具有的权限.这是使用 Win32_Share 类完成的,但是它没有用于获取其他用户的共享权限的选项.如果有人有任何有用的提示,那将是巨大的帮助.

As per my answer I was able to iterate through all the shares, and get the access you (ie the person running the program) has on that share, but have not found a way to read the permissions others have on that share. This was done using Win32_Share class, however it does not have an option for getting the share permissions of other users. If anyone has any helpful hints that would be a huge help.

推荐答案

我能够通过扩展所采用的方法来使此工作正常进行同样,请确保运行此代码的进程模拟了服务器上的特权用户.

I was able to get this working by expanding on the approach taken by Petey B. Also, be sure that the process that runs this code impersonates a privileged user on the server.

    using System;
    using System.Management;

    ...

    private static void ShareSecurity(string ServerName)
    {
        ConnectionOptions myConnectionOptions = new  ConnectionOptions();

        myConnectionOptions.Impersonation = ImpersonationLevel.Impersonate;            
        myConnectionOptions.Authentication = AuthenticationLevel.Packet;

        ManagementScope myManagementScope = 
            new ManagementScope(@"\\" + ServerName + @"\root\cimv2", myConnectionOptions);

        myManagementScope.Connect();

        if (!myManagementScope.IsConnected)
            Console.WriteLine("could not connect");
        else
        {
            ManagementObjectSearcher myObjectSearcher = 
                new ManagementObjectSearcher(myManagementScope.Path.ToString(), "SELECT * FROM Win32_LogicalShareSecuritySetting");

            foreach(ManagementObject share in myObjectSearcher.Get())
            {
                Console.WriteLine(share["Name"] as string);
                InvokeMethodOptions options = new InvokeMethodOptions();
                ManagementBaseObject outParamsMthd = share.InvokeMethod("GetSecurityDescriptor", null, options);
                ManagementBaseObject descriptor = outParamsMthd["Descriptor"] as ManagementBaseObject;
                ManagementBaseObject[] dacl =  descriptor["DACL"] as ManagementBaseObject[];                  

                foreach (ManagementBaseObject ace in dacl)
                {
                    try
                    {
                        ManagementBaseObject trustee = ace["Trustee"] as ManagementBaseObject;
                        Console.WriteLine(
                            trustee["Domain"] as string + @"\" + trustee["Name"] as string + ": " +
                            ace["AccessMask"] as string + " " + ace["AceType"] as string
                        );                            
                    }
                    catch (Exception error)
                    {
                        Console.WriteLine("Error: "+ error.ToString());
                    }
                }
            }               
        }
    }

这篇关于阅读C#中的共享权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆