사용자가 회원인지 확인하고 확인하기 위해 만든 동일한 C #, .Net 2.0 코드를 모두 사용하는 일련의 응용 프로그램이 있습니다. Active Directory 그룹.
최근 다른 신뢰할 수있는 AD 도메인의 사용자를 내 AD 그룹 중 하나에 추가했을 때까지 코드에 문제가 없었습니다. 내 질문은 도메인에 관계없이 사용자가 Active Directory 그룹의 구성원인지 확인하는 방법입니다. 즉, 내 그룹과 동일한 도메인에있을 수도 있고 아닐 수도 있습니다. 다음은 사용자가 Active Directory 그룹에 있는지 확인하기 위해 수년 동안 작성하고 사용한 코드입니다. 이 코드를 어디에서 수정했는지는 모르겠지만 MSDN 기사에서 가져온 것이라고 가정합니다. 또한 솔루션은 .Net 2.0 프레임 워크 용이어야합니다. .Net 3.5에서이 문제에 대해 작동 할 수있는 몇 가지 답변을 찾았습니다. 불행히도 내 시나리오에서는 작동하지 않습니다.
//This method takes a user name and the name of an AD Group (role).
//Current implementations of this method do not contain the user's domain
//with userName, because it comes from the Environment.UserName property.
private static bool IsInRole(string userName, string role)
{
try
{
role = role.ToLowerInvariant();
DirectorySearcher ds = new DirectorySearcher(new DirectoryEntry(null));
ds.Filter = "samaccountname=" + userName;
SearchResult sr = ds.FindOne();
DirectoryEntry de = sr.GetDirectoryEntry();
PropertyValueCollection dir = de.Properties["memberOf"];
for (int i = 0; i < dir.Count; ++i)
{
string s = dir[i].ToString().Substring(3);
s = s.Substring(0, s.IndexOf(',')).ToLowerInvariant();
if (s == role)
return true;
}
throw new Exception();
}
catch
{
return false;
}
}