2010-05-18 3 views
1

WindowsIdentity와 WindowsImpersonationContext가 포함 된 가장 (Impersonation) 클래스를 생성했습니다. 그리고 lsass.exe 프로세스가 실행 된 후 인증 응용 프로그램에 가장을 추가했습니다. 많은 메모리와 CPU를 소비합니다.이 문제를 해결하는 방법을 알려주십시오.lsass.exe 많은 메모리와 CPU를 가지고 있습니다.

public class Impersonation : IDisposable 

{ 
    #region external 
    // Declare signatures for Win32 LogonUser and CloseHandle APIs 

    [DllImport("advapi32.dll", SetLastError = true)] 
    static extern bool LogonUser(
     string principal, 
     string authority, 
     string password, 
     LogonSessionType logonType, 
     LogonProvider logonProvider, 
     out IntPtr token); 

    [DllImport("kernel32.dll", SetLastError = true)] 
    public static extern bool CloseHandle(IntPtr handle); 

    enum LogonSessionType : uint 
    { 
     Interactive = 2, 
     Network, 
     Batch, 
     Service, 
     NetworkCleartext = 8, 
     NewCredentials 
    } 

    enum LogonProvider : uint 
    { 
     Default = 0, // default for platform (use this!) 
     WinNT35,  // sends smoke signals to authority 
     WinNT40,  // uses NTLM 
     WinNT50  // negotiates Kerb or NTLM 
    } 

    #endregion 

    #region variables 

    private WindowsIdentity m_userWindowsID; 

    private WindowsImpersonationContext m_userImpersonationContext; 

    #endregion 

    public void LogonWindowsUser(string domain, string userLogin, string password) 
    { 
     IntPtr token; 
     // Create a token for DomainName\Bob 
     // Note: Credentials should be encrypted in configuration file 
     bool result = LogonUser(userLogin, domain, password, 
           LogonSessionType.NewCredentials, 
           LogonProvider.Default, 
           out token); 
     if (result) 
     { 
      m_userWindowsID = new WindowsIdentity(token); 
     } 
    } 

    public void ImpersonateUser() 
    { 
     if (m_userWindowsID == null) 
     { 
      throw new Exception("User is not loged on"); 
     } 
     m_userImpersonationContext = m_userWindowsID.Impersonate(); 
    } 

    #region IDisposable Members 

    public void Dispose() 
    { 
     if (m_userImpersonationContext != null) 
     { 
      m_userImpersonationContext.Undo(); 
      m_userImpersonationContext.Dispose(); 
     } 
     if (m_userWindowsID != null) 
     { 
      CloseHandle(m_userWindowsID.Token); 
      m_userWindowsID.Dispose(); 
      //m_userWindowsID.Token = IntPtr.Zero; 
     } 
    } 
    #endregion 
} 
+1

은 어쩌면 당신은 관련 코드를 게시 할 수 있을까? –

+0

바이러스를 확인 했습니까? lsass.exe –

답변

2

이 문제가 계속 발생하는 경우 나도 몰라,하지만 매우 비슷한 문제가 발생하고이 적절한 시간에하여 CloseHandle (...)를 호출하지와 함께해야했다.

m_userWindowsID = new WindowsIdentity (토큰) 다음에 CloseHandle (...)을 이동하십시오. LogonWindowsUser 메서드의 줄.

예 :이 도움이

public void LogonWindowsUser(string domain, string userLogin, string password) 
{ 
    IntPtr token; 
    // Create a token for DomainName\Bob 
    // Note: Credentials should be encrypted in configuration file 
    bool result = LogonUser(userLogin, domain, password, 
          LogonSessionType.NewCredentials, 
          LogonProvider.Default, 
          out token); 
    if (result) 
    { 
     m_userWindowsID = new WindowsIdentity(token); 
     CloseHandle(token); 
    } 
} 

희망!

크리스

+1

이라는 이름의 서비스로 시작된 바이러스가있었습니다. WindowsIdentity에서 Dispose를 호출하면 메모리가 확보되지만, CloseHandle을 호출해야합니다. – Doogal

관련 문제