0
我找不到與我遇到的錯誤相關的任何內容。模擬X509Store.Add到StoreName.Root - 請求不受支持
的問題是,我在下面store.Add
遇到CryptographicException:「請求不被支持」
的MSDN Documentation是不是非常有幫助,它指出:
證書不能被添加到存儲中。
我用following code爲指導,具有以下變化:
- 他們是加入到StoreName.My和我要添加到 StoreName.Root
- 我想使用IDisposable類來模擬用戶。
我的概念證明代碼(有刪節/以次充好):
public class UserLogonImpersonator : IDisposable
{
private WindowsImpersonationContext _impersonationContext = null;
private const int LOGON_INTERACTIVE = 2;
private const int PROVIDER_DEFAULT = 0;
[DllImport("advapi32.dll", SetLastError=true)]
private static extern int LogonUser(
string lpszUserName,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
public UserLogonImpersonator()
{
_impersonationContext = null;
IntPtr userHandle = IntPtr.Zero;
const string domain = "domain";
const string user = "user";
const string hcpw = "password";
try
{
int logonResult = LogonUser(user, domain, hcpw,
LOGON_INTERACTIVE, PROVIDER_DEFAULT, ref userHandle);
bool isLoggedOn = logonResult != 0;
if (isLoggedOn)
{
_impersonationContext =
WindowsIdentity.Impersonate(userHandle);
}
}
catch(Exception e)
{
// Handle Exception
}
}
public void Dispose()
{
// UndoImpersonation();
}
// Private methods ...
}
public class CertService
{
public void AddCertToRootStore()
{
using(new UserLogonImpersonator())
{
X509Certificate2 rootCert =
new X509Certificate2(certData.CertFilePath);
X509Store store =
new X509Store(StoreName.Root, StoreLocation.CurrentUser);
store.Open(OpenFlags.MaxAllowed);
store.Add(rootCert);
store.Close();
}
}
}
我可以刪除模擬和沒有異常拋出,但這不是正確的用戶的商店。
隨着模仿,我可以把證書放入StoreName.AuthRoot
毫無例外。這不是我希望證書進入的商店。
這些無例外解決方案都不起作用。我要求程序以提升的權限運行並進入另一個用戶的商店。
我在下面添加了我的解決方案。也許有人可以闡明爲什麼在未來發生這種情況。 – Brett