2011-06-21 51 views
0

我可以編程和遠程通過WCF(個體經營託管)和C#創建和刪除Windows用戶帳戶? 這適用於本地,但不通過WCF ...想法?刪除Windows用戶帳戶遠程WCF和C#

  DirectoryEntry localDirectory = new DirectoryEntry("WinNT://" + Environment.MachineName.ToString()); 
      DirectoryEntries users = localDirectory.Children; 
      try 
       { 
        DirectoryEntry user = users.Find(usernameAccount); 
        users.Remove(user); 

       }catch(SystemException) 
       { 
        System.Console.WriteLine("Error: User account not found in the system"); 
       } 
      } 
+0

我認爲值得注意的是DirectoryEntry是IDisposable,它可能是一個好主意,它包裝在一個使用。 使用(用戶的DirectoryEntry = users.Find(usernameAccount)) { users.Remove(用戶); } – Felan

回答

3

只要運行該服務的憑據具有刪除該帳戶的相應權限,它就應該可以工作。如果服務代碼運行的默認憑據沒有此權限,則可能需要查看impersonating the client來執行此操作。

+0

卡洛斯,你是對的。有效。我只需要更新客戶端上的代碼......無論如何,謝謝大家。 – Manolete

0

我必須連接到與錯誤錯誤(0X80004005)遠程Windows的一些問題:未指定的錯誤。我解決如下:

//Define path 
//This path uses the full path of user authentication 
String path = string.Format("WinNT://{0}/{1},user", server_address, username); 
DirectoryEntry deBase = null; 
try 
{ 
    //Try to connect with secure connection 
    deBase = new DirectoryEntry(_ldapBase, _username, _passwd, AuthenticationTypes.Secure); 

    //Connection test 
    //After test define the deBase with the parent of user (root container) 
    object nativeObject = _deRoot.NativeObject; 
    _deRoot = _deRoot.Parent; 

} 
catch (Exception ex) 
{ 
    //If an error occurred try without Secure Connection 
    try 
    { 
     _deRoot = new DirectoryEntry(_ldapBase, _username, _passwd); 

     //Connection test 
     //After test define the deBase with the parent of user (root container) 
     object nativeObject = _deRoot.NativeObject; 
     _deRoot = _deRoot.Parent; 
     nativeObject = _deRoot.NativeObject; 

    } 
    catch (Exception ex2) 
    { 
     //If an error occurred throw the error 
     throw ex2; 
    } 
}