Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot load System.DirectoryServices.AccountManagement in ASP.NET VB site

I have been working on adding Active Directory functionality to an already existing ASP.NET website following this guide by Microsoft: http://support.microsoft.com/kb/326340. It's been a long process, but what I'm stuck on now is not being able to access the AccountManagement class to use certain functions such as "GetGroup()".

I can access DirectoryServices just fine, but not Account Management. When I use the following code to test the reference:

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

I get this error: BC30456: 'AccountManagement' is not a member of 'DirectoryServices'.

I have already added this assembly tag to the web.config page:

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

Also I am importing both namespaces:

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

And this is my version info that shows on the error page:

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

I'm editing this website in VS 2010. What am I missing and how can I get AccountManagement added here? Am I not importing it properly, or is there somewhere I can check to see if there is a missing .dll?

like image 816
justanotherguy Avatar asked Sep 18 '14 15:09

justanotherguy


People also ask

What is System DirectoryServices AccountManagement?

System. DirectoryServices. AccountManagement namespace provides a set of APIs that can be used to access users, security groups and other directory objects stored in Active Directory, which I will demonstrate below. To get started you need to add a reference to System.

What is PrincipalContext?

PrincipalContext(ContextType, String, String, ContextOptions) Initializes a new instance of the PrincipalContext class with the specified context type, name, container, and context options.


2 Answers

Although you're importing both namespaces in the markup, you only reference System.DirectoryServices.dll. The AccountManagement part is in a separate dll.

Add another reference to web.config:

<add assembly="System.DirectoryServices.AccountManagement ...
like image 188
Reg Edit Avatar answered Nov 15 '22 00:11

Reg Edit


I tried as suggested by Reg Edit, but then I had another error in which my application couldn't find the reference described in the web.config. After a few hours, I came across this stackoverflow answer.

It says that the reference to System.DirectoryServices.AccountManagement should have "Copy local" on 'True'. Honestly, I see no reason why that should work, because that is a Framework library, but changing that setting worked for me.

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.

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

like image 41
Rolo Avatar answered Nov 15 '22 01:11

Rolo