2016-05-19 75 views
2

我正在將我的ASPNET Core RC1應用程序升級到RC2。我在一些* .cs文件中對System.DirectoryServices和System.DirectoryServices.AccountManagement有一些引用,以便我可以查詢LDAP。但我不知道如何在Project.json文件中的RC2中添加引用。我所嘗試的一切只是給了我更多的錯誤。任何幫助表示讚賞。在ASP.NET Core中使用DirectoryServices

{ 
    "version": "1.0.0-*", 
    "buildOptions": { 
    "emitEntryPoint": true, 
    "preserveCompilationContext": true 
    }, 

    "dependencies": { 
    "Microsoft.NETCore.App": { 
     "version": "1.0.0-rc2-3002702", 
     "type": "default" 
    }, 
    "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final", 
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final", 
    "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final", 
    "Microsoft.AspNetCore.Mvc.TagHelpers": "1.0.0-rc2-final", 
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final", 
    "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final", 
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview1-final", 
    "Microsoft.EntityFrameworkCore": "1.0.0-rc2-final", 
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-rc2-final", 
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview1-final", 
    "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.0-rc2-final", 
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-rc2-final", 
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final", 
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final", 
    "Newtonsoft.Json": "8.0.3", 
    "Microsoft.Extensions.Logging": "1.0.0-rc2-final", 
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final", 
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-final", 
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc2-final", 
    "System.Linq": "4.0.1-beta-23516", 
    "System.Linq.Queryable": "4.0.1-beta-23516" 
    }, 

    "tools": { 
    "Microsoft.AspNetCore.Razor.Tools": { 
     "version": "1.0.0-preview1-final", 
     "imports": "portable-net45+win8+dnxcore50" 
    }, 
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": { 
     "version": "1.0.0-preview1-final", 
     "imports": "portable-net45+win8+dnxcore50" 
    }, 
    "Microsoft.EntityFrameworkCore.Tools": { 
     "version": "1.0.0-preview1-final", 
     "imports": [ 
     "portable-net45+win8+dnxcore50", 
     "portable-net45+win8" 
     ] 
    }, 
    "Microsoft.Extensions.SecretManager.Tools": { 
     "version": "1.0.0-preview1-final", 
     "imports": "portable-net45+win8+dnxcore50" 
    }, 
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": { 
     "version": "1.0.0-preview1-final", 
     "imports": [ 
     "portable-net45+win8+dnxcore50", 
     "portable-net45+win8" 
     ] 
    } 
    }, 

    "frameworks": { 
    "netcoreapp1.0": { 
     "imports": [ 
     "dotnet5.6", 
     "dnxcore50", 
     "portable-net45+win8" 
     ] 
    } 
    }, 

    "publishOptions": { 
    "include": [ 
     "wwwroot", 
     "Views", 
     "appsettings.json", 
     "web.config" 
    ] 
    }, 

    "scripts": { 
    "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ] 
    } 
} 
+0

您是否在使用AD身份驗證系統或僅查詢AD? –

回答

2

目前新的CoreCLR不支持該庫。有關於此的open GitHub issue,您可以在其中找到更多信息和討論。 (如果您僅在身份驗證系統中使用AD,則會提供解決方法。)

如果您只打算在Windows服務器上運行此應用程序,則可以將「net452」作爲框架目標並添加框架在下面的組件。

"frameworks": { 
    "net452": { 
    "frameworkAssemblies": { 
     "System.DirectoryServices": "4.0.0.0", 
     "System.DirectoryServices.AccountManagement": "4.0.0.0" 
    } 
    } 
}, 
+1

感謝您提及我在GitHub主題上發佈的解決方法!這對我很好。我會建議閱讀,但你打敗了我。 –

+0

@ClintB哇,和克林特B一樣?感謝您發佈它!我最終自己使用它來進行內部應用 - 爲我節省了很多麻煩。 –

+0

酷!我很高興它幫助!你對我的[問題](http://stackoverflow.com/questions/37330500/asp-net-core-rc2-cant-find-html-encoder-implementation)有任何見解嗎? –

1

您可以使用​​
兼容的.NET運行時:.NET核心,.NET框架4.6,...
它可以對任何LDAP協議兼容的目錄服務器(包括Microsoft Active Directory )。

private static bool LoginLdap(string username, string password) 
    { 
     try 
     { 
      using (var conn = new LdapConnection()) 
      { 
       conn.Connect("<LdapHost>", 389); 
       conn.Bind(LdapConnection.Ldap_V3, $"<yourDomain>\\{username}", password); 
      } 
      return true; 
     } 
     catch (LdapException) 
     { 
      return false; 
     }    
    } 

欲瞭解更多信息閱讀這個問題:Support for System.DirectoryServices
它很適合我。 (在.net核心1.1.1)

1

添加到Bastyons上面的回答,您可以在.NET應用程序的核心作爲NuGet包(預覽版)從https://www.nuget.org/packages/System.DirectoryServices.AccountManagement/4.5.0-preview1-25914-04安裝System.DirectoryServices.AccountManagement。一旦安裝,您可以創建一個簡單的調用來驗證AD用戶,如下所示:

public static bool ValidateCredentials(string userName, string password) 
{ 
    try 
    { 
     using (var adContext = new PrincipalContext(ContextType.Domain, "YOUR_AD_DOMAIN")) 
     { 
      return adContext.ValidateCredentials(userName, password); 
     } 
    } 
    catch(Exception ex) 
    { 
     throw ex; 
    } 
} 
相關問題