반응형
사용자 계정이 활성화 또는 비활성화되었는지 확인하는 방법
반복적 인 사무 업무를 해결하는 데 도움이되는 빠른 C # win Forms 앱을 함께 제공하고 있습니다.
모든 사용자 계정에 대해 AD에서 검색을 수행했으며 확인란이있는 목록보기에 추가하고 있습니다.
계정의 활성화 / 비활성화 상태에 따라 listviewitems의 기본 확인 상태를 기본값으로 설정하고 싶습니다.
string path = "LDAP://dc=example,dc=local";
DirectoryEntry directoryRoot = new DirectoryEntry(path);
DirectorySearcher searcher = new DirectorySearcher(directoryRoot,
"(&(objectClass=User)(objectCategory=Person))");
SearchResultCollection results = searcher.FindAll();
foreach (SearchResult result in results)
{
DirectoryEntry de = result.GetDirectoryEntry();
ListViewItem lvi = new ListViewItem(
(string)de.Properties["SAMAccountName"][0]);
// lvi.Checked = (bool) de.Properties["AccountEnabled"]
lvwUsers.Items.Add(lvi);
}
DirectoryEntry 개체에서 계정 상태를 가져 오기 위해 구문 분석 할 올바른 속성을 찾는 데 어려움을 겪고 있습니다. AD User 속성을 검색 했지만 유용한 정보를 찾지 못했습니다.
누구든지 포인터를 제공 할 수 있습니까?
이 코드는 작동합니다 ...
private bool IsActive(DirectoryEntry de)
{
if (de.NativeGuid == null) return false;
int flags = (int)de.Properties["userAccountControl"].Value;
return !Convert.ToBoolean(flags & 0x0002);
}
System.DirectoryServices.AccountManagement 사용 : domainName 및 username은 도메인 및 사용자 이름의 문자열 값이어야합니다.
using (var domainContext = new PrincipalContext(ContextType.Domain, domainName))
{
using (var foundUser = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, username))
{
if (foundUser.Enabled.HasValue)
{
return (bool)foundUser.Enabled;
}
else
{
return true; //or false depending what result you want in the case of Enabled being NULL
}
}
}
Not that anyone asked, but here's a java version (since I ended up here looking for one). Null checking is left as an exercise for the reader.
private Boolean isActive(SearchResult searchResult) {
Attribute userAccountControlAttr = searchResult.getAttributes().get("UserAccountControl");
Integer userAccountControlInt = new Integer((String) userAccoutControlAttr.get());
Boolean disabled = BooleanUtils.toBooleanObject(userAccountControlInt & 0x0002);
return !disabled;
}
참고URL : https://stackoverflow.com/questions/2005637/how-to-determine-if-user-account-is-enabled-or-disabled
반응형
'programing tip' 카테고리의 다른 글
pip 자체의 버전을 아는 방법 (0) | 2020.11.11 |
---|---|
Bittorent 클라이언트의 DHT는 어떻게 "부트 스트랩"됩니까? (0) | 2020.11.11 |
svn 브랜치와 태그를 git-svn으로 가져 오는 방법은 무엇입니까? (0) | 2020.11.11 |
'less'명령을 사용하는 동안 Unix에서 특수 문자 표시 (0) | 2020.11.11 |
CLR 유형과 EDM 유형의 매핑은 EF 6 & 5에서 모호합니다. (0) | 2020.11.11 |