views:

48

answers:

1

I am retrieving a WindowsIdentity object by calling:

win_id = System.Security.Principal.WindowsIdentity.GetCurrent();

with the intention of getting the currently logged on user name, which works fine.

WindowsIdentity implements IDisposable, but since I din't create the object myself, do I still need to call .Dispose() on it when I am finished with it or not?

Thanks.

A: 

WindowsIdentity.GetCurrent() returns an instance, so you should properly dispose it.

Quote from MSDN:

Returns a WindowsIdentity object that represents the current Windows user.

And here is another nice article regarding this topic:

WindowsIdentity.GetCurrent() returns an instance which you should dispose, otherwise you temporarily leak a user handle.

Ondrej Slinták