現在のユーザーがActiveDirectoryグループのメンバーであるかどうかを確認する必要があります。私は以下のように現在のユーザーを取得することから始めました。ここで、このCurrentUserがアクティブディレクトリグループ「CustomGroup」にあることを確認する方法を知りたいです。
string CurrentUser = WindowsIdentity.GetCurrent().Name;
現在のユーザーがActiveDirectoryグループのメンバーであるかどうかを確認する必要があります。私は以下のように現在のユーザーを取得することから始めました。ここで、このCurrentUserがアクティブディレクトリグループ「CustomGroup」にあることを確認する方法を知りたいです。
string CurrentUser = WindowsIdentity.GetCurrent().Name;
.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
}
}
.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);