c# - Impersonation memory leak -
on http://msdn.microsoft.com/en-us/library/w070t6ka(v=vs.100).aspx there example on how impersonation .net 4.0. have used example in class inherits idisposable ease of use. however, when use class in asp.net web application, notice slight steady increase of pool paged bytes in performance monitor. after week, application crashes.
i've tried different implementations of impersonation-class, using http://msdn.microsoft.com/en-us/library/w070t6ka(v=vs.90).aspx , http://support.microsoft.com/kb/306158 reference, show same leak.
where leak come from? there problem windows api? running windows 2008 r2.
this our current version of impersonation class:
public class impersonator : idisposable { public impersonator(string username, string domain, string password) { if (!impersonatevaliduser(username, domain, password)) { throw new securityexception("could not impersonate. wrong username / password"); } } public void dispose() { undoimpersonation(); } [dllimport("advapi32.dll", setlasterror = true, charset = charset.unicode)] private static extern bool logonuser(string lpszusername, string lpszdomain, string lpszpassword, int dwlogontype, int dwlogonprovider, out safetokenhandle phtoken); private const int logon32_provider_default = 0; private const int logon32_logon_interactive = 2; //this parameter causes logonuser create primary token. private windowsimpersonationcontext impersonateduser; private bool impersonatevaliduser(string username, string domain, string password) { safetokenhandle safetokenhandle; // call logonuser obtain handle access token. bool success = logonuser(username, domain, password, logon32_logon_interactive, logon32_provider_default, out safetokenhandle); if (success) { using (safetokenhandle) { // use token handle returned logonuser. windowsidentity newid = new windowsidentity(safetokenhandle.dangerousgethandle()); impersonateduser = newid.impersonate(); } } return success; } private void undoimpersonation() { // releasing context object stops impersonation if (impersonateduser != null) { impersonateduser.undo(); impersonateduser.dispose(); } } } public sealed class safetokenhandle : safehandlezeroorminusoneisinvalid { private safetokenhandle() : base(true) { } [dllimport("kernel32.dll")] [reliabilitycontract(consistency.willnotcorruptstate, cer.success)] [suppressunmanagedcodesecurity] [return: marshalas(unmanagedtype.bool)] private static extern bool closehandle(intptr handle); protected override bool releasehandle() { return closehandle(handle); } }
and performance monitor graph of 2 webservers using different versions of class:
perfmon http://img222.imageshack.us/img222/5388/captureyog.png
when disable class, , use global impersonation via web.config, lines flat.
update
i have made test-application reproduce problem. can downloaded here:
http://rapidshare.com/files/447325211/impersonationtest.zip
the result on 18 hours looks this:
testapp http://img689.imageshack.us/img689/2055/impersonationtest.png
hard tell. @ least, windowsidentity
idisposable
, , newid
variable never disposed of. also, check if uses of impersonator
class disposed of.
Comments
Post a Comment