下面的代码不起作用: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