의 구성원 인 경우. 로그인시 사용 중입니다.확인 사용자 나 사용자가 그룹의 구성원 인 경우 코드를 확인해야 그룹
내가 도메인 사용자 및 로컬 사용자 등이 있습니다. testdomain\administrator
및 administrator
.
이
내가 사용하는 코드입니다 :using (DirectoryEntry groupEntry = new DirectoryEntry("WinNT://./" + userGroupName + ",group"))
{
foreach (object member in (IEnumerable)groupEntry.Invoke("Members"))
{
using (DirectoryEntry memberEntry = new DirectoryEntry(member))
{
string completeName = memberEntry.Name;
DirectoryEntry domainValue = GUIUtility.FindDomain(memberEntry);
if (domainValue != null)
{
completeName = domainValue.Name + "\\" + memberEntry.Name;
}
Global.logger.Info("completeName from " + userGroupName + " = " + completeName);
if (userName.Equals(completeName, StringComparison.InvariantCultureIgnoreCase))
{
Global.logger.Debug("IsUserPartOfWindowsGroup returned True with username =" + userName + " , UserGroupName = " + userGroupName);
return true;
}
}
}
Global.logger.Debug("IsUserPartOfWindowsGroup returned false for username =" + userName + " , UserGroupName = " + userGroupName);
return false;
}
이 코드는 작동하지만, 내가보기로
DirectoryEntry domainValue = GUIUtility.FindDomain(memberEntry);
은 프로파일에서 많은 시간을하고있다. 이것을 처리하는 더 나은/빠른 방법이 있습니까?
public static DirectoryEntry FindDomain(DirectoryEntry memberEntry)
{
if (memberEntry.Parent != null)
{
if (memberEntry.Parent.SchemaClassName.Equals("domain", StringComparison.InvariantCultureIgnoreCase))
{
return memberEntry.Parent;
}
}
return null;
}
또 다른 방법 :
DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, userName, Password);
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = "(&(objectClass=user)(|(cn=" + userName + ")(sAMAccountName=" + userName + ")))";
SearchResult result = mySearcher.FindOne();
Global.logger.Info("result == " + result.Path);
foreach (string GroupPath in result.Properties["memberOf"])
{
if (GroupPath.Contains(adminGroupName))
{
Global.logger.Info(compUsrNameForEncryption + "exists in " + adminGroupName);
}
}
우리가'GUIUtility.FindDomain'이 무엇인지 안다면 우리는 아마 더 도움이 될 수. 또한 새 (틱) System.DirectoryServices.AccountManagement] (http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.aspx) 클래스는이 물건의 종류 훨씬 간단합니다. –
[관련 질문 (http://stackoverflow.com/q/9603777/635634)의 코드 [내 대답 (http://stackoverflow.com/a/9604203/635634) 그 질문에 대한 것 아마 당신을 도와 밖으로. –
안녕하세요 babcock .. 나는 finddomain 함수를 추가했습니다. 또한 사용자가 도메인의 일부가 아닌 경우 어떻게해야합니까? – user175084