【问题标题】:help with finding user using directory service account management帮助使用目录服务帐户管理查找用户
【发布时间】:2011-04-04 13:57:54
【问题描述】:

我在尝试通过参数 (userName) 将值传递给方法时遇到问题。如果我对值进行硬编码,它将找到用户。

任何指导将不胜感激,

protected void btnSubmit_Click(object sender, EventArgs e)
{
   if (!String.IsNullOrEmpty(txtUserName.Text))
   {
      string userName = txtUserName.ToString();

      PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "car2.local", "DC=car2,DC=local");
      UserPrincipal usr = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, userName);

      if(usr != null)
      {
         lblStatus.Text = "user exists";
      }
      else
      {
         lblStatus.Text = "user does not exists";
      }
   }
}

【问题讨论】:

  • 我正在键入一个与 SAM 帐户名称匹配的值。如果我这样做(ctx,IdentityType.SamAccountName,“jason”)它将找到用户
  • 尝试改用string userName = txtUserName.Text.Trim(); - 这会改变什么吗??

标签: c# active-directory directoryservices


【解决方案1】:

试试这个 - 读出文本框的 .Text 属性(并调用 .Trim() 以消除任何额外的、多余的空格),而不是在其上使用 .ToString()

string userName = txtUserName.Text.Trim();

UserPrincipal usr = UserPrincipal.FindByIdentity(ctx, userName);

如果您不指定要搜索的身份类型 - 那么 AD 将搜索最常见的身份类型并希望找到您的用户!

【讨论】:

  • 感谢您的帮助!我的问题是使用 txt.UserName.ToString();一旦我将其更改为 txt.UserName.Text.Trim();它开始工作了
猜你喜欢
  • 2017-04-29
  • 2021-06-23
  • 2016-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-31
  • 1970-01-01
  • 2016-08-05
相关资源
最近更新 更多