公司用的系统很多要求用域账号来登录,具体实现如下:
using System.DirectoryServices;
public string ValidateDomainUser(string Domain, string UserName, string Password)
{
DirectoryEntry entry = new DirectoryEntry(Domain, UserName, Password);
try
{
string objectSid = (new SecurityIdentifier((byte[])entry.Properties["objectSid"].Value, 0).Value);//如果账号不对则会抛出异常
return objectSid;
}
catch
{
return null;
}
finally
{
entry.Dispose();
}
}
这里的第一个参数是用来初始化DirectoryEntry.Path参数的,CSDN对于此参数的介绍如下:
语法 Path 属性会根据提供程序而不同。 一些常见的情况有:
WinNT
连接到的计算机上的组。 例如,"WinNT:// < 域名 > / < 计算机名 > / < 组名称 >"。 如果要连接到本地计算机上,"WinNT: / /< 计