ProtectedData.Unprotect() after Impersonate()

Posted by Andrey on Stack Overflow See other posts from Stack Overflow or by Andrey
Published on 2010-03-23T18:23:25Z Indexed on 2010/03/23 19:33 UTC
Read the original article Hit count: 1282

The following code doesn't work:

IntPtr token = Win32Dll.LogonUser(“user1”, “mydomain”, “password1”);
WindowsIdentity id = new WindowsIdentity(token);
WindowsImpersonationContext ic = id.Impersonate();
byte[] unprotectedBytes = ProtectedData.Unprotect(passwordBytes, null, DataProtectionScope.CurrentUser);
password = Encoding.Unicode.GetString(unprotectedBytes);
ic.Undo();

The password is not decrypted.

MSDN said "If you use this method during impersonation, you may receive the following error: "Key not valid for use in specified state." This error can be prevented by loading the profile of the user you want to impersonate, before calling the method."

I would be very grateful for the help!

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET