Deleting Windows user accounts remotely WCF and C#

流过昼夜 提交于 2020-01-06 04:31:06

问题


Can I programmatically and remotely create and delete Windows User accounts via WCF (self hosted) and C#? This works locally, but not via WCF... Ideas?

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

回答1:


It should work, as long as the credentials with which the service is running have the appropriate permission to delete the account. If the default credentials in which the service code runs do not have such permission, you may want to look into impersonating the client to do that.




回答2:


I had the some problem connecting to the remote windows with the error Error (0x80004005): Unspecified error. I resolved as follows:

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


来源:https://stackoverflow.com/questions/6431043/deleting-windows-user-accounts-remotely-wcf-and-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!