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.

Andy
  • 5,188
  • 10
  • 42
  • 59

1 Answers1

3

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.

H H
  • 263,252
  • 30
  • 330
  • 514
Ondrej Slinták
  • 31,386
  • 20
  • 94
  • 126
  • 2
    That second article has very important context you don't link: "Unless you impersonate". Apparently that can result in difficult to track down errors. – Quibblesome Jan 11 '17 at 10:57