1

私は午前中ずっとこの問題を解決しようとして以前の投稿を調べてきましたが、どれもうまくいかないようです。

私はコース管理者のためにADのユーザー管理インターフェースを修正してきました。ソリューションは開発サーバーで正常に機能しますが、必要なものだけを表示するという考えであり、本番環境で上記のエラーが発生します。

HostingEnvironment.Impersonate、サービス アカウントをドメイン管理者に昇格させるなど、見つけられるすべてのことを試しましたが、うまくいきませんでした。

public static List<GroupPrincipal> GetGroups(string client)
{
        List<GroupPrincipal> List = new List<GroupPrincipal>();

        DirectoryEntry ou = null;
        GroupPrincipal group = null;
        PrincipalContext context = null;

        if (domain.Path.ToLower().Contains(DevDN.ToLower()))
        {
            context = new PrincipalContext(ContextType.Domain,
                DevDom,
                DevDN,
                DevService,
                DevServicePass);
        }
        else
        {
            context = new PrincipalContext(
                ContextType.Domain,
                LiveDom,
                LiveDN,
                LiveService,
                LiveServicePass);
        }

        DirectorySearcher searcher = new DirectorySearcher(domain, "(&(ou=" + client + ")(objectClass=organizationalUnit))");
        try
        {
            ou = new DirectoryEntry(searcher.FindOne().Path);
        }
        catch (System.Exception ex)
        {
            Log.WriteError("SUGM.ADLink.GetGroups", "Unable to locate client: " + ex.Message);
            List = null;
            return List;
        }
        try
        {
            foreach (DirectoryEntry groups in ou.Children)
            {
                if (groups.SchemaClassName == "group")
                {
                    string name = groups.Name.Replace("CN=", "");
                    group = GroupPrincipal.FindByIdentity(context, name);
                    List.Add(group);
                }
            }
        }
        catch (System.Exception ex)
        {
            Log.WriteError("SUGM.ADLink.GetGroups", "Unable to add groups to list: " + ex.Message);
            List = null;
            return List;
        }

        return List;
    }

デバッグ中にチェックがあり、すべての正しい値が渡されていますが、常に foreach ブロックで失敗します。

誰かが私が間違っていることを指摘できますか。

乾杯

4

1 に答える 1

0

System.DirectoryServicesと名前空間を混在させることは避けるべきですSystem.DirectoryServices.AccountManagement。これはあまり良い戦略ではありません。

S.DS.AM (.NET 3.5) でもやりたいことはすべてできます。また、はるかに簡単です。

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

// create your domain context and specify the initial container to work from
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN", "OU=YourStartingPoint,DC=YourCompany,DC=com");

// define a "query-by-example" principal - here, we search for a GroupPrincipal 
GroupPrincipal qbeGroup = new GroupPrincipal(ctx);

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

// 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 ドキュメントを参照してください。

于 2012-07-06T12:35:50.687 に答える