2

以下のコードを使用して、特定のユーザーがADの配布グループの一部であるかどうかを確認しています。

static bool IsUserMemberOf(string userName, string groupName)
{
  using (var ctx = new PrincipalContext(ContextType.Domain))
  using (var groupPrincipal = GroupPrincipal.FindByIdentity(ctx, groupName))
  using (var userPrincipal = UserPrincipal.FindByIdentity(ctx, userName))
  {
    return userPrincipal.IsMemberOf(groupPrincipal);
  }
}

上記のメソッドを次のような値で呼び出していますが、がnull値でIsUserMemberOf("domain\\username","domain\\groupname") あるため、nullポインター例外が表示されgroupPrincipalます。

この点で何か助けはありますか?

4

2 に答える 2

1

それはただそれを意味します:

groupPrincipal = GroupPrincipal.FindByIdentity(ctx, groupName)) 

グループがドメインに存在しないため、nullポインタを返します。あなたはただあなたのvarをテストする必要がありますctxuserPrincipalそしてgroupPrincipal

于 2011-08-26T03:58:53.603 に答える
0

実際、私のグループは、照会しているユーザーとは異なるドメインにあります。プログラムに以下の変更を加えて、現在作業中です。

そして私はこのように呼んでいます:

IsUserMemberOf("domain1\\username","domain2\\groupname")


static bool IsUserMemberOf(string userName, string groupName)
{
 using (var ctx = new PrincipalContext(ContextType.Domain,"domain1"))
 using (var groupPrincipal = GroupPrincipal.FindByIdentity(new PrincipalContext(ContextType.Domain,"domain2"), groupName))
 using (var userPrincipal = UserPrincipal.FindByIdentity(ctx, userName))
 {
    return userPrincipal.IsMemberOf(groupPrincipal);
 }

}

于 2013-01-16T23:21:35.657 に答える