Alec Menconi Alec Menconi - 1 month ago 13x
C# Question

Problems with Directory Services and Account Management in ASP.NET Core

I have a ASP.NET Core API project and I'd like to utilize DirectoryServices and DirectoryServices.AccountManagement namespaces.

After some research I thought I found some project.json references that would work:

"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true

"dependencies": {
"EntityFramework.Commands": "7.0.0-rc1-final",
"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
"EntityFramework.MicrosoftSqlServer.Design": "7.0.0-rc1-final",
"Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
"Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
"Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final"

"commands": {
"web": "Microsoft.AspNet.Server.Kestrel",
"ef": "EntityFramework.Commands"

"frameworks": {
"dnx451": {
"frameworkAssemblies": {
"System.DirectoryServices": "",
"System.DirectoryServices.AccountManagement": ""
"dnxcore50": { },
"net451": { }

"exclude": [
"publishExclude": [

After adding those, code that used classes from those references suggested the namespaces to add to the class:

using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;

After that intellisense had no complaints and everything looked perfect, however if I try to build or run the project I get "type or namespace name 'DirectoryServices' does not exist in the namespace 'System'" errors.

Is there a resource out there can help me figure out how to implement DirectoryServices and DirectoryServices.AccountManagement in ASP.Net 5?

As a side note I like ASP.NET Core but it seems like having to manually create a json file for references is a big step back from adding references in MVC 5 where you just had to find them and check the box to add them. Makes me wonder if there isn't something I'm missing that can generate that file for you rather than needing to know the exact characters and pasting them in yourself to get things working.


In ASP.NET Core (the new Name for ASP.NET 5 and vNext isn't being officially used for months) a project will by default target multiple platforms.

You have added the references to dnx451 framework moniker, which targets the full .NET 4.5 Framework.

Now if you use

using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;

inside a *.cs file, it will work for .NET 4.5, but fail for .NET Core, because there is no reference for .NET Core to this namespaces/assemblies. On build always both versions will be compiled, that's why you get this error.

You don't see it in the Visual Studio Editor, because it shows you the code of .NET 4.5 (above the code window is a pulldown menu where you can switch between the target frameworks).

That being said, you have two choices:

  1. You want to target only .NET 4.5

    In this case, you just delete the dnxcore50 moniker from your frameworks section of the project.json file (net451 too because it's for class libraries only). This will turn your project an ASP.NET Core project which targets only the full .NET 4.5.

  2. You want/need to target .NET Core w/o working against the full .NET 4.5 Framework

    This is a bit more difficult. You either need a replacement for the same library in .NET Core. If it uses the same namespaces, just add its references to the dnxcore50 moniker and you are good to go.

    If there is no direct replacement, you have to remove this code parts from .NET Core projects, by using preprocessor directives.

    #if DNX451
    using System.DirectoryServices;
    using System.DirectoryServices.AccountManagement;

Now your namespace will be used only in the .NET 4.5 target, but not in the .NET Core target.

On a sidenote: You can remove the net451 moniker, as it's only used for Class Library (Package), the new project type for .NET Core libraries which compiles into a package which targets multiple framework targets. See the RC1 announcement for more.

For applications (ASP.NET Core Web Project or xUnit project) always use the application moniker (dnx451 and dnxcore50 respectively). For class libraries it's net451 and dotnet5.x.