简体   繁体   English

C# 中的读取共享权限

[英]Reading Share Permissions in 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:这是我要阅读的权限的图像:

共享权限

Is this possible?这可能吗? I'm running an XP Pro machine if it helps.如果有帮助,我正在运行 XP Pro 机器。

Edit:编辑:

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.这是使用Win32_Share class 完成的,但是它没有获取其他用户共享权限的选项。 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.通过扩展Petey B 所采用的方法,我能够完成这项工作。另外,请确保运行此代码的进程模拟服务器上的特权用户。

    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());
                    }
                }
            }               
        }
    }

I know you can with Windows Home Server: http://msdn.microsoft.com/en-us/library/bb425864.aspx我知道您可以使用 Windows 家庭服务器: http://msdn.microsoft.com/en-us/library/bb425864.aspx

You can do this in MMC and most of that is available through code, so it should be possible.您可以在 MMC 中执行此操作,其中大部分可通过代码获得,因此应该是可能的。 If you can't find it there then you should check out Windows API calls.如果您在那里找不到它,那么您应该查看 Windows API 调用。 I've seen it done in C++, so it should also be possible in C#.我已经看到它在 C++ 中完成,因此在 C# 中也应该可以。 Sorry, I don't have any sample code or other links to provide for those.抱歉,我没有任何示例代码或其他链接来提供这些。 I'll see if I can dig some up though.不过我看看能不能挖出来。

I also just saw this on SO: how to create shared folder in C# with read only access?我也刚刚在 SO 上看到了这个: 如何在 C# 中创建具有只读访问权限的共享文件夹?

Another good link: http://social.msdn.microsoft.com/Forums/en/windowssdk/thread/de213b61-dc7e-4f33-acdb-893aa96837fa另一个很好的链接: http://social.msdn.microsoft.com/Forums/en/windowssdk/thread/de213b61-dc7e-4f33-acdb-893aa96837fa

The best I could come up with is iterating through all the shares on a machine and reading the permissions you have on the share.我能想到的最好的办法是遍历机器上的所有共享并读取对共享的权限。

ManagementClass manClass = new ManagementClass(@"\\" +computerName +@"\root\cimv2:Win32_Share"); //get shares

//run through all the shares
foreach (ManagementObject objShare in manClass.GetInstances())
{
  //ignore system shares
  if (!objShare.Properties["Name"].Value.ToString().Contains('$'))
  {
    //print out the share name and location
    textBox2.Text += String.Format("Share Name: {0}  Share Location: {1}", objShare.Properties["Name"].Value, objShare.Properties["Path"].Value) + "\n";

    Int32 permissions = 0;

    try
    {
      //get the access values you have
      ManagementBaseObject result = objShare.InvokeMethod("GetAccessMask", null, null);

      //value meanings: http://msdn.microsoft.com/en-us/library/aa390438(v=vs.85).aspx
      permissions = Convert.ToInt32(result.Properties["ReturnValue"].Value);
    }
    catch (ManagementException me)
    {
      permissions = -1; //no permissions are set on the share
    }

    textBox2.Text += "You have permissions: " + permissions + "\n\n";

  }
}

If anyone could figure out how to get the permissions others have on the share that would be amazing.如果有人能弄清楚如何获得其他人对共享的权限,那就太棒了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM