【问题标题】:Lock AD account c#锁定AD账号c#
【发布时间】:2015-09-25 09:29:08
【问题描述】:


我需要通过 c# 锁定 AD 帐户。这是我的功能

/// <summary>
/// This Methoid will Disable the User Account based on the Directory Entry Object
/// </summary>
/// <param name="oDE">The Directoy Entry Object of the Account to Disable</param>
public void LockAccount(DirectoryEntry oDE)
{
   oDE.InvokeSet("IsAccountLocked", true); 
   //oDE.Properties["userAccountControl"][0] = ADMethods.ADAccountOptions.UF_NORMAL_ACCOUNT | ADMethods.ADAccountOptions.UF_DONT_EXPIRE_PASSWD | ADMethods.ADAccountOptions.UF_ACCOUNT_LOCKOUT;
   //oDE.CommitChanges();
   //oDE.Close();
}

运行它和舞会异常:

System.Reflection.TargetInvocationException:已抛出异常 通过调用的目标。 ---> System.Runtime.InteropServices.COMException:来自 HRESULT 的异常: 0x80005008 --- 内部异常堆栈跟踪结束 --- 在 System.DirectoryServices.DirectoryEntry.InvokeSet(字符串属性名称, 对象[] 参数)

【问题讨论】:

标签: c# active-directory


【解决方案1】:

我假设错误出现在未注释的单个行上?

您是否可能没有锁定用户的权限。您在什么帐户权限下运行?

MSDN says InvokeSet should not be used

This CodeProject link 详细介绍了 Active Directory,禁用帐户的具体代码如下:

public void Disable(string userDn)
{
    try
    {
        DirectoryEntry user = new DirectoryEntry(userDn);
        int val = (int)user.Properties["userAccountControl"].Value;
        user.Properties["userAccountControl"].Value = val | 0x2; 
             //ADS_UF_ACCOUNTDISABLE;

        user.CommitChanges();
        user.Close();
    }
    catch (System.DirectoryServices.DirectoryServicesCOMException E)
    {
        //DoSomethingWith --> E.Message.ToString();    
    }
}

【讨论】:

  • 我正在使用系统管理员权限。我可以远程到 AD 机器并手动锁定此帐户。您的代码是禁用用户,我需要锁定 AD 用户的方法
  • 锁定和禁用几乎没有区别,windows-active-directory.com/… 唯一的问题是锁定可以设置为在预定义的时间限制后解锁,而禁用则保持这种状态。
  • 原始的 CodeProject 链接有一些代码可以锁定帐户,这与您的代码非常相似,但我读过它可能不起作用(我无法从我所在的位置进行测试 - 没有权限) .另一种方法可能是通过重复的编程错误登录尝试来锁定,请参阅sanjivblog.wordpress.com/2011/05/13/…
猜你喜欢
  • 2013-07-13
  • 2018-03-18
  • 1970-01-01
  • 2021-09-06
  • 1970-01-01
  • 1970-01-01
  • 2013-06-26
  • 1970-01-01
  • 2018-07-29
相关资源
最近更新 更多