如何将PrincipalContext与MVC Web应用程序一起使用

Gib*_*boK 3 c# asp.net-mvc active-directory asp.net-mvc-3

我在MVC .Net 3.5中有一个网站,我需要在Controller中使用下面的代码.所以我引用了名称空间

System.DirectoryServices.AccountManagement

我收到一个错误:

Error   1   The type or namespace name 'DirectoryServices' does not exist in the namespace 'System' (are you missing an assembly reference?)
Run Code Online (Sandbox Code Playgroud)

我错过了哪个装配以及如何在项目中添加它?

        // set up domain context
        PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

        // find a user
        UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

        if (user != null)
        {
            // do something here.... 
            string givenName = user.GivenName;
        }
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 19

确保已添加System.DirectoryServices.AccountManagement.dll对此命名空间所在的程序集的引用.

供参考:PrincipalContext.