【问题标题】:Check if Active Directory Account is Locked out (WPF C#)检查 Active Directory 帐户是否被锁定(WPF C#)
【发布时间】:2012-10-20 20:41:34
【问题描述】:

大家好(这是我的第一篇文章) 我有一些从 Codeplex http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C 提取的简单 AD 代码,我能够从所述代码中获取所有最终用户的信息。现在,我一直在搜索和搜索,从这里和网络上发现了一些有趣的代码 sn-ps 关于“用户被锁定了吗?”

我想使用我已经使用了 2 年的代码,并在其中添加更多内容以添加到锁定部分...如果有一个文本框,我会很高兴给了我我的信息,或者一个复选框,或者只是说“用户锁定”的东西,然后我会通知我的 Exchange 团队并让用户解锁......

我的代码如下:

string eid = this.tbEID.Text;
string user = this.tbUserName.Text.ToString();
string path = "PP://dc=ds,dc=SorryCantTellYou,dc=com";

DirectoryEntry de = new DirectoryEntry(path);

DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = "(&(objectCategory=person)(sAMAccountName=" + eid + "))";

SearchResultCollection src = ds.FindAll();

//AD results
if (src.Count > 0)
{
   if (src[0].Properties.Contains("displayName"))
   {
      this.tbUserName.Text = src[0].Properties["displayName"][0].ToString();
   }
}

所以,如果我能弄清楚如何使用相同的目录条目,并让搜索器向我显示帐户锁定状态,那将是惊人的......请协助

【问题讨论】:

标签: c# wpf active-directory


【解决方案1】:

如果您使用的是 .NET 3.5 及更高版本,则应查看 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在此处阅读所有相关信息:

基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SamAccountName");

if(user != null)
{
    string displayName = user.DisplayName;

    if(user.IsAccountLockedOut())
    {       
        // do something here....    

    }
}

新的 S.DS.AM 让在 AD 中与用户和组一起玩变得非常容易!

【讨论】:

  • 好的,我喜欢这样......所以如果我已经有了“用户名”或 SamAccountName(所有这些都是我在那里的 EID)我如何将你的代码绑定我有什么?
  • @user1762132:只需将eid 作为您的第二个参数传递给.FindByIdentity(ctx, eid) 就可以了!
  • 好吧,我就是这么做的……所以我不必再输入那些疯狂的 OU 和 DC 代码了吗?这就是我所拥有的......如果你觉得它很酷,请告诉我......谢谢你回答这个问题:)
  • @user1762132:PrincipalContext 仍然可以在特定域和该域内的特定(子)容器中创建 - 使用 crazy OU 和 DC 代码: -) - 但默认情况下,它只会选择您连接的当前域并使用它。
  • @user1762132: 使用 PrincipalContext ctx1 = new PrincipalContext(ContextType.Domain, "DOMAIN1"); 等等 - 您可以使用 PrincipalContext 的重载构造函数来定义用于上下文的域
猜你喜欢
  • 2010-11-26
  • 2012-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多