开发者

Deleting Windows user accounts remotely WCF and C#

开发者 https://www.devze.com 2023-03-14 07:08 出处:网络
Can I programmatically and remotely create and delete Windows User accounts via WCF (self hosted) and C#?

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 syste开发者_运维知识库m");
                }
            }


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消