2010-05-18 21 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