0

なぜこれが起こるのかわかりませんが、このコードを実行すると、あるサーバーでは機能しますが、別のサーバーでは機能しません。

両方のサーバーが正しい found.DisplayName を返しますが、1 つのサーバーのみが oUserPrincipal の値を返し、もう 1 つのサーバーは null 値を返します。

エラー行:

       UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(ctx, found.DisplayName) returns null


        dynamic config = _getExpandoFromXml("config.xml");

        PrincipalContext ctx = new PrincipalContext(ContextType.Domain, config.activeDirectory.sDomain, config.activeDirectory.sDefaultOU,config.mailServer.user, config.mailServer.pass);
        UserPrincipal user = new UserPrincipal(ctx);

        PrincipalSearcher search = new PrincipalSearcher(user);
        Console.WriteLine("before foreach");
        foreach (Principal found in search.FindAll())
        {
            try{
                if (found.DisplayName == null)
                {
                    Console.WriteLine("found.Dispalyname is null");
                }
                else
                {    
                    Console.Write("Dispalyname: ");
                    Console.WriteLine(found.DisplayName);
                }
                UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(ctx, found.DisplayName);
                Console.Write("looking for user: ");
                Console.WriteLine(found.DisplayName);
                Console.WriteLine("after findbyidentiy");
                if (oUserPrincipal == null)
                {
                    Console.WriteLine("oUserPrinciapal is null");
                }
                if (oUserPrincipal.LastPasswordSet == null)
                {
                    Console.WriteLine("lastpasswordset is null");
                }
                DateTime? dateOrNull = oUserPrincipal.LastPasswordSet;
                Console.WriteLine("after LastPasswordSet");
4

2 に答える 2

1

FindByIdentity は、少数のプロパティのみを検索できます。これらは、「IdentityType列挙に含まれる任意の形式」です。

Name は有効なオプションですが、DisplayName はリストされていないため、おそらく DisplayName と Name が同じである場合に結果が得られ、それ以外の場合は失敗します。

使用:

var oUserPrincipal = UserPrincipal.FindByIdentity(ctx, found.Name);

また

var oUserPrincipal = UserPrincipal.FindByIdentity(ctx, found.SamAccountName);

動作するはずです。

検索するプロパティを指定できるFindByIdentityの 3 つのパラメータ バージョンもあります。

于 2013-03-13T22:19:50.977 に答える