这允许用户禁止访问共享文件夹防止WNetAddConnection2类

问题描述 投票:1回答:2

我已经开发C#Windows应用程序。操作系统是Windows 7

要求:是访问网络共享文件夹“测试”使用使用WNetAddConnection2类证书代码。

限制:一些用户有此共享文件夹“测试”的访问,但对于其他用户,“拒绝”共享权限设置。

在代码WNetAddConnection2验证错误的用户名/密码,它会给我的错误。

例如

从LAN“用户A”正在尝试访问共享文件夹“测试”使用run command,他是不是能够访问“访问被拒绝”,因为他没有权限。

但问题是WNetAddConnection2类允许“用户A”成功建立网络连接。感染“WNetAddConnection2允许所有用户域”。类是验证访问权限。

码是

private void btnValidate_Click(object sender, EventArgs e)
     {
         bool valid = false;
         try
         {              
             NetworkCredential NC = new NetworkCredential(txtUserName.Text.Trim(), txtPassword.Text.Trim());  

         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message.ToString());
         }
     }

public class NetworkConnection : IDisposable
 {
     string _networkName;
     uint dwFlags;
     public NetworkConnection(string networkName, NetworkCredential credentials)
     {
         _networkName = networkName;

         var netResource = new NetResource()
         {
             Scope = ResourceScope.GlobalNetwork,
             ResourceType = ResourceType.Disk,
             DisplayType = ResourceDisplaytype.Share,
             RemoteName = networkName
         };

         var userName = string.IsNullOrEmpty(credentials.Domain)
             ? credentials.UserName
             : string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName);

         var result = WNetAddConnection2(netResource,"","",0x00000008 | 0x00000010);

         if (result != 0)
         {
             string strErrMsg = "";
             if (result == 67)
             {
                 strErrMsg = "The network name cannot be found.";
             }
             if (result == 86)
             {
                 strErrMsg = "Invalid UserName or Password for ProBiz server";
             }
             else if (result == 1219)
             {
                 strErrMsg = "Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed.Close application to Disconnect all previous connections to the server or shared resource and try again.";
             }

             throw new Win32Exception(result, "Error connecting to "+networkName+" remote share.Error Code:"+result.ToString()+"."+strErrMsg);
         }
         else
         {
             MessageBox.Show("Test connection is successful for "+ networkName);
         }
     }

     ~NetworkConnection()
     {
         Dispose(false);
     }

     public void Dispose()
     {
         Dispose(true);
         GC.SuppressFinalize(this);
     }

     protected virtual void Dispose(bool disposing)
     {
         WNetCancelConnection2(_networkName,  1, true  );
         var command = "NET USE  /delete *";
         ExecuteCommand(command, 5000);

     }
     public static int ExecuteCommand(string command, int timeout)
     {
         var processInfo = new ProcessStartInfo("cmd.exe", "/C " + command)
         {
             CreateNoWindow = true,
             UseShellExecute = false,
             WorkingDirectory = "C:\\",
         };

         var process = Process.Start(processInfo);
         process.WaitForExit(timeout);
         var exitCode = process.ExitCode;
         process.Close();
         return exitCode;
     } 

     [DllImport("mpr.dll")]
     private static extern int WNetAddConnection2(NetResource netResource,
         string password, string username, int flags);

     [DllImport("mpr.dll")]
     private static extern int WNetCancelConnection2(string name, int flags,
         bool force);
 }

 [StructLayout(LayoutKind.Sequential)]
 public class NetResource
 {
     public ResourceScope Scope;
     public ResourceType ResourceType;
     public ResourceDisplaytype DisplayType;
     public int Usage;
     public string LocalName;
     public string RemoteName;
     public string Comment;
     public string Provider;
 }

 public enum ResourceScope : int
 {
     Connected = 1,
     GlobalNetwork,
     Remembered,
     Recent,
     Context
 };

 public enum ResourceType : int
 {
     Any = 0,
     Disk = 1,
     Print = 2,
     Reserved = 8,
 }

 public enum ResourceDisplaytype : int
 {
     Generic = 0x0,
     Domain = 0x01,
     Server = 0x02,
     Share = 0x03,
     File = 0x04,
     Group = 0x05,
     Network = 0x06,
     Root = 0x07,
     Shareadmin = 0x08,
     Directory = 0x09,
     Tree = 0x0a,
     Ndscontainer = 0x0b
 }
c# windows networking directory shared
2个回答
0
投票

在设计上,连接到共享需要访问该共享 - 它不需要访问该共享的根目录。

通过运行框中打开共享打开共享的根目录,因此它需要至少读取权限的目录,以及到共享。该WNetAddConnection2()API,通过比较,只需要访问该共享。

它有这样的工作方式,因为有时需要给只有某些子目录有人访问,但并非根目录下。如果连接到共享需要访问根目录,这将是不可能的。

连接到共享后,您可以通过试图枚举文件来测试访问根目录。如果您收到拒绝访问异常时,用户不能访问。


-2
投票

我有同样的问题,当我用IIS 7.5中的部署我的C#项目,但它是惊人的,当我做删除我的代码的注销手续。 。 。

我的意思是删除功能LogoutFromShare(ip,folder)服务器目录。

我使用的只有这一个qazxsw POI。

© www.soinside.com 2019 - 2024. All rights reserved.