현재 Windows 사용자가 로컬 관리자인지 UAC를 사용하여 해당 그룹 구성원을 "획득"하고 있는지 파악하려고합니다. 지금까지 왔어요 무엇그룹 구성원을 빨리 가져 오기
은 다음과 같습니다
var adminIdentifier = new SecurityIdentifier("S-1-5-32-544");
var current = WindowsIdentity.GetCurrent();
bool isAdmin = current.Groups.Contains(adminIdentifier);
bool canBeAdmin = isAdmin;
if (!isAdmin)
{
var adminGroupName = adminIdentifier.Translate(typeof(NTAccount)).Value;
adminGroupName = adminGroupName.Substring(adminGroupName.LastIndexOf('\\'));
string path = "WinNT://./" + adminGroupName + ",group";
using (DirectoryEntry groupEntry = new DirectoryEntry(path))
{
foreach (object member in (IEnumerable)groupEntry.Invoke("Members"))
{
using (DirectoryEntry memberEntry = new DirectoryEntry(member))
{
object obVal = memberEntry.Properties["objectSid"].Value;
SecurityIdentifier sid = null;
if (null != obVal)
{
sid = new SecurityIdentifier((Byte[])obVal,0);
}
canBeAdmin = Equals(current.User, sid);
if (canBeAdmin)
break;
}
}
}
}
Console.WriteLine(canBeAdmin +" "+isAdmin);
이 솔루션은 계산하는 데 몇 밀리 초 걸립니다. 이전에 시도한 System.DirectoryServices.AccountManagement 기반 접근 방식보다 훨씬 빠릅니다.
마지막으로 저를 괴롭히는 것이 하나 있습니다. 관리자 그룹의 SecurityIdentifier를 이름으로 변환해야합니다. 을 SID를 사용하여 DirectoryEntry에 직접 가져 오는 방법이 있어야합니다. Google에 따르면이 방법은 작동합니다.
string path = "LDAP://<SID=" + adminIdentifier.ToString() + ">";
그러나이 방법은 효과가없는 것 같습니다. 구문이 어떻게 생겼는지 어떤 생각?
다른 답변을하기 전에 몇 가지 질문 ... a) 어떤 Windows 버전을 사용하고 있습니까?, b) adminIdentifier.ToString()이 "S-1-5-32 -544 ", c) LDAP 쿼리시 특정 오류가 발생합니까? – Noldorin
a) Windows Vista Ultimate. b) 예. c) 오류는 지정된 도메인을 찾을 수 없음을 나타냅니다. 내 컴퓨터가 어떤 도메인에도 없기 때문에 단서가 될 수 있습니다. 그러나 컴퓨터가 도메인에 있는지 여부에 관계없이 작동하는 솔루션이 필요합니다. –
구문이 좋다 .. 그리고 그것을 사용하고 있기 때문에 나는 그것이 작동하는지 안다. 나는 knownsids에 대해 작동하지 않는 것 같아요. – Flores