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?
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.
PrincipalContext(ContextType, String, String, ContextOptions) Initializes a new instance of the PrincipalContext class with the specified context type, name, container, and context options.
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 ...
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With