代码之家  ›  专栏  ›  技术社区  ›  Bh00shan

针对活动目录的身份验证。。董事搜寻者的必要性

  •  0
  • Bh00shan  · 技术社区  · 8 年前

    我有下面的代码来根据Active Directory验证给定的用户名和密码

    DirectoryEntry entryInAbsg = new DirectoryEntry("LDAP://....../DC=....,DC=..", username, password,AuthenticationTypes.Secure);
                object nativeObject = entryInAbsg.NativeObject;
                authenticated = true;
    

    这足以验证用户吗?是否需要使用DirectorySearcher再次搜索用户? 上面的用户名和密码应该是管理员帐户吗?

    1 回复  |  直到 8 年前
        1
  •  0
  •   Dan K    8 年前

    这足以验证用户。但是要执行搜索,您需要创建directorysacher的实例,并将DirectoryEntry对象(entryinAbsg)和您可能需要的任何筛选器传递给构造函数。考虑下面C#中的例子;

    private DirectorySeacher GetDirectorySeacher(DirectoryEntry entryInAbsg)
    {
          DirectorySearcher dirSeach = null;
          if(dirSearch == null)
          {
              try
              {
                  dirSearch = new DirectorySeacher(entryInAbsg)
              }
              catch (DirectoryServicesCOMException e)
              {
                  MessageBox.Show("Authentification Failed!!!, Please Check.", "Error Info", MessageBoxButtons.OK,MessageBoxIcon.Error);
              }
              return dirSearch;
         }
         else
         {
              return dirSearch;
         }
    }
    

    你可以根据你的具体需要定制。