2010-05-18 28 views
1

我已經創建了包括WindowsIdentity和WindowsImpersonationContext的模擬類,並且在服務運行一段時間後,我的身份驗證應用程序中添加了模擬lsass.exe過程是消耗大量內存和CPU請問如何解決這個問題?lsass.exe cousumes很多內存和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

也許你可以發佈相關的代碼? – 2010-05-18 07:42:35

+0

你檢查過病毒嗎?有一種病毒,以服務的形式啓動,命名爲lsass.exe – 2010-05-18 07:51:59

回答

2

我不知道,如果你仍然有這個問題,但我有一個非常類似的問題,它與在適當的時候不調用CloseHandle的(...)做。

嘗試移動m_userWindowsID = new WindowsIdentity(token)後的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); 
    } 
} 

希望這有助於!

Chris

+1

我假定在WindowsIdentity上調用Dispose會釋放內存,但似乎必須調用CloseHandle – Doogal 2011-06-03 13:25:36