删除Windows用户帐户远程WCF和C#

问题描述:

我可以编程和远程通过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

只要运行该服务的凭据具有删除该帐户的相应权限,它就应该可以工作。如果服务代码运行的默认凭据没有此权限,则可能需要查看impersonating the client来执行此操作。

+0

卡洛斯,你是对的。有效。我只需要更新客户端上的代码......无论如何,谢谢大家。 – Manolete

我必须连接到与错误错误(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; 
    } 
}