Следующий код не работает:ProtectedData.Unprotect() после того, как Impersonate()
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();
Пароль не расшифрован.
MSDN говорит
"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."
Спасибо вам за ответ. Но я попытался вызвать LoadUserProfile() после "... id.Impersonate();" и код по-прежнему не работает. Также я не знаю, что делать с результатом функции LoadUserProfile() (его токен IntPtr). Я просто вызываю LoadUserProfile()? Должен ли я что-то делать с токеном? – Andrey
Боюсь, я не знаю, что делать с результатом LoadUserProfile. Я пытался воспроизвести вашу проблему, но я никогда не получаю исключение, которое вы описываете. Я тестировал как локальные, так и учетные записи домена, но ProtectedData.Protect всегда преуспевает. Удачи, мне было бы интересно услышать, нашли ли вы решение ... –
@ rwwilden: Снять защиту, а не защищать. –