For learning purposes I am writing my own Active Directory client. In order to execute commands like unlocking accounts and resetting passwords, I have to put in my admin account and password and use and WindowsIdentity.Impersonate run the code as my admin account. I am wondering about my options for securing my password, as it has to be used many times across my program.
From what I understand, I can use SecureString to store the password. But to run code as my admin account, I have use WindowsIdentity.Impersonate, and to use that I have to get a token from LogonUser which requires a regular string and not SecureString.
So I would have to:
Login at start up
Convert input to a
SecureStringClear the input
Then later when I want to preform a function that requires elevation:
Convert previously created
SecureStringtostringPass the converted string to
LogonUserClear the convert string to
nullExecute command
Clear the
LogonUserobject
Is this the correct way to approach this? It seems weird to have to have to convert the SecureString to string to use it... seems like it would defeat the purpose and leave the password more vulnerable while I am converting it.
EDIT: Fixed the name for LogonUser
UserImpersonationis a class I wrote a loooong time ago... it's basically a wrapper for LogonUser , which also requests a string for a password. Can that be changed to anIntPtr? If that's the case, could I just pass aSecureStringto the password parameter instead? (I'd try now but I am no longer at work...) - duckSecureStringdirectly but it should not be difficult to pass in the password safely. I will answer below... - Ilian