验证Active Directory中的用户名和安全组
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了验证Active Directory中的用户名和安全组相关的知识,希望对你有一定的参考价值。
public static bool IsUserInDomain(string userName) { string name = userName; bool isInDomain = false; if (name.IndexOf(@"", StringComparison.OrdinalIgnoreCase) != -1) name = name.Substring(name.IndexOf(@"", StringComparison.OrdinalIgnoreCase) + 1); string ADpath = System.Configuration.ConfigurationManager.AppSettings["ADPath"]; string ADusername = System.Configuration.ConfigurationManager.AppSettings["ADUser"]; string ADpassword = System.Configuration.ConfigurationManager.AppSettings["ADPassword"]; string securityGroup = System.Configuration.ConfigurationManager.AppSettings["ADSecurityGroup"]; DirectoryEntry de = new DirectoryEntry { Path = ADpath, Username = ADusername, Password = ADpassword }; // "!userAccountControl:1.2.840.113556.1.4.803:=2" is a check for enabled users only searcher.Filter = "(&(&((objectClass=user)(objectClass=person))(sAMAccountName=" + userName + ")(!userAccountControl:1.2.840.113556.1.4.803:=2)))"; // Is the user found? SearchResult sr = searcher.FindOne(); if (sr != null) { // Find the Security group searcher.Filter = "(&(objectCategory=group)(sAMAccountName=" + securityGroup + "))"; searcher.PropertiesToLoad.Add("distinguishedname"); foreach (SearchResult item in searcher.FindAll()) { // Get the DN from the group if (item.Properties["distinguishedname"].Count > 0) { String dn = item.Properties["distinguishedname"][0].ToString(); searcher.Filter = "(&(sAMAccountName=" + userName + ")(memberOf=" + dn + "))"; SearchResult userAndSecurityGroup = searcher.FindOne(); if (userAndSecurityGroup != null) { isInDomain = true; break; } } } } return isInDomain; }
以上是关于验证Active Directory中的用户名和安全组的主要内容,如果未能解决你的问题,请参考以下文章
根据 ASP.NET Windows 身份验证的用户名与 Active Directory 中的登录名不匹配
C# 中的 LDAP 和 Active Directory 身份验证
如何从 sharepoint 2013 Web 服务和 Active Directory 对用户进行身份验证
具有自定义角色和 Active Directory 的 ASP MVC 5 Windows 身份验证
使用 Spring-boot Application 中的 Spring Security 使用 Active Directory(使用 AD 域)对用户进行身份验证时出现问题