2014-09-18 38 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?