0

我有一个问题,我没有找到解决方案:(

我有一个从 Active Directory 获取数据的 ASP.NET 应用程序。我构建了一个 3 层应用程序。我有一个模型项目,它有一个用户类(“Benutzer.cs”)和一个数据层和服务层。在我的数据层中,我获得了有关 AD 中用户的信息。我现在只想获得一个用户,我的问题是如何过滤列表中的一个用户。也许是一个愚蠢的问题,但我不知道我必须做什么:D

我的代码:

...

                    IUserService srv = new UserService();
                    List<Model.Benutzer> usr = srv.GetUser(Domain, null, null, UserID); 

                        // I want the one User of this List can I found this User about
                        //  Benutzer User = usr.? 

                    ...

我只想要一个用户。这是我的 getUser 方法的代码:

public List<Benutzer> GetUser(string Domain, string Benutzer, string Werk, string GUID)
        {
            List<Benutzer> result = new List<Benutzer>();

                DirectoryEntry Entry = new DirectoryEntry("LDAP://<GUID=" + GUID + ">");

                string filter = "(&(objectClass=user)(objectCategory=person)(cn=*))"; 

                DirectorySearcher Searcher = new DirectorySearcher(Entry, filter);

                   SearchResult res = Searcher.FindOne();

                    result.Add(new Benutzer()
                    {
                        Benutzername = GetProperty(res, "sAMAccountName"),
                        Vorname = GetProperty(res, "givenName"),
                        Nachname = GetProperty(res, "sn"),
                        eMail = GetProperty(res, "mail"),
                        Unternehmen = GetProperty(res, "company"),
                        Abteilung = GetProperty(res, "Department"),
                        Raum = GetProperty(res, "physicalDeliveryOfficeName"),
                        Beschreibung = GetProperty(res, "Description"),
                        Kostenstelle = GetProperty(res, "extensionAttribute3"),
                        Telefonnummer = GetProperty(res, "telephoneNumber"),
                        Mobilnummer = GetProperty(res, "mobile"),
                        Haustelefon = GetProperty(res, "homePhone"),
                        Fax = GetProperty(res, "facsimileTelephoneNumber"),
                        Pager = GetProperty(res, "pager"),
                        Standort = GetProperty(res, "l")
                    });

            return result; 
        }

塔拉索夫

4

2 回答 2

1

如果您只关注一个用户,则更新您的GetUser方法以返回单个用户实例而不是列表:

public Benutzer GetUser(...)
{
    ....
    return new Benutzer(...);
}

如果您无法控制该GetUser方法并且只想从返回的列表中提取用户,则调用:

srv.GetUser(Domain, null, null, UserID).FirstOrDefault();
于 2012-07-26T13:53:02.053 回答
1

您的代码应该只返回列表中的一个用户。DirectorySearcher.FindOne 仅返回找到的第一个条目。

如果您不想在列表中返回它,请将返回值设为 Benutzer,然后结果也应为 Benutzer 类型:

public Benutzer GetUser(string Domain, string Benutzer, string Werk, string GUID)
{
    Benutzer result = new Benutzer();
    DirectoryEntry Entry = new DirectoryEntry("LDAP://<GUID=" + GUID + ">");
    string filter = "(&(objectClass=user)(objectCategory=person)(cn=*))";

    DirectorySearcher Searcher = new DirectorySearcher(Entry, filter);
    SearchResult res = Searcher.FindOne();
    result =new Benutzer()
    {
        Benutzername = GetProperty(res, "sAMAccountName"),
        Vorname = GetProperty(res, "givenName"),
        Nachname = GetProperty(res, "sn"),
        eMail = GetProperty(res, "mail"),
        Unternehmen = GetProperty(res, "company"),
        Abteilung = GetProperty(res, "Department"),
        Raum = GetProperty(res, "physicalDeliveryOfficeName"),
        Beschreibung = GetProperty(res, "Description"),
        Kostenstelle = GetProperty(res, "extensionAttribute3"),
        Telefonnummer = GetProperty(res, "telephoneNumber"),
        Mobilnummer = GetProperty(res, "mobile"),
        Haustelefon = GetProperty(res, "homePhone"),
        Fax = GetProperty(res, "facsimileTelephoneNumber"),
        Pager = GetProperty(res, "pager"),
        Standort = GetProperty(res, "l")
    };              

    return result;  
}
于 2012-07-26T13:59:08.493 回答