【问题标题】:Deleting Windows user accounts remotely WCF and C#远程删除 Windows 用户帐户 WCF 和 C#
【发布时间】:2011-06-21 19:40:17
【问题描述】:

我可以通过 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");
                }
            }

【问题讨论】:

  • 我认为值得注意的是 DirectoryEntry 是 IDisposable 并且将它包装在 using 中可能是一个好主意。 using(DirectoryEntry user = users.Find(usernameAccount)) { users.Remove(user); }

标签: c# windows wcf windows-services


【解决方案1】:

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

【讨论】:

  • 卡洛斯,你是对的。有效。我只需要更新客户端上的代码...还是谢谢大家。
【解决方案2】:

我在连接到远程窗口时遇到了一些问题,错误为 Error (0x80004005): Unspecified error。我解决如下:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    相关资源
    最近更新 更多