查询Ad域用户属性
#region 查询用户属性
/// <summary>
/// 查询用户属性
/// </summary>
/// <param name="UserName">用户名</param>
/// <returns></returns>
public async Task<JsonView> QueryUserAttribute(string UserName)
{
var result = new JsonView();
UserAttributeView userAttributeView = new UserAttributeView();
if (string.IsNullOrEmpty(UserName))
{
result.Code = 1;
result.Msg = "用户名称不可为空";
return result;
}
try
{
using (DirectoryEntry entry = operateADHelper.IsConnected())
{
var UserPath = GetEntryPath(UserName, "user");//获取路径
DirectoryEntry UserDE = entry.Children.Find(UserPath, "user");//按路径查找用户对象
userAttributeView = GetUserAttribute(UserDE);//获取用户属性
}
result.Code = 0;
result.Msg = "查询成功";
result.Data = userAttributeView;
return result;
}
catch (DirectoryServicesCOMException ex)
{
result.Code = 1;
result.Msg = "查询失败," + ex.Message.ToString();
return result;
}
}
#endregion
定义GetUserAttribute方法
#region 获取用户属性
/// <summary>
/// 获取用户属性
/// </summary>
/// <param name="de">DirectoryEntry</param>
public UserAttributeView GetUserAttribute(DirectoryEntry de)
{
UserAttributeView userAttributeView = new UserAttributeView();
AttributeRoutines attributeRoutines = new AttributeRoutines();
if (de.Properties.Contains("sn"))//该属性存在
{
attributeRoutines.FirstName = de.Properties["sn"].Value.ToString();//姓
}
if (de.Properties.Contains("givenName"))
{
attributeRoutines.LastName = de.Properties["givenName"].Value.ToString();//名
}
if (de.Properties.Contains("initials"))
{
attributeRoutines.Initials = de.Properties["initials"].Value.ToString();//英文缩写
}
if (de.Properties.Contains("displayName"))
{
attributeRoutines.DisplayName = de.Properties["displayName"].Value.ToString();//显示名称
}
if (de.Properties.Contains("description"))
{
attributeRoutines.Description = de.Properties["description"].Value.ToString();//描述
}
userAttributeView.Routines = attributeRoutines;//属性常规
AttributeAccount attributeAccount = new AttributeAccount();
string Name = de.Properties["userPrincipalName"].Value.ToString();//登录名 必然不为空
if (Name.Contains("@"))//如果登录名中包含域名就拆分登录名和域名
{
attributeAccount.UserName = Name.Substring(0, Name.LastIndexOf("@")); ;
attributeAccount.ADName = Name.Substring(Name.IndexOf('@'));
attributeAccount.UserName2000 = attributeAccount.UserName;
}
else
{
attributeAccount.UserName = Name;
attributeAccount.UserName2000 = Name;
}
attributeAccount.ADName2000 = SubDomainName(de.Path).ToUpper() + @"\";
if (de.Properties.Contains("pwdLastSet"))
{
if (de.Properties["pwdLastSet"].Value.ToString() == "0")
{
attributeAccount.NextLogonChangePsw = true;//用户下次登录需修改密码
}
else
{
attributeAccount.NextLogonChangePsw = false;
}
}
if (de.Properties.Contains("userAccountControl"))
{
var Len = de.Properties["userAccountControl"].Value;
if ((int)Len > (int)ADUserEnum.ADS_UF_DONT_EXPIRE_PASSWD)//值比65536大说明勾选了密码永不过期
{
attributeAccount.NeverExpires = true;//密码永不过期
}
else
{
attributeAccount.NeverExpires = false;
}
}
userAttributeView.Account = attributeAccount;//属性账户
userAttributeView.Subordinate = GetSubordinateList(de);//获取用户隶属于
return userAttributeView;
}
#endregion
定义返回实体类
/// <summary>
/// 用户属性返回实体
/// </summary>
public class UserAttributeView
{
/// <summary>
/// 属性-常规
/// </summary>
public AttributeRoutines Routines { get; set; }
/// <summary>
/// 属性-账户
/// </summary>
public AttributeAccount Account { get; set; }
/// <summary>
/// 属性-隶属于
/// </summary>
public AttributeSubordinate Subordinate { get; set; }
}
/// <summary>
/// 属性-常规
/// </summary>
public class AttributeRoutines
{
/// <summary>
/// 姓
/// </summary>
public string FirstName { get; set; }
/// <summary>
/// 名
/// </summary>
public string LastName { get; set; }
/// <summary>
/// 英文缩写
/// </summary>
public string Initials { get; set; }
/// <summary>
/// 显示名称
/// </summary>
public string DisplayName { get; set; }
/// <summary>
/// 描述
/// </summary>
public string Description { get; set; }
}
/// <summary>
/// 属性-账户
/// </summary>
public class AttributeAccount
{
/// <summary>
/// 登录名
/// </summary>
public string UserName { get; set; }
/// <summary>
/// 域名
/// </summary>
public string ADName { get; set; }
/// <summary>
/// 登录名(windows2000以前版本)
/// </summary>
public string UserName2000 { get; set; }
/// <summary>
/// 域名(windows2000以前版本)
/// </summary>
public string ADName2000 { get; set; }
/// <summary>
/// 密码永不过期
/// </summary>
public bool NeverExpires { get; set; }
/// <summary>
/// 下次登录须修改密码
/// </summary>
public bool NextLogonChangePsw { get; set; }
}
/// <summary>
/// 属性-隶属于
/// </summary>
public class AttributeSubordinate
{
/// <summary>
/// 隶属于组的名称
/// </summary>
public List<SubordinateGroup> Subordinate { get; set; }
}
/// <summary>
/// 隶属于
/// </summary>
public class SubordinateGroup
{
/// <summary>
/// 隶属于组的名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 隶属于组的路径
/// </summary>
public string Path { get; set; }
}