2013-03-01 140 views
1

我開發了C#windows應用程序。操作系統是Windows 7防止WNetAddConnection2類允許被禁止的用戶訪問共享文件夾

要求:是使用代碼使用WNetAddConnection2類使用憑據訪問網絡共享文件夾'測試'。

限制:是一些用戶有權訪問此共享文件夾「測試」,但對於其他用戶,設置了「拒絕」共享權限。

代碼WNetAddConnection2驗證錯誤的用戶名/密碼,它會給我錯誤。

例如

「用戶A」從LAN試圖訪問共享文件夾「測試」使用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 
} 
+1

如果您使用特定的用戶名和密碼連接到共享,那麼登錄到本地計算機的用戶帳戶無關緊要。如果您想根據誰登錄到本地計算機進行訪問,請不要在對WNetAddConnection2的調用中提供用戶名和密碼。 – 2013-03-02 02:46:23

+0

@Harry現在,我沒有通過代碼傳遞憑證,WNetAddConnection2提示我和共享文件夾連接成功,當我登錄爲'用戶A'而沒有訪問時,WNetAddConnection2允許與'用戶A'的憑證連接。 – 2013-03-02 05:27:34

+0

你在嘗試使用哪種操作系統? – 2013-03-02 07:05:27

回答

-2

我有同樣的問題,當我用IIS 7.5中的部署我的C#項目,但它是驚人的,當我做刪除我的代碼的註銷手續。 。 。

我的意思是刪除功能LogoutFromShare(ip,folder)服務器目錄。

我使用ony這一個LoginToOtherPC(ip, usr, pwd, folder)

0

在設計上,連接到共享需要訪問的份額 - 它需要訪問該共享的根目錄。

通過運行框打開共享將打開共享的根目錄,因此它至少需要讀取目錄以及共享的訪問權限。 WNetAddConnection2()API相比較而言只需要訪問共享。

它必須以這種方式工作,因爲有時需要讓某人只能訪問某些子目錄,而不能訪問根目錄。如果連接到需要訪問根目錄的共享,這是不可能的。

連接到共享後,您可以通過嘗試枚舉文件來測試對根目錄的訪問權限。如果您獲得拒絕訪問的例外,則該用戶無權訪問。

相關問題