3

C# を介してプログラムでリモート サーバー上のローカル グループのメンバーシップを取得する方法を誰かが知っているかどうか疑問に思いました。これには管理者権限が必要ですか? もしそうなら、これらのグループの現在ログインしているユーザーのメンバーシップ (またはメンバーでない) を確認する方法はありますか?

4

6 に答える 6

4

ハウツー: (ほぼ) C# を介した Active Directory のすべてが非常に役立ち、グループ内の AD メンバーを反復処理する方法についての説明も含まれています。

public ArrayList Groups(string userDn, bool recursive)
{
    ArrayList groupMemberships = new ArrayList();
    return AttributeValuesMultiString("memberOf", userDn,
        groupMemberships, recursive);
}

次の関数も必要になります。

public ArrayList AttributeValuesMultiString(string attributeName,
     string objectDn, ArrayList valuesCollection, bool recursive)
{
    DirectoryEntry ent = new DirectoryEntry(objectDn);
    PropertyValueCollection ValueCollection = ent.Properties[attributeName];
    IEnumerator en = ValueCollection.GetEnumerator();

    while (en.MoveNext())
    {
        if (en.Current != null)
        {
            if (!valuesCollection.Contains(en.Current.ToString()))
            {
                valuesCollection.Add(en.Current.ToString());
                if (recursive)
                {
                    AttributeValuesMultiString(attributeName, "LDAP://" +
                    en.Current.ToString(), valuesCollection, true);
                }
            }
        }
    }
    ent.Close();
    ent.Dispose();
    return valuesCollection;
}

この AD メソッドを使用したい場合は、この記事の情報を使用できますが、アンマネージ コードが使用されます。

http://www.codeproject.com/KB/cs/groupandmembers.aspx

彼らが作成したサンプル アプリケーション:

代替テキスト

于 2008-09-05T09:20:15.790 に答える
2

System.DirectoryServicesよりもクリーンな実装を提供するSystem.DirectoryServices.AccountManagementと呼ばれる新しいアセンブリが.net3.5にあるようです。Dominick Baierは、グループのメンバーシップの確認など、いくつかの簡単な操作についてブログに書いています。-

public static bool IsUserInGroup(string username, string groupname, ContextType type)
{
    PrincipalContext context = new PrincipalContext(type);

    UserPrincipal user = UserPrincipal.FindByIdentity(
        context,
        IdentityType.SamAccountName,
        username);
    GroupPrincipal group = GroupPrincipal.FindByIdentity(
        context, groupname);

    return user.IsMemberOf(group);
}

私はこのアプローチを使用すると思いますが、提案に感謝します!:-)

于 2008-09-05T10:38:07.087 に答える
0

これはおそらく役立つかもしれません。Active Directoryに対して認証するアプリを開発し、ユーザーが所属するグループ文字列も調べる必要がありました。

いくつかの理由から、Windows認証を使用するのではなく、独自のフォームベースの認証を使用します。最初にユーザーを認証し、次にユーザーが属するすべてのグループを調べるために、以下のルーチンを開発しました。おそらくそれは役立つかもしれません。このルーチンは、LogonUserを使用して認証を行い、そのユーザーの数値のGUIDのようなグループID(SID)のリストを取得して、それぞれを人間が読める形式に変換します。

これが役立つことを願って、私はさまざまな異なるグーグル検索からこのアプローチを統合しなければなりませんでした。

private int validateUserActiveDirectory()
{
    IntPtr token = IntPtr.Zero;
    int DBgroupLevel = 0;

    // make sure you're yourself -- recommended at msdn http://support.microsoft.com/kb/248187
    RevertToSelf();

    if (LogonUser(txtUserName.Value, propDomain, txtUserPass.Text, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, token) != 0) {
        // ImpersonateLoggedOnUser not required for us -- we are not doing impersonated stuff, but leave it here for completeness.
        //ImpersonateLoggedOnUser(token);
        // do impersonated stuff
        // end impersonated stuff

        // ensure that we are the original user
        CloseHandle(token);
        RevertToSelf();

        System.Security.Principal.IdentityReferenceCollection groups = Context.Request.LogonUserIdentity.Groups;
        IdentityReference translatedGroup = default(IdentityReference);

        foreach (IdentityReference g in groups) {
            translatedGroup = g.Translate(typeof(NTAccount));
            if (translatedGroup.Value.ToLower().Contains("desired group")) {
                inDBGroup = true;
                return 1;
            }
        }
    }
    else {
        return 0;
    }
}
于 2008-11-27T06:23:54.770 に答える
0

同様の質問をしたところ、WMIを使用してグループメンバーを列挙する回答を書くことになりました。system.directoryservices.accountmanagementのものでの認証に実際の問題がありました。もちろん、YMMV。

于 2008-09-07T14:19:56.643 に答える
0

System.DirectoryServices.AccountManagementが完全に管理されているかどうか知りたいです。私は、COM相互運用機能のラッパーであるSystem.DirectoryServices.ActiveDirectoryを使用しましたが、これは多くの頭痛の種になりました...

于 2008-09-07T14:28:04.910 に答える
0

おそらく、これは WMI 経由で実行できるものでしょうか?

于 2008-09-05T08:44:17.920 に答える