【发布时间】:2018-11-03 08:16:12
【问题描述】:
我正在使用 ASP.NET 技术开发 WEB API 项目。此 Web API 需要从 AD Active Directory 中检查用户,作为域身份验证使用 LDEP://
[HttpGet]
public IHttpActionResult ListProperties(string domainName, string userName, string password)
{
try
{
using (DirectoryEntry dEntry = new DirectoryEntry("LDAP://" + domainName, userName, password))
{
DirectorySearcher dSearcher = new DirectorySearcher(dEntry)
{
Filter = "(&(objectClass=user)(mail=" + userName + "))"
};
SearchResult sResult = dSearcher.FindOne();
Dictionary<string, string> resultDictionary = new Dictionary<string, string>
{
{"Name", GetProperty(sResult,"cn")},
{"Email", GetProperty(sResult,"mail")}
};
return Ok(resultDictionary.ToList());
}
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
private string GetProperty(SearchResult searchResult, string propertyName)
{
if (searchResult.Properties.Contains(propertyName))
{
return searchResult.Properties[propertyName][0].ToString();
}
return string.Empty;
}
所以我只用ajax调用这个方法来测试
$(document).ready(function () {
$.ajax({
type: "GET",
url: "../api/xxxxxxx/ListProperties",
data: { domainName: "mydomain.xxx.xx", userName: "MyUsername", password: "MyPassword" },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) { console.log(JSON.stringify(data)); },
failure: function (data) { console.log(0); },
error: function (data) { console.log(1); }
});
});
不幸的是,我总是收到错误的请求或以下错误
System.Runtime.InteropServices.COMException HResult=0x8007203A Message=The server is not operational.
您能否为我提供解决此问题的指南,因为我以前从未使用过安全编程。
【问题讨论】:
-
不能用PrincpalContext吗?例如。
using (var context = new PrincipalContext(ContextType.Domain, domain)) { var user = UserPrincipal.FindByIdentity(context, userName); }
标签: asp.net authentication ldap asp.net-web-api2