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

梦想的初衷 提交于 2019-12-31 00:50:56

问题


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": "4.0.0.0",
        "System.DirectoryServices.AccountManagement": "4.0.0.0"
      }
    },
    "dnxcore50": { },
    "net451": { }
  },

  "exclude": [
    "wwwroot",
    "node_modules"
  ],
  "publishExclude": [
    "**.user",
    "**.vspscc"
  ]
}

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.


回答1:


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;
    #endif
    

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.




回答2:


To the anser of @Tseng I want to add that Directory Services are planned for .NET Core 1.2. Here is the feature request on GitHub: https://github.com/dotnet/corefx/issues/2089. You can subscribe there and get notifications when something is happening there!

When it's released you can just use Directory Services in ASP.NET Core with .NET Core! It might take some time, considering the current state and version of .NET Core, though.




回答3:


Microsoft has released pre-release version for System.DirectoryServices in asp.net core 2.0. You can get it from nuget package manager using this command:

Install-Package System.DirectoryServices -Version 4.5.0-preview1-25914-04

This is working fine for me till now.



来源:https://stackoverflow.com/questions/36048910/problems-with-directory-services-and-account-management-in-asp-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!