2009-10-22 1 views
1

현재 사용자가 로그인 한 컴퓨터 계정에 대해 WindowsIdentity를 가져 오려고합니다. 이 잘 작동WindowsIdentity를 통해 현재 컴퓨터로 가장

WindowsIdentity currentIdent = WindowsIdentity.GetCurrent(); 
foreach (IdentityReference indentity in currentGroups) 
{ 
    String groupName = indentity.Translate(typeof(NTAccount)).ToString(); 
} 

,하지만 난 바람직 AD를 쿼리하지 않고 현재의 컴퓨터 계정에 대해 동일한 작업을 수행해야합니다

은 현재 내가 현재 사용자의 그룹 구성원을 얻기 위해 다음 코드를 사용하고 있습니다.

나는 이것을 가장을 사용해서해야한다고 믿지만 어떻게 찾을 수 없었는지.

+0

당신이 '컴퓨터 계정'가 무엇을 의미합니까 :

그래서, 당신은 AD 쿼리해야? –

+0

컴퓨터 도메인 개체 및 그룹 구성원 자격 – benPearce

답변

0

로컬 컴퓨터에는 domain \ computer $의 사용자 토큰과 domain \ computer $의 Kerberos 티켓이라는 두 가지 도메인 계정 그룹 구성원이 있습니다. 로컬 컴퓨터가 사용자 토큰을 필요로 할 때마다 domain \ computer $가 아닌 SYSTEM으로 설정되므로 옵션이 아닙니다. domain \ computer $ Kerberos 티켓에서 사용자 토큰을 얻는 유일한 방법은 티켓을 해독하는 데 키가 필요하므로 SYSTEM으로 실행해야합니다 (또한 운영 체제의 일부로 작동하는 권한이 필요하며 심지어는 운영 체제의 일부로 작동하는 권한이 필요합니다. 티켓에서 토큰을 만드는 방법을 모른다.

using System; 
using System.Runtime.InteropServices; 
using System.Collections.Generic; 
using System.Text; 
using System.Security.Principal; 
using System.ComponentModel; 
using System.DirectoryServices; 

public static SecurityIdentifier[] GetLocalComputerGroups() 
{ 
    string sAMAccountName = PInvoke.GetSYSTEMsAMAccountName(); 
    DirectorySearcher searcher = new DirectorySearcher("(sAMAccountName=" + sAMAccountName + ")"); 
    DirectoryEntry entry = searcher.FindOne().GetDirectoryEntry(); 
    entry.RefreshCache(new string[] { "tokenGroups" }); 
    List<SecurityIdentifier> groupSids = new List<SecurityIdentifier>(); 
    foreach(byte[] byteSid in entry.Properties["tokenGroups"]) 
    { 
     groupSids.Add(new SecurityIdentifier(byteSid, 0)); 
    } 
    return groupSids.ToArray(); 
} 

public class PInvoke 
{ 
    public const int STATUS_SUCCESS = 0; 
    public static readonly IntPtr NULL = IntPtr.Zero; 

    public enum SECURITY_LOGON_TYPE 
    { 
     UndefinedLogonType = 0, 
     Interactive = 2, 
     Network, 
     Batch, 
     Service, 
     Proxy, 
     Unlock, 
     NetworkCleartext, 
     NewCredentials, 
     RemoteInteractive, 
     CachedInteractive, 
     CachedRemoteInteractive, 
     CachedUnlock 
    } 

    [StructLayout(LayoutKind.Sequential)] 
    public struct LARGE_INTEGER 
    { 
     public uint LowPart; 
     public int HighPart; 
    } 

    [StructLayout(LayoutKind.Sequential)] 
    public struct LUID 
    { 
     public uint LowPart; 
     public int HighPart; 

     public static LUID GetSYSTEMLuid() 
     { 
      return new LUID() { LowPart = 0x3E7, HighPart = 0 }; 
     } 
    } 

    [StructLayout(LayoutKind.Sequential)] 
    public struct LSA_UNICODE_STRING 
    { 
     public ushort Length; 
     public ushort MaximumLength; 
     public IntPtr Buffer; 

     public override string ToString() 
     { 
      if (Buffer == NULL) return null; 
      return Marshal.PtrToStringUni(Buffer, Length/UnicodeEncoding.CharSize); 
     } 
    } 

    [StructLayout(LayoutKind.Sequential)] 
    public struct SECURITY_LOGON_SESSION_DATA 
    { 
     public uint Size; 
     public LUID LogonId; 
     public LSA_UNICODE_STRING UserName; 
     public LSA_UNICODE_STRING LogonDomain; 
     public LSA_UNICODE_STRING AuthenticationPackage; 
     public SECURITY_LOGON_TYPE LogonType; 
     public uint Session; 
     public IntPtr Sid; 
     public LARGE_INTEGER LogonTime; 
     public LSA_UNICODE_STRING LogonServer; 
     public LSA_UNICODE_STRING DnsDomainName; 
     public LSA_UNICODE_STRING Upn; 
    } 

    [StructLayout(LayoutKind.Sequential)] 
    public struct SECURITY_LOGON_SESSION_Managed 
    { 
     public LUID LogonId; 
     public string UserName; 
     public string LogonDomain; 
     public string AuthenticationPackage; 
     public SECURITY_LOGON_TYPE LogonType; 
     public uint Session; 
     public SecurityIdentifier Sid; 
     public LARGE_INTEGER LogonTime; 
     public string LogonServer; 
     public string DnsDomainName; 
     public string Upn; 

     public SECURITY_LOGON_SESSION_DATA_Managed(IntPtr pSecurityLogonSessionData) 
     { 
      SECURITY_LOGON_SESSION_DATA data = (SECURITY_LOGON_SESSION_DATA)Marshal.PtrToStructure(pSecurityLogonSessionData, typeof(SECURITY_LOGON_SESSION_DATA)); 
      this.LogonId = data.LogonId; 
      this.UserName = data.UserName.ToString(); 
      this.LogonDomain = data.LogonDomain.ToString(); 
      this.AuthenticationPackage = data.AuthenticationPackage.ToString(); 
      this.LogonType = data.LogonType; 
      this.Session = data.Session; 
      this.Sid = new SecurityIdentifier(ConvertPSIDToString(data.Sid)); 
      this.LogonTime = data.LogonTime; 
      this.LogonServer = data.LogonServer.ToString(); 
      this.DnsDomainName = data.DnsDomainName.ToString(); 
      this.Upn = data.Upn.ToString(); 
     } 
    } 

    [DllImport("kernel32.dll", SetLastError = true)] 
    public static extern IntPtr LocalFree(IntPtr hMem); 

    [DllImport("advapi32.dll", SetLastError = true)] 
    protected static extern bool ConvertSidToStringSidW(IntPtr Sid, out IntPtr StringSid); 

    public static string ConvertPSIDToString(IntPtr pSid) 
    { 
     IntPtr pString; 
     if (ConvertSidToStringSidW(pSid, out pString)) 
     { 
      try 
      { 
       return Marshal.PtrToStringUni(pString); 
      } 
      finally 
      { 
       LocalFree(pString); 
      } 
     } 
     else 
     { 
      throw new Win32Exception(); 
     } 
    } 

    [DllImport("advapi32.dll")] 
    protected static extern int LsaNtStatusToWinError(uint Status); 

    public static Win32Exception NtStatusToWinException(uint ntstatus) 
    { 
     return new Win32Exception(LsaNtStatusToWinError(ntstatus); 
    } 

    [DllImport("secur32.dll")] 
    public static extern uint LsaFreeReturnBuffer(IntPtr Buffer); 

    [DllImport("secur32.dll")] 
    protected static extern uint LsaGetLogonSessionData(ref LUID LogonId, out IntPtr ppLogonSessionData); 

    public static SECURITY_LOGON_SESSION_DATA_Managed GetLogonSessionData(LUID logonId) 
    { 
     IntPtr pLogonSessionData; 
     uint ntstatus = LsaGetLogonSessionData(ref logonId, out pLogonSessionData); 
     if(ntstatus != STATUS_SUCCESS) 
     { 
      throw NtStatusToWinException(ntstatus); 
     } 

     try 
     { 
      return new SECURITY_LOGON_SESSION_DATA_Managed(pLogonSessionData); 
     } 
     finally 
     { 
      LsaFreeReturnBuffer(pLogonSessionData); 
     } 
    } 

    public static string GetSYSTEMsAMAccountName() 
    { 
     LUID systemLuid = LUID.GetSYSTEMLuid(); 
     SECURITY_LOGON_SESSION_DATA_Managed systemData = GetLogonSessionData(systemLuid); 
     return systemData.UserName; 
    } 
}