2014-09-18 49 views
4

我一直在努力将Microsoft Active Directory功能添加到Microsoft现有的以下指南中:ASP.NET网站:http://support.microsoft.com/kb/326340。这是一个漫长的过程,但我现在坚持不能访问AccountManagement类来使用某些函数,如“GetGroup()”。无法加载ASP.NET VB站点中的System.DirectoryServices.AccountManagement

我可以访问DirectoryServices,但没有帐户管理。当我用下面的代码来测试参考:

Response.Write(System.DirectoryServices.AccountManagement.Principal.GetGroups()) 

我得到这个错误:BC30456:“AccountManagement”不是“的DirectoryServices”中的一员。

我已经添加了该组件标签到web.config页:

<add assembly="System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" /> 

而且我导入这两种命名空间:

<%@ Import Namespace="System.DirectoryServices" %> 
<%@ Import Namespace="System.DirectoryServices.AccountManagement" %> 

这是我的版本信息,其中显示有错误页面:

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34237

我在VS 2010中编辑此网站。我缺少什么以及如何获得Acc ountManagement添加在这里?我是不是正确导入它,或者是否有某处我可以检查是否存在缺少的.dll文件?

+0

对我来说,不是有效的使用'<%@大会名称= “System.DirectoryServices.AccountManagement” %>' – Kiquenet 2016-12-20 07:58:40

回答

4

尽管您在标记中导入了两个名称空间,但您只能引用System.DirectoryServices.dll。 AccountManagement部分是在一个单独的dll中。

添加另一个参考web.config中:

<add assembly="System.DirectoryServices.AccountManagement ... 
+0

谢谢!我不认为可以在那里添加引用,但是我没有检查“添加引用...”选项,因为这个选项更新为使用ASP.NET 4.0。 – justanotherguy 2014-09-18 17:01:25

+0

代码行现在可以获得“GetGroups()”现在触发此错误之前: BC30469:对非共享成员的引用需要对象引用。 – justanotherguy 2014-09-18 17:03:05

+0

@justanotherguy,你调用'GetGroups()'就好像它是一个静态方法(在C#中'静态'和VB.Net中的'shared'相同。但是它是一个实例方法,因此在调用此方法之前,您需要一个Principal **对象**。事实上,因为Principal是抽象的,所以**对象**将必须是(非抽象的)子类的实例,例如, UserPrincipal。然后,GetGroups()将为您提供该用户所属的组。 – 2015-10-16 15:25:19

4

我试图通过注册编辑的建议,但后来我有另一个错误在我的应用程序找不到在web.config中描述的参考。几个小时后,我碰到this stackoverflow answer

上面说的是参考System.DirectoryServices.AccountManagement应该有“复制本地”上“”。说实话,我没有看到为什么应该这样工作,因为这是一个框架库,但改变这个设置对我有效。

You can do something like this:

using (var context = new PrincipalContext(ContextType.Domain)) 
{ 
    var principal = UserPrincipal.FindByIdentity(context, User.Identity.Name); 
    var firstName = principal.GivenName; 
    var lastName = principal.Surname; 
} 

You'll need to add a reference to the System.DirectoryServices.AccountManagement assembly.

You can add a Razor helper like so:

@helper AccountName() 
    { 
     using (var context = new PrincipalContext(ContextType.Domain)) 
    { 
     var principal = UserPrincipal.FindByIdentity(context, User.Identity.Name); 
     @principal.GivenName @principal.Surname 
    } 
} 

If you indend on doing this from the view, rather than the controller, you need to add an assembly reference to your web.config as well:

<add assembly="System.DirectoryServices.AccountManagement" /> 

Add that under configuration/system.web/assemblies .

来源:Answer To: How do I get the full name of a user in .net MVC 3 intranet app?