模拟用户后加密数据

时间:2019-01-08 08:23:17

标签: windows impersonation

我正在使用此方法获取用户令牌:

[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);

然后,我使用以下代码通过用户上下文保护数据:

WindowsIdentity.RunImpersonated(new SafeAccessTokenHandle(pUserToken), () =>{
byte[] protected = ProtectedData.Protect(new byte[]{1,2,3}, null, DataProtectionScope.CurrentUser);
});

对于某些用户,我会遇到此异常:

  

System.Security.Cryptography.CryptographicException:数据   保护操作失败。这可能是由于   没有为当前线程的用户加载用户配置文件   上下文,这可能是线程正在模拟的情况。在   System.Security.Cryptography.ProtectedData.Protect(Byte [] userData,   Byte [] optionalEntropy,DataProtectionScope范围)

主要进程正在与LocalSystem用户一起运行。

0 个答案:

没有答案
相关问题