Runtime: System.DirectoryServices.AccountManagement.PrincipalContext support in .NET Core 2.0

Created on 28 Aug 2017  路  49Comments  路  Source: dotnet/runtime

Hello,

I have tried to download the latest version of preview System.DirectoryServices.AccountManagement from myget - https://dotnet.myget.org/feed/dotnet-core/package/nuget/System.DirectoryServices.AccountManagement

And I have tried to use the PrincipalContext:

using(var context = new PrincipalContext(ContextType.Domain))
            {
//code
            }

And I get the System.NullReferenceException:

 {System.NullReferenceException: Object reference not set to an instance of an object.
   at System.DirectoryServices.Protocols.LdapConnection.ConstructEntry(IntPtr entryMessage)
   at System.DirectoryServices.Protocols.LdapConnection.ConstructResponse(Int32 messageId, LdapOperation operation, ResultAll resultType, TimeSpan requestTimeOut, Boolean exceptionOnTimeOut)
   at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request, TimeSpan requestTimeout)
   at System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(String serverName, ServerProperties& properties)
   at System.DirectoryServices.AccountManagement.PrincipalContext.DoServerVerifyAndPropRetrieval()
   at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name, String container, ContextOptions options, String userName, String password)
   at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType)
   at CoreADcko.Controllers.HomeController.Index() in C:\Projects\CoreADcko\CoreAD\CoreAD\Controllers\HomeController.cs:line 17
   at lambda_method(Closure , Object , Object[] )
   at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__12.MoveNext()}

I have used the WebApplication .NET Core template with windows authentication enabled and these dependecies:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
    <AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0-preview2-final" />
    <PackageReference Include="System.DirectoryServices.AccountManagement" Version="4.5.0-preview2-25628-01" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0-preview2-final" />
  </ItemGroup>

</Project>

How can I solve this error?

Thank you.

area-System.DirectoryServices bug

Most helpful comment

All 49 comments

Same issue here

What I found was that targetting net47 things work properly, but when targetting netcoreapp2.0 running in a Windows environment I get the exact same exception.

Same issue with targeting netcoreapp2.0. I am using the latest build: 25630-01

Can confirm this is trivially reproed. Line numbers:

>   System.DirectoryServices.Protocols.dll!System.DirectoryServices.Protocols.LdapConnection.ConstructEntry(System.IntPtr entryMessage) Line 1786   C#  Symbols loaded.
    System.DirectoryServices.Protocols.dll!System.DirectoryServices.Protocols.LdapConnection.ConstructResponse(int messageId, System.DirectoryServices.Protocols.LdapOperation operation, System.DirectoryServices.Protocols.ResultAll resultType, System.TimeSpan requestTimeOut, bool exceptionOnTimeOut) Line 1553   C#  Symbols loaded.
    System.DirectoryServices.Protocols.dll!System.DirectoryServices.Protocols.LdapConnection.SendRequest(System.DirectoryServices.Protocols.DirectoryRequest request, System.TimeSpan requestTimeout) Line 300  C#  Symbols loaded.
    System.DirectoryServices.Protocols.dll!System.DirectoryServices.Protocols.LdapConnection.SendRequest(System.DirectoryServices.Protocols.DirectoryRequest request) Line 245  C#  Symbols loaded.
    System.DirectoryServices.AccountManagement.dll!System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(string serverName, ref System.DirectoryServices.AccountManagement.ServerProperties properties) Line 1132    C#  Symbols loaded.
    System.DirectoryServices.AccountManagement.dll!System.DirectoryServices.AccountManagement.PrincipalContext.DoServerVerifyAndPropRetrieval() Line 700    C#  Symbols loaded.
    System.DirectoryServices.AccountManagement.dll!System.DirectoryServices.AccountManagement.PrincipalContext.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType contextType, string name, string container, System.DirectoryServices.AccountManagement.ContextOptions options, string userName, string password) Line 434    C#  Symbols loaded.
    System.DirectoryServices.AccountManagement.dll!System.DirectoryServices.AccountManagement.PrincipalContext.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType contextType) Line 362    C#  Symbols loaded.
    1.dll!Program.AssemblyLoadRepro.Main() Line 21  C#  Symbols loaded.

This request

        DistinguishedName   null    string
        Filter  "(objectClass=*)"   object {string}
        Scope   Base    System.DirectoryServices.Protocols.SearchScope

is giving back an empty result from ldap_get_dn. I am not famliar with the code so I don't know yet what is supposed to happen.

@tquerec @jay98014 do you have any pointers? I could set up the same repro on desktop to see what it does there, but you know far more about this than I do (zero)

How to get debugging - build corefx in the normal way. Create project thus, replacing paths to the corefx output path:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Reference Include="C:\git\corefx\bin\Windows_NT.AnyCPU.Debug\System.DirectoryServices.AccountManagement\netstandard\System.DirectoryServices.AccountManagement.dll"/>
    <Reference Include="C:\git\corefx\bin\Windows_NT.AnyCPU.Debug\System.DirectoryServices.Protocols\netstandard\System.DirectoryServices.Protocols.dll"/>
    <Reference Include="C:\git\corefx\bin\Windows_NT.AnyCPU.Debug\System.DirectoryServices\netstandard\System.DirectoryServices.dll"/>

    <PackageReference Include="System.DirectoryServices.AccountManagement" Version="4.5.0-preview2-25628-01" />
  </ItemGroup>

</Project>

Paste this line into main
c# using(var context = new PrincipalContext(ContextType.Domain)) {}

Hit F5.

@danmosemsft I have used this setting for now:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.DirectoryServices.AccountManagement" Version="4.5.0-preview2-25631-02" />
  </ItemGroup>

</Project>
using(var context = new PrincipalContext(ContextType.Domain)) {}

But I get absolutely same System.NullReferenceException.

Thank you for any points.

Hmm even easier repro, just run System.DirectoryServices.AccountManagement.Tests, at least on my machine. Interestingly they fail differently in test runs https://mc.dot.net/#/product/netcore/master/source/official~2Fcorefx~2Fmaster~2F/type/test~2Ffunctional~2Fcli~2F/build/20170901.02/workItem/System.DirectoryServices.AccountManagement.Tests

@tquerec @jay98014 can you help us get System.DirectoryServices.AccountManagement.Test passing? Or give me pointers to investigate further?

On Helix machines, they are getting different results: https://github.com/dotnet/corefx/issues/23548. They are not domain joined. We need to fix both.

@tarekgh this is the first thing to look at it directory services. A lot of folks are hitting this. I also got it at one point when running the unit tests.

I'll start looking on Monday.

@tarekgh Hello! Where can I download fix for Domain type PrincipalContext instantiating? Could I find it in future System.DirectoryServices.AccountManagement NuGet package or it would release only as CoreFX fix?

@kazantsev-nikita since yesterday our build system is down and this is being worked on. I would expect this to automatically go to myget when it's up. That doesn't explain why there was no update to the package since the 7th. We certainly had builds since then. @tarekgh any idea about that?

I'll check why we didn't have build for these packages since Sept 7th.

@kazantsev-nikita the fix I did will show at https://dotnet.myget.org/feed/dotnet-core/package/nuget/System.DirectoryServices.Protocols when our builds succeed.

@danmosemsft we already have packages published yesterday. you may look at the version 4.5.0-preview1-25718-03 of the package. note that the list in myget is not sorted by publishing date so you have to scroll down to see such package.

I am not expecting my fix will show in the latest published package though. I think tomorrow's one would be.

@danmosemsft are there any predictions when build system will work?

Guys, something is really messed up with these version numbers, you are building 4.5.0-preview1- now, but the builds from Sep 7 were 4.5.0-preview2-, that's why the list is out of order on your myget.

CC @weshaggard

@kazantsev-nikita

are there any predictions when build system will work?

Hopefully, we'll get build tomorrow.

@los93sol

something is really messed up with these version numbers, you are building 4.5.0-preview1- now, but the builds from Sep 7 were 4.5.0-preview2-, that's why the list is out of order on your myget.

We are trying to align the version with the 2.1 release. This is why we changed it to preview1. preview2 version can be deleted from myget but we don't see strong reason doing that.

@tarekgh Why not bump from 4.5.0 to 4.6.0 so it isn't so confusing

@tarekgh Why not bump from 4.5.0 to 4.6.0 so it isn't so confusing

We wanted to avoid versioning everything in the repo and essentially skipping versions. This is an issue that will only be seen in our dev feeds so it is limited it will never be seen in our official releases on nuget.org.

I have tried download the latest version of System.DirectoryServices.AccountManagement - but I still get same exception - 4.5.0-preview1-25718-03.

Can you please help me with this - is it correct version?

@Jenan we have some issues signing, so binary is not getting updated. Can you try with locally built copy?

@Jenan the version 4.5.0-preview1-25718-03 is built on Mon, 18 Sep 2017 13:19:11 GMT which doesn't have the fix. people around here working hard to get the build succeed. @danmosemsft idea is good to unblock you till you get the official one.

OK, it's fine, I'll be waiting. Thank you. :)

@tarekgh is there any progress in the System.DirectoryServices.Protocols publishing?

Been checking daily for a status update on package publishing, but so far haven't seen a new build pop up yet

People around here working to get the builds back. Sorry for the delay but it is something out of our control. Hopefully, we'll get the builds soon. As suggested, if you really blocked you still have the option to build the library yourself and use it till you get the official one.

Thanks for working on this!

For those watching for a new build, looks like one went up last night

Any idea when a new package will be available?

Take a look here: https://dotnet.myget.org/feed/dotnet-core/package/nuget/System.DirectoryServices.AccountManagement/4.5.0-preview1-25726-06

I have tried this latest version of this package and works fine! :)

but that is preview 1 they recommend 4.5.0-preview2-25707-02 as the latest which is 20 days old.

@rdbossjr I agree with you, this versioning system is insane, but preview1 is the latest. The best way to see what is newest is just to browse the nuget feed or build from source until things rtm. I guess it's the price we pay for bleeding edge

@los93sol as @tarekgh mentions above, this wasn't an intentional versioning scheme, it was essentially a glitch. 馃槂

I have downloaded and am using the latest version, 4.5.0-preview1-25726-06. I never had a null reference exception but I did have a Platform not supported exception. I am on Windows 10.

After updating I am now getting 'Cannot load System.DirectoryServices.Protocols'. I tried pulling down that package as well (same version) and the issue did not resolve.

This is my current setup:
All AD methods are in a class library targeting Net Standard 2.0
Referencing the AD class library in a .net core web api project

calling
using(var context = new PrincipalContext(ContextType.Domain)) { }

Any pointers on where to look to resolve this? I'm asking here because the problem I was having was very similar and updating to the newest release did help some.

Also might be good to mention, in a console app I can use the same code and everything works.

@Influencer could you please share a simple project we can look at?

@weshaggard @ericstj any guess what could be the problem here?

We can consider this a non-issue at the moment. I'm not at my workstation so I can't double check but creating a new webapi project and adding the code in there it appears to be working. I will have to test at work tomorrow morning.

One thing that comes to mind (and the only difference I can think of off hand), is there a limitation from using PrincipalContext when also using Cors? I am hitting the webapi from an angular app running on a different server instance.

@Jenan thank you for 4.5.0-preview1-25726-06
It works fine!
PS: need install via nuget console
Install-Package System.DirectoryServices.AccountManagement -Version 4.5.0-preview1-25726-06 -Source https://dotnet.myget.org/F/dotnet-core/api/v3/index.json
because there is version absent in nuget package manager

@Jenan 4.5.0-preview1-25726-06
This version works only on windows.
Does not work on Linux

ErrorMessage: Operation is not supported on this platform.. StackTrace: at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name, String container, ContextOptions options)

@balkarov Yes, it is probably necessary run this code on windows wih IIS. IIS know information about AD via turn on the windows authentication. But I am not sure.

Please help - @tarekgh or @danmosemsft - Is it possible use PrincipalContextoutside windows and IIS?
Thank you.

@balkarov @Jenan we support Windows only for this release. @danmosemsft may give more details about that.

CC @tquerec

@tarekgh what about plan for Linux?
Will you make it? If yes, how long?

@danmosemsft could you help answering @balkarov question? Thanks.

Hi, I installed the nuget version "4.5.0-preview2-25707-02" for System.DirectoryServices, System.DirectoryServices.Protocols, System.DirectoryServices.AccountManagement, and still having the issue at
at System.DirectoryServices.Protocols.LdapConnection.ConstructEntry(IntPtr entryMessage)
at System.DirectoryServices.Protocols.LdapConnection.ConstructResponse(Int32 messageId, LdapOperation operation, ResultAll resultType, TimeSpan requestTimeOut, Boolean exceptionOnTimeOut)
at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request, TimeSpan requestTimeout)
at System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(String serverName, ServerProperties& properties)
at System.DirectoryServices.AccountManagement.PrincipalContext.DoServerVerifyAndPropRetrieval()
at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name, String container, ContextOptions options, String userName, String password)
at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name, String container, String userName, String password).
Could anyone please help. Thanks.

@alinlam you need install 4.5.0-preview1-25726-06

Thanks @balkarov , the preview1 is working for me, I downloaded 4.5.0-preview1-25813-01. :-)

In the Package Manager Console, I ran the following:

Install-Package System.DirectoryServices.AccountManagement -Version 4.5.0-preview1-25813-01 -Source https://dotnet.myget.org/F/dotnet-core/api/v3/index.json

I got the error:

Unable to find package 'System.DirectoryServices.AccountManagement'

Am I doing something wrong?

Seems like this package is no longer in the JSON feed at:
https://dotnet.myget.org/F/dotnet-core/api/v3/index.json

Is there another way to get it?

Nevermind, had to run the PowerShell command 2x for some reason...odd

Was this page helpful?
0 / 5 - 0 ratings

Related issues

EgorBo picture EgorBo  路  3Comments

Timovzl picture Timovzl  路  3Comments

v0l picture v0l  路  3Comments

yahorsi picture yahorsi  路  3Comments

omajid picture omajid  路  3Comments