6

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?

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
justanotherguy
  • 506
  • 2
  • 4
  • 18

2 Answers2

5

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 ...
Reg Edit
  • 6,719
  • 1
  • 35
  • 46
  • Thank you! I didn't think that reference could be added there, but I hadn't checked the "Add Reference..." option since updating the site to using ASP.NET 4.0. – justanotherguy Sep 18 '14 at 17:01
  • The line of code can now get as far as the "GetGroups()" Before triggering this error now: BC30469: Reference to a non-shared member requires an object reference. – justanotherguy Sep 18 '14 at 17:03
  • @justanotherguy, you are calling `GetGroups()` as if it were a static method (and 'static' in C# means the same as 'shared' in VB.Net). However it is an instance method, so you'll need a Principal **object** before you can call this method. And in fact because Principal is abstract, the **object** will have to be an instance of a (non-abstract) subclass, e.g. UserPrincipal. And then GetGroups() will give you the Groups to which that user belongs. – Peter B Oct 16 '15 at 15:25
5

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?

Community
  • 1
  • 1
Rolo
  • 71
  • 1
  • 4