使用工具:Visual Studio 2017
注意点:
1、直接通过webservice接口编写的域验证,可以在其他地方如小程序中直接调用接口,也可以复制接口中的代码放到C#程序中
2、接口中输出的内容是以json格式输出的,如需以json格式输出,还需在Bin目录文件中引入【Newtonsoft.Json.dll】
3、【Newtonsoft.Json.dll】下载地址:
https://download.csdn.net/download/qq_39236157/37251842
/// <summary>
/// 域验证登录
/// </summary>
/// <param name="Username">用户名</param>
/// <param name="Password">密码</param>
[WebMethod(Description = "域验证登录")]
public void dd_Login_yuyanzheng(string Username,string Password)
{
string errCode = "";//错误码
string errMsg = "";//错误信息
JObject obj = new JObject();//对象
//域验证
string myADSPath = "LDAP:xxx.xx.xx.xx";//xxx.xx.xx.xx为你的IP地址或公司的域IP
try
{
DirectoryEntry myDirectoryEntry = new DirectoryEntry(myADSPath);
myDirectoryEntry.Username = Username;
myDirectoryEntry.Password = Password;
string strFilter = "(&(objectCategory=person)(objectClass=user))";
DirectorySearcher myDirectorySearcher = new DirectorySearcher(myDirectoryEntry, strFilter);
myDirectorySearcher.Filter = "(SAMAccountName=" + Username + ")";
myDirectorySearcher.PropertiesToLoad.Add("cn");
SearchResult mySearchResult = myDirectorySearcher.FindOne();
if (mySearchResult.ToString() == "")
{
errCode = "no";
errMsg = "登录失败,未知的用户名或错误密码";
}
else
{
errCode = "yes";
errMsg = "登录成功";
}
}
catch (Exception ex)
{
errCode = "catch中的no";
errMsg = ex.Message;
}
obj.Add("status", errCode);
obj.Add("msg", errMsg);
Context.Response.Write(obj.ToString());
Context.Response.End();
}