【问题标题】:Validate a SearchResultCollection (active directory)验证 SearchResultCollection(活动目录)
【发布时间】:2017-07-10 19:08:32
【问题描述】:

我正在构建一个具有登录系统但仅适用于管理员的 Web 应用程序,并且它使用 Active Directory 作为凭据,我可以使用此代码检索其信息并且它可以工作,我得到一个包含所有用户的“SearchResultcollection 对象”使用相应的密码和用户名,如果搜索集合不为空,我只需将用户名添加到会话变量中。

代码如下:

DirectoryEntry dir = new DirectoryEntry("ServerAdress", model.username + "@csnavigateurs.qc.ca", model.Password);
DirectorySearcher dirSearch = new DirectorySearcher(dir);
dirSearch.PropertiesToLoad.Add("memberof");
dirSearch.PropertiesToLoad.Add("userPrincipalName");
dirSearch.Filter = "(&(userPrincipalName=" + model.username + "@csnavigateurs.qc.ca))";

SearchResultCollection result;
 try
 {
     result = dirSearch.FindAll();
 }
 catch (DirectoryServicesCOMException)
 {
   ModelState.AddModelError("", "wrong username or password.");
    return View(model);
 }
 if () /*** Im trying to find the condition that goes here ***/
 {
    Session["utilisateur"] = model.username;
 }                 
   return RedirectToAction("AdminPage", "Admin");

但我的问题是我不知道如何检查此集合是否有 1 个或多个用户,我尝试检查对象方法并查看是否有任何方法可以像“list.any()”一样工作,但是我找不到一个,我不确定如果对象没有找到具有这些凭据的任何人或只是空的对象是否将为空,我无法调试以检查。

【问题讨论】:

    标签: c# asp.net-mvc active-directory


    【解决方案1】:

    你可以试试这个if(result != null && result.Count!=0)

    【讨论】:

    • 就像我说的 SearchResultCollection 没有 .any() 方法
    • ok 你可以试试这个 if(result!= null && result.Count!=0),我可以看到有 count 方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多