下面的代碼不起作用:ProtectedData.Unprotect()後模擬()
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."
謝謝您的答覆啓用PRIVS。 但我試圖在「... id.Impersonate();」之後調用LoadUserProfile()和代碼仍然沒有工作。 此外,我不知道如何處理函數LoadUserProfile()(其返回IntPtr的令牌)的結果做。 我只是調用LoadUserProfile()嗎? 我是否會用令牌做點什麼? – Andrey 2010-03-23 20:24:50
恐怕我不知道如何處理LoadUserProfile的結果。我試圖重現你的問題,但我從來沒有得到你描述的例外。我測試了本地和域帳戶,但ProtectedData.Protect總是成功。祝你好運,我有興趣聽到如果你找到解決方案... – 2010-03-24 12:32:41
@rwwilden:解除保護,而不是保護。 – 2010-03-24 13:53:25