4

現在のユーザーがActiveDirectoryグループのメンバーであるかどうかを確認する必要があります。私は以下のように現在のユーザーを取得することから始めました。ここで、このCurrentUserがアクティブディレクトリグループ「CustomGroup」にあることを確認する方法を知りたいです。

string CurrentUser = WindowsIdentity.GetCurrent().Name;
4

2 に答える 2

13

.NET3.5System.DirectoryServices.AccountManagementクラスを使用できます。詳細については、MSDNの記事「.NETFramework3.5でのディレクトリセキュリティプリンシパルの管理」を参照してください。次のようなものを使用できます。

string CurrentUser = WindowsIdentity.GetCurrent().Name;

PrincipalContext context = new PrincipalContext(ContextType.Domain, "Domain");
UserPrincipal upUser = UserPrincipal.FindByIdentity(context, CurrentUser);
if(upUser != null)
{
    if (upUser.IsMemberOf(context, IdentityType.SamAccountName, "CustomGroup")) 
    {
        // The user belongs to the group
    }
}
于 2012-02-06T04:48:17.787 に答える
1

.NET 3.5または4でこれを試してください:

PrincipalContext infPC = new PrincipalContext(ContextType.Domain, "domain", "login", "password");
UserPrincipal infUP = new UserPrincipal(infPC);
PrincipalSearcher infPS = new PrincipalSearcher();
UserPrincipal foundUP;
GroupPrincipal infGP = new GroupPrincipal(infPC);
GroupPrincipal foundGP;
string CurrentUser = WindowsIdentity.GetCurrent().Name;

infUP.SamAccountName = CurrentUser;
infPS.QueryFilter = infUP;
foundUP = infPS.FindOne();
infGP.Name = "CustomGroup";
infPS.QueryFilter = infGP;
foundGP = infPS.FindOne();
bool ismember = foundUP.IsMemberOf(foundGP);
于 2012-02-05T20:42:31.600 に答える