AD


ドメインユーザでログイン検証し、自グループのユーザを確認

using System;
using System.DirectoryServices.AccountManagement;
string userName = "ゆーざめい";
string pass = "ぱすわーど";
string dcname = "どめいん.local";

using (PrincipalContext pctxt = new PrincipalContext(ContextType.Domain, dcname))
{
  var canLogion = pctxt.ValidateCredentials(userName, pass);
  Console.WriteLine("ログイン:{0}", canLogion);
}
using (var pctxt = new PrincipalContext(ContextType.Domain, dcname, userName, pass))
{

  UserPrincipal user = UserPrincipal.FindByIdentity(pctxt, IdentityType.SamAccountName, userName);
  var groups = user.GetGroups();
  foreach (var g in groups)
  {
    Console.WriteLine(g.Name);
    if (!(g is GroupPrincipal))
      continue;
          
    var group = (GroupPrincipal)g;
    foreach (var u in group.Members)
    {
      Console.WriteLine(u.Name);
    }//end loop user
          
  }//end loop group
}//end ctx
カテゴリー: 開発 タグ: パーマリンク