使用C#从ASP.Net MVC访问Active Directory

时间:2010-06-02 12:14:28

标签: c# asp.net-mvc security active-directory

我需要访问Active Directory才能获取有关客户所属组的信息。我的项目是使用C#的ASP.Net MVC应用程序。我以前从未编写过针对Active Directory的编程,并且需要一些关于最佳入门方式的建议,使用什么安全模型来访问信息,并且可能指向一些好的教程。

4 个答案:

答案 0 :(得分:47)

由于您正在使用MVC,因此您可以访问.NET 3.5中的新System.DirectoryServices.AccountManagement命名空间。这些类应该优先于DirectoryServices本身的旧类,因为它们使用起来要简单得多。有一些问题在3.5中没有解决(例如,在查询组时有1500个成员限制),但我确信这些已在.NET 4.0中得到修复。对于大多数任务,新类非常有效。

 using (var context = new PrincipalContext( ContextType.Domain )) 
 {
      using (var user = UserPrincipal.FindByIdentity( context, "username" ))
      {
          var groups = user.GetAuthorizationGroups();
          ...
      }
 }

答案 1 :(得分:13)

使用System.DirectoryServices命名空间访问AD。

两个最重要的课程是:

  1. DirectoryEntry;
  2. DirectorySearcher
  3. 我们假设您的域名是: MyIntranet.MyCompany.com

    然后,您必须创建DirectoryEntry类的根实例:

    DirectoryEntry root = new DirectoryEntry("LDAP://DC=MyIntranet,DC=MyCompany,DC=com");
    

    在AD中搜索某个组或用户的特定事件时:

    DirectorySearcher searcher = new DirectorySearcher();
    searcher.SearchRoot = root;
    searcher.SearchScope = SearchScope.Subtree;
    

    假设您要查找名为: AnyUser1 的用户名,DirectorySearcher.Filter应如下所示:

    searcher.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(sAMAccountName={0})", "AnyUser1");
    

    然后,通过SearchResult类获取结果,如下所示:

    bool userFound = false;
    SearchResult foundUser = null;
    
    try {
        foundUser = searcher.FindOne(); // You might as well use the FindAll() method if you expect more then one result.
        userFound = foundUser != null;
    } catch(Exception) {
        throw;
    }
    
    if (!userFound)
        return;
    
    DirectoryEntry user = foundUser.GetDirectoryEntry();
    

    然后,您可以通过 memberOf 属性获取此用户所属的组:

    user.Properties("memberOf").Value
    

    有关概述,请参阅此CodeProject文章:How to (almost) everything in Active Directory

    并列出了一些属性:Mapping Between IADsUser Properties and Active Directory Attributes

    编辑#1

    如果您正在使用模拟,您可能会考虑为应用程序设置一些参数,例如 DefaultRootDomain DefaultUserName DefaultPassword ,然后在实例化根DirectoroEntry时使用它们。

    public static class AdHelper {
    
        public static string DefaultRootDse {
            get {
                return Properties.Settings.Default.DefaultRootDomain;
            }
        }
    
        private static string DefaultUserName {
            get {
                return Properties.Settings.Default.DefaultUserName;
            }
        }
    
        private static string DefaultPassword {
            get {
                return Properties.Settings.Default.DefaultPassword;
            }
        } 
    
        public static DirectoryEntry RootDse {
            get {
                if (_rootDse == null)
                    _rootDse = new DirectoryEntry(DefaultRootDse, DefaultUserName, DefaultPassword);
                return _rootDse;
            }
        }
        private static DirectoryEntry _rootDse;
    }
    

答案 2 :(得分:0)

如果你有.NET 3.5或者你可以升级到它 - 请务必使用System.DirectoryServices.AccountManagement中的新功能!

有关更多信息和快速入门,请参阅MSDN杂志上的精彩介绍文章Managing Directory Security Principals in the .NET Framework 3.5

答案 3 :(得分:0)

相关问题