【问题标题】:Unwilling server: Can't unlock AD account in code不愿意的服务器:无法在代码中解锁 AD 帐户
【发布时间】:2014-10-10 13:01:31
【问题描述】:

当我尝试使用自己的 C# 程序解锁 AD 帐户时,我收到以下错误:

System.DirectoryServices.DirectoryServicesCOMException (0x80072035):服务器不愿意处理请求。

这是我用来解锁帐户的代码:

// "ldap" is an instance of my own class for accessing an LDAP server

using (DirectoryEntry entry = ldap.GetEntry(objectGuid))
{
    entry.InvokeSet("lockouttime", 0);

    // I also tried:
    entry.Properties["lockouttime"].Clear();

    entry.CommitChanges();
}

我在多个域中使用此软件,但只在其中一个域中出现此错误,我不知道有什么区别。当我使用dsa.msc 解锁帐户时,一切正常。

该错误也发生在不同的用户对象上,但两个版本(ClearInvokeSet)都可以在其他环境中工作。谁能给个提示?

P.S.:我使用域管理员凭据来访问 LDAP 服务器。

【问题讨论】:

    标签: c# active-directory ldap directoryservices


    【解决方案1】:

    试试this example:

       public void Unlock(string userDn)
        {
            try
            {
                DirectoryEntry uEntry = new DirectoryEntry(userDn);
                uEntry.Properties["LockOutTime"].Value = 0; //unlock account
    
                uEntry.CommitChanges(); //may not be needed but adding it anyways
    
                uEntry.Close();
            }
            catch (System.DirectoryServices.DirectoryServicesCOMException E)
            {
                //DoSomethingWith --> E.Message.ToString();
    
            }
    }
    

    【讨论】:

    • 这几乎是我的代码(因为InvokeSet 除了设置uEntry.Properties["LockOutTime"].Value = 0 之外什么都不做),因此该代码不起作用 - 至少在上述环境中。
    【解决方案2】:

    我设法使用System.DirectoryServices.AccountManagement 中的类解决了这个问题:

    using (var ctx = new PrincipalContext(
        ContextType.Domain,
        host,
        rootDn,
        ContextOptions.ServerBind | ContextOptions.Negotiate | ContextOptions.SecureSocketLayer,
        username,
        password))
    using (var user = UserPrincipal.FindByIdentity(ctx, IdentityType.Guid, objectGuid.ToString()))
    {
        if (user != null)
        {
            user.UnlockAccount();
        }
        else
        {
            // user not found
        }
    }
    

    但我仍然不知道UnlockAccount 方法除了将lockOutTime 设置为零(或清除它)之外还有什么作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-04
      • 1970-01-01
      • 1970-01-01
      • 2019-03-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-08
      • 1970-01-01
      相关资源
      最近更新 更多