Bin*_*xus 10 c# ldap active-directory asp.net-core
我正在将我的ASP.NET Core RC1应用程序升级到RC2.我有一些参考System.DirectoryServices
和System.DirectoryServices.AccountManagement
一些*的.cs文件,这样我可以查询LDAP.但我不知道如何在Project.json
文件中的RC2中添加对它的引用.我尝试的所有东西都给了我更多的错误.任何帮助表示赞赏.
{
"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" ]
}
}
Run Code Online (Sandbox Code Playgroud)
Bas*_*yon 13
只是想说他们刚刚发布了Microsoft.Windows.Compatibility的预发行版,其中包含工作目录与活动目录所需的System.DirectoryServices组件......它的beta ..但它最终在那里.
https://www.nuget.org/packages/Microsoft.Windows.Compatibility/2.0.0-preview1-25914-04
zdu*_*dub 12
除了上述的Bastyons答案外,您还可以System.DirectoryServices.AccountManagement
从https://www.nuget.org/packages/System.DirectoryServices.AccountManagement/4.5.0-preview1-25914-作为NuGet软件包(预览版)安装在.NET Core应用程序中。04。安装后,您可以创建一个简单的调用来验证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;
}
}
Run Code Online (Sandbox Code Playgroud)
更新:该软件包现在可以从https://www.nuget.org/packages/System.DirectoryServices.AccountManagement/4.5.0作为最终版本获得。
如果您只想在 .NET Core 2.0 中对用户进行身份验证,则只需添加 System.DirectoryServices Nuget 包(无需添加 Microsoft.Windows.Compatibility nuget 包)。.NET Standard 2.0 也支持它
注意:我只在 .NET Core 2.2 中使用了以下代码,但 nuget 兼容性信息表明它适用于 .NET Core 2.0。
要验证密码使用:
var domainAndUsername = domain + @"\" + username;
var entry = new DirectoryEntry(_path, domainAndUsername, pwd);
object isValidPassword = null;
try
{
// authenticate (check password)
isValidPassword = entry.NativeObject;
}
catch (Exception ex)
{
_logger.Log.Debug($"LDAP Authentication Failed for {domainAndUsername}");
return false;
}
Run Code Online (Sandbox Code Playgroud)
此外,如果你想在目录中搜索用户,下面应该工作:
var search = new DirectorySearcher(entry) { Filter = "(SAMAccountName=" + username + ")" };
search.PropertiesToLoad.Add("cn");
var result = search.FindOne();
Run Code Online (Sandbox Code Playgroud)
根据Github 问题,LDAP 尚不支持 System.DirectoryServices.AccountManagement。除此之外,@zdub 和@Bastyon 提供的先前信息似乎仍然有效。
目前,新的 CoreCLR 不支持这个库。有一个关于此的公开 GitHub 问题,您可以在其中找到更多信息和讨论。(如果您仅将 AD 用于身份验证系统,则此处提供了一种解决方法。)
如果您只打算在 Windows 服务器上运行此应用程序,则可以将框架定位为“net452”并在其下添加框架程序集。
"frameworks": {
"net452": {
"frameworkAssemblies": {
"System.DirectoryServices": "4.0.0.0",
"System.DirectoryServices.AccountManagement": "4.0.0.0"
}
}
},
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
24862 次 |
最近记录: |