简体   繁体   English

C# 如何在使用智能卡时防止多个 Windows PIN 提示?

[英]C# how to prevent multiple Windows PIN prompts when using a smart card?

I'm using a Gemalto IDPrime .NET smart card for my application.我的应用程序使用的是 Gemalto IDPrime .NET 智能卡。 The following method encrypts some bytes and displays the signature:以下方法加密一些字节并显示签名:

private static void SimpleDataEncryption()
{
    var csp = new CspParameters(1, "Microsoft Base Smart Card Crypto Provider")
    {
        Flags = CspProviderFlags.UseDefaultKeyContainer
    };

    var rsa = new RSACryptoServiceProvider(csp);
    var data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
    Console.WriteLine("Data         : " + BitConverter.ToString(data));
    var sig = rsa.SignData(data, "SHA1");
    Console.WriteLine("Signature    : " + BitConverter.ToString(sig));

    var verified = rsa.VerifyData(data, "SHA1", sig);
    Console.WriteLine("Verified     : " + verified);
}

So far, this methods works flawlessly.到目前为止,这种方法完美无缺。

My problem is that each time the application is started, Windows prompts the user to reenter the PIN.我的问题是每次启动应用程序时,Windows 都会提示用户重新输入 PIN。 The documentation on Smart Card Architecture states the following: 智能卡架构文档说明如下:

The Base CSP internally maintains a per-process cache of the PIN. Base CSP 在内部维护 PIN 的每个进程缓存。 The PIN is encrypted and stored in memory. PIN 被加密并存储在内存中。

I found a way to set the PIN of a smartcard programmatically using the advapi32.dll , but this approach seems very hackish to me since the PIN has to be passed as clear text.我找到了一种使用advapi32.dll 以编程方式设置智能卡的 PIN 的方法,但这种方法对我来说似乎很advapi32.dll ,因为 PIN 必须以明文形式传递。

So my question is:所以我的问题是:

Is there a way supported by .NET to cache the PIN for the smart card the first time the user enters it correctly and use it each time until the PIN is changed by the user? .NET 是否支持在用户第一次正确输入时缓存智能卡的 PIN 并每次使用它,直到用户更改 PIN?

As mentioned above, .Net crypto API manages to work with smart card, and it caches password per process for performance.如上所述,.Net 加密 API 设法与智能卡一起使用,并为每个进程缓存密码以提高性能。

So if you still use .Net crypto API and want the password to be cached between app restarts, you have to cache password yourself.因此,如果您仍然使用 .Net 加密 API 并希望在应用程序重新启动之间缓存密码,则必须自己缓存密码。 Of course, you face security risks.当然,您面临安全风险。 The user password can be changed out side .Net, so you can not receive a change event.用户密码可以在 .Net 之外更改,因此您无法收到更改事件。 You can prompt the user to update the password when password error raises during authenticating with smartcard.在使用智能卡进行身份验证期间出现密码错误时,您可以提示用户更新密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM