ProtectedData.Unprotect()后模拟()
下面的代码不起作用: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."
这里是发生了什么事情:为DPAPI的工作,它需要这部分得自用户的登录凭据用户的密钥材料。该数据与用户的配置文件一起存储。这就是为什么一个进程没有运行,因为用户必须加载用户的配置文件。
必须调用使用从LoadUserProfile(返回)数据
这里就是你需要做什么UnloadUserProfile())))
LogonUser的(
冒充(
LoadUserProfile(
加密( )
UnloadUserProfile()
RevertImpersonation()(撤消()在.NET)
您必须检查错误,使用GetLastError函数()的每一步。
请注意,为你所有这些东西基本上都是需要过程是管理员帐户。您需要备份和还原权限才能加载用户的配置文件。如果在调用LoadUserProfile当你得到一个特权不持有错误,那么你需要:
一)确保应用程序帐户具有备份和恢复PRIVS
二)PRIVS启用,默认情况下它们都没有。
可以使用AdjustTokenPrivileges()http://msdn.microsoft.com/en-us/library/aa375202(VS.85).aspx
谢谢您的答复启用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