Active Directory帮助程序类

Sai*_*han 5 .net helper active-directory

是否有某个Active Directory帮助程序类可用?在重新发明轮子之前检查一下.

我需要

  1. 在AD中验证用户.

  2. 获得hhis /她的成员角色.

谢谢

tva*_*son 10

在.NET 3.5中,您希望查看System.DirectoryServices.AccountManagement.对于早期版本,System.DirectoryServices版本具有您所需要的功能,但它需要更多工作.

using (var context = new PrincipalContext( ContextType.Domain ))
{
      var valid = context.ValidateCredentials( username, password );
      using (var user = UserPrincipal.FindByIdentity( context,
                                                      IdentityType.SamAccountName,
                                                      username ))
      {
          var groups = user.GetAuthorizationGroups();
      }
}
Run Code Online (Sandbox Code Playgroud)

  • 如果您的用户是计算机的本地用户,则可以使用ContextType.Machine而不是ContextType.Domain将查询集中在本地授权存储上. (2认同)