WEB开发使用AD域用户名和密码登录
发表时间:2017-2-21
发布人:葵宇科技
浏览次数:40
jquery代码
function CheckADUser() {
var domain = "ssglobal.com"var userName = "username"
var pwd = "123456"
$.post("/Order/CheckADUser", { domain: domain, userName: userName, pwd: pwd }, function (data) {
alert(data)
})
}
html代码
<button value="test" οnclick="CheckADUser()">ADUser</button>
cs代码 controller层
using System.DirectoryServices;
public ActionResult CheckADUser(string domain,string userName,string pwd)
{
System.DirectoryServices.DirectoryEntry entry = new System.DirectoryServices.DirectoryEntry(string.Format("LDAP://{0}",
domain),
userName,
pwd);
string r = "ok";
try
{
DirectorySearcher search = new DirectorySearcher(entry); //创建DirectoryEntry对象的搜索对象
search.Filter = "(SAMAccountName=" + userName + ")"; //过滤条件为登录帐号=user
SearchResult result = search.FindOne(); //查找第一个
if (null == result) //没找到
{
r = "cancel";
}
}
catch (Exception ex)
{
r = ex.Message;
}
return Json(r);
}