0

以下は私のコードです。姓または名が同じグリッド内のすべてのユーザーを取得したいと考えています。しかし、ここではソートされたグリッド内の名前自体を取得しています。

ユーザー名を入力する選択肢をユーザーに与えています。彼が名前を入力すると、Active Directory で検索して、ユーザーが入力したテキストで始まるすべてのユーザーを返すことができるはずです。

すべての可能性を表示できる必要があります。たとえば、ユーザーが入力した場合、表示adamするかどうかを選択する選択肢を与える必要がありadam josefますadam john

どんな提案も役に立ちます。

ここにコードがあります

       DirectoryEntry de = new DirectoryEntry("ADConnection");

        DirectorySearcher deSearch = new DirectorySearcher(de);

        //set the search filter    
        deSearch.SearchRoot = de;
        String UserName = txt_To.Text;
        deSearch.Filter = "(&(objectCategory=user)(GivenName=*" + UserName + "*))";
        string[] arrPropertiesToLoad = { "sn" };
        deSearch.PropertiesToLoad.AddRange(arrPropertiesToLoad);

      SearchResultCollection sResultColl = deSearch.FindAll();//Getting undefined error



        Gridview1.DataSource = sResultColl ;
        Gridview1.DataBind();

ここにスタックトレースがあります

[COMException (0x80004005): 不明なエラー]
System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +439513
System.DirectoryServices.DirectoryEntry.Bind() +36
System.DirectoryServices.DirectoryEntry.get_AdsObject() +31
System.DirectoryServices.DirectorySearcher. FindAll(Boolean findMoreThanOne) +78
System.DirectoryServices.DirectorySearcher.FindAll() +9
C:\Users\273714\documents\visual studio 2010\Projects\Certificate\Certificate\ の Certificate.WebForm4.btngo0_Click(オブジェクト送信者、EventArgs e) WebForm4.aspx.cs:202
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page .RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(ブール値 includeStagesBeforeAsyncPoint、ブール値 includeStagesAfterAsyncPoint) +5563

4

2 に答える 2

2

PrincipalSearcherおよび「例によるクエリ」プリンシパルを使用して検索を行うことができます。

// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// define a "query-by-example" principal - here, we search for a UserPrincipal 
// and with the first name (GivenName) of "Bruce" and a last name (Surname) of "Miller"
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = "*" + UserName + "*";

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

// find all matches
foreach(var found in srch.FindAll())
{
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
}

まだお読みでない場合は、.NET Framework 3.5でディレクトリ セキュリティ プリンシパルを管理するという MSDN の記事を必ずお読みくださいSystem.DirectoryServices.AccountManagement。または、System.DirectoryServices.AccountManagement 名前空間に関する MSDN ドキュメントを参照してください。

もちろん、必要に応じて、作成した「例によるクエリ」ユーザー プリンシパルに他のプロパティを指定することもできます。

  • DisplayName(通常: 名 + スペース + 姓)
  • SAM Account Name- Windows/AD アカウント名
  • User Principal Name- "username@yourcompany.com" スタイル名

の任意のプロパティを指定し、UserPrincipalそれらを の「例によるクエリ」として使用できますPrincipalSearcher

更新:多数のユーザーを見つけてそれらをグリッドビューにバインドする場合は、次のコードを使用します。

// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// define a "query-by-example" principal - here, we search for a UserPrincipal 
// and with the first name (GivenName) of "Bruce" and a last name (Surname) of "Miller"
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = "*" + UserName + "*";

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

var results = srch.FindAll();

Gridview1.DataSource = results; 
Gridview1.DataBind();

返されたすべてのデータに対して 2 回反復しないでください。(あなたのコメントのように).....

更新 #2: S.DS.AM クラスを使用すると、常に完全なクラスを取得できます。これについては何もできません。特定の LDAP 属性のみを選択する場合は、独自のアプローチを使用する必要があります。

そのアプローチから、特定のユーザーのような個々のADオブジェクトではなく、DirectorySearcherコンテナー(コンテナーなど)に のルートを作成するようにする必要があります。OU=Users

したがって、次のコードを使用してみてください。

// define a *CONTAINER* as the root of your searcher!
DirectoryEntry de = new DirectoryEntry("LDAP://OU=Users,OU=NJY,OU=NewJersey,OU=USA,OU=NorthAmerica,OU=America,OU=gunt,DC=xxx,DC=com");

DirectorySearcher deSearch = new DirectorySearcher(de);

// set the search filter    
string UserName = txt_To.Text;

deSearch.Filter = string.Format("(&(objectCategory=user)(givenName=*{0}*))", UserName);
deSearch.PropertiesToLoad.Add("sn");

SearchResultCollection sResultColl = deSearch.FindAll();

Gridview1.DataSource = sResultColl;
Gridview1.DataBind();

それは動作しますか?

于 2012-11-08T08:46:50.520 に答える