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

时间:2011-06-21 19:40:18

标签: c# windows wcf windows-services

我是否可以通过WCF(自托管)和C#以编程方式远程创建和删除Windows用户帐户? 这在本地工作,但不是通过WCF ...想法?

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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

我遇到了一些问题,连接到远程窗口时出现错误错误(0x80004005):未指定错误。我解决如下:

//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;
    }
}