【问题标题】:How to access remote machine to add/remove/manage users accounts?如何访问远程机器以添加/删除/管理用户帐户?
【发布时间】:2013-05-17 13:12:37
【问题描述】:

我有一个场景,我需要访问远程计算机以编程方式添加和删除 Windows 用户帐户。 远程机器是我需要远程配置的“备用工作站”,以防万一需要更换主工作站 - 所以这里没有安全绕过或恶意软件:)

我知道远程计算机管理员的用户/密码,并且我能够使用 WMI Win32_UserAccount 检索现有用户帐户的完整列表。现在,我正在尝试为每个用户获取一个 UserPrincipal 对象(最终将其删除),但我的所有尝试都遇到了异常。

  • 尝试 #1:

    PrincipalContext context = new PrincipalContext(ContextType.Domain, "xxx.xxx.xxx.xxx" /*remote IP Address*/);
    UserPrincipal user = (UserPrincipal.FindByIdentity(context, "userName"));
    // Do something with user, like user.Delete();
    

    在这种情况下,我总是在第一行得到一个异常:

    System.DirectoryServices.AccountManagement.PrincipalServerDownException 被捕获 消息=无法联系服务器。
    源 = System.DirectoryServices.AccountManagement StackTrace: 在 System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(字符串 服务器名称、服务器属性和属性) 在 System.DirectoryServices.AccountManagement.PrincipalContext.DoServerVerifyAndPropRetrieval() 在 System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType,字符串名称,字符串容器,ContextOptions 选项, 字符串用户名,字符串密码) 在 System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType、字符串名称、字符串容器、字符串用户名、字符串 密码)内部异常: System.DirectoryServices.Protocols.LdapException Message=LDAP 服务器不可用。 Source=System.DirectoryServices.Protocols 错误代码=81 堆栈跟踪: 在 System.DirectoryServices.Protocols.LdapConnection.Connect() 在 System.DirectoryServices.Protocols.LdapConnection.SendRequestHelper(DirectoryRequest 请求,Int32 和消息 ID) 在 System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest 请求,时间跨度请求超时) 在 System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest 要求) 在 System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(字符串 服务器名称、服务器属性和属性) 内部异常:

  • 尝试 #2:

    PrincipalContext context = new PrincipalContext(ContextType.Machine, "xxx.xxx.xxx.xxx" /*remote IP Address*/);
    UserPrincipal user = (UserPrincipal.FindByIdentity(context, "userName"));
    // Do something with user, like user.Delete();
    

    在这种情况下,我总是在第二行得到一个异常:

    System.IO.FileNotFoundException 被捕获 Message=Network path not 找到了。

    Source=Active Directory 堆栈跟踪: 在 System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.GetInfo() 在 System.DirectoryServices.DirectoryEntry.RefreshCache() 在 System.DirectoryServices.AccountManagement.PrincipalContext.DoMachineInit() 在 System.DirectoryServices.AccountManagement.PrincipalContext.Initialize() 在 System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx() 在 System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext 上下文,类型 principalType,Nullable`1 身份类型,字符串 身份值,日期时间参考日期) 在 System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext 上下文,类型 principalType,字符串 identityValue) 在 System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext 上下文,字符串标识值) 内部异常:

我尝试了 PrincipalContext 对象的不同签名(使用域名而不是 IP 地址,使用用户名和密码,...),但我总是在两次尝试中都遇到异常。

我是否遗漏了一些说明?在创建 PrincipalContext 对象之前,我是否需要使用模拟来获得对远程机器的完全访问权限? 还有其他方法可以完成我想做的事情吗? (即访问远程机器添加/删除Windows帐户)

【问题讨论】:

    标签: c# active-directory impersonation remote-access principal


    【解决方案1】:

    如果您只是想删除帐户,为什么不改用目录服务:

    using System.DirectoryServices;
    
    DirectoryEntry deParent = new DirectoryEntry("WinNT://[computername]", 
             @"[domain\adminname", "password");            
    DirectoryEntry deToRemove = deParent.Children.Find("usernametoremove");
    deParent.Children.Remove(deToRemove);
    

    【讨论】:

    • 非常感谢!我没想到DirectoryServices 删除帐户。你知道我是否还可以通过DirectoryServices 课程以编程方式创建新帐户并更改用户设置?无论如何,我会深入了解这门课,谢谢。
    • 当然可以。 MSDN 中的 DirecoryEntry 页面将显示您需要执行此操作的所有方法。不要忘记“提交”您的更改! msdn.microsoft.com/en-us/library/…。另外,如果这回答了您的问题,请打勾
    • 是的,谢谢,我想这回答了我的问题。我如此专注于 System.DirectoryServices.AccountManagement 命名空间,主要是因为引入了 the related MSDN article ,以至于我没有考虑长期存在的 DirectoryEntry 类。
    【解决方案2】:

    如果您在域下,请删除 IP 地址参数并尝试使用它。

    using(var pc = new PrincipalContext(ContextType.Domain))
    {
      using(var up = new UserPrincipal(pc))
      {
       //Do some stuff
      }
    }
    

    似乎无法使用 IP 地址找到您的 Active Directory

    【讨论】:

    • 嘿,谢谢!使用您的示例代码我没有得到任何异常,但是 pcup 的所有属性都设置为 null,所以我可以t 对用户对象进行任何操作。其他问题:如果我不指定 IP 地址,代码如何“理解”我所指的远程机器?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    • 1970-01-01
    • 2015-05-22
    • 2017-09-20
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    相关资源
    最近更新 更多