runtime: System.IO.FileNotFoundException System.DirectoryServices.AccountManagement
@CarlosOnline commented on Fri Sep 15 2017
System.DirectoryServices.AccountManagement won’t load from asp.net Core 2.0 (netcoreapp2.0) website
Referencing .net 4.5.1 framework class library from asp.net Core 2.0 (netcoreapp2.0), where class library references System.DirectoryServices.AccountManagement leads to receives FileNotFound exception.
System.IO.FileNotFoundException occurred HResult=0x80070002 Message=Could not load file or assembly ‘System.DirectoryServices.AccountManagement, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’. The system cannot find the file specified. Source=<Cannot evaluate the exception source> StackTrace: at SUR.Util1.ActiveDirectory.GetGroupNames(String userName) in combined\Util1\ActiveDirectory.cs:line 33 at spa.Controllers.HomeController.Index() in combined\spa\Controllers\HomeController.cs:line 15 at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__12.MoveNext()
Repro
Problem solution uploaded to DotNetCore2DirectoryServicesBug
Manual Repro
dotnet new aurelia npm install add new Utils .Net Framework 4.51 project (version doesn’t matter) add class below to Utils .Net Framework 4.51
public class ActiveDirectory
{
public static List<string> GetGroupNames(string userName)
{
var result = new List<string>();
if (string.IsNullOrEmpty(userName))
return result;
var domain = userName.Split('\\').FirstOrDefault();
var pc = new PrincipalContext(ContextType.Domain, domain);
var identity = UserPrincipal.FindByIdentity(pc, userName);
if (identity != null)
{
var src = identity.GetGroups(pc);
src.ToList().ForEach(sr => result.Add(sr.SamAccountName));
}
return result;
}
}
add Utils project reference to asp.net core app project update HomeController.cs
public class HomeController : Controller
{
public IActionResult Index()
{
var groups = ActiveDirectory.GetGroupNames(User.Identity.Name);
return View();
}
}
start debugging
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 30 (18 by maintainers)
Hey @CarlosOnline and @avantidesaigit here is a sample project that contains a .NET Framework Library that uses System.DirectoryServices.AccountManagement and a .NET Core 2.0 App that has a project reference to the .NET Framework Library and calls the API that uses AccountManagement.
The .NET Core 2.0 App needs to have the
PackageReferencetoSystem.DirectoryServices.AccountManagementin order to find the assembly, if not you would get the FileNotFoundException. As you can see here.Note that the .NET Framework Library has to include a reference to System.DirectoryServices.AccountManagement
The System.DirectoryServices.AccountManagement version that I’m referencing is coming from NuGet. https://www.nuget.org/packages/System.DirectoryServices.AccountManagement/4.5.0-preview1-25914-04
Also a few weeks ago we published a compatibility package that includes references to various packages that where ported from .NET Framework, including
System.DirectoryServices.AccountManagementyou can find more about it in this blog postPlease let me know if this fixed your issue or if you have any questions 😃
SOLVED: Both packages solved the issue.
Thank You. The preview1 nuget package versions worked.
Either Install-Package Microsoft.Windows.Compatibility -Version 2.0.0-preview1-25914-04 or Install-Package System.DirectoryServices.AccountManagement -Version 4.5.0-preview1-25914-04 Worked.
Repo has been updated with fix. https://github.com/CarlosOnline/DotNetCore2DirectoryServicesBug Thanks again 👍
The spa - net core project is using PackageReference to System.DirectoryServices. There was no need to modify the net451 project since it has a traditional reference to the System.DirectoryServices
spa.csproj net core 2.0 project
Util.csproj net451 project