2009-03-17 4 views
1

ScénarioComment puis-je toujours activer une boîte de dialogue demandant une broche de carte à puce?

J'essaie d'utiliser des certificats pour signer des documents. La première fois que je signe, le système d'exploitation affiche une boîte de dialogue permettant à l'utilisateur de définir le code PIN, mais pas les fois suivantes. Pour des raisons de sécurité, j'en ai besoin chaque fois que je signe, le système d'exploitation demande le code PIN à l'utilisateur. Quelqu'un sait comment faire cela?

Voici le code:

''// create ContentInfo 
Dim content As New ContentInfo(bytesContenido) 

''// create a signer 
Dim signer As New CmsSigner(certificado) 

''// SignedCms represents signed data 
Dim signedMessage As New SignedCms(content) 

''// sign the data 
signedMessage.ComputeSignature(signer, False) 

''// create and return PKCS #7 byte array 
Return signedMessage.Encode() 

Sur certaines pages, j'ai trouvé que l'utilisation du CryptSetProvParam peut nettoyer la broche, mais jusqu'à présent, ne fonctionne pas.

La déclaration:

<DllImport("advapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _ 
Public Shared Function CryptSetProvParam(ByVal hProv As IntPtr, ByVal dwParam As Int32, ByVal pbData As Byte(), ByVal dwFlags As Int32) As Boolean 

End Function 

L'invocation:

Public punteroContexto As New System.IntPtr  ''// Obtenido usando CryptAcquireContext 
Public Const PP_SIGNATURE_PIN As UInt32 = 33 

If (Not CryptSetProvParam(punteroContexto, PP_SIGNATURE_PIN, Nothing, 0)) Then 
    Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error) 
End If 

L'erreur:

Invalid type specified. (Exception from HRESULT: 0x8009000A)

tester également d'utiliser multithread (en utilisant un autre thread juste pour la signature) et doesn' t travail.

Merci beaucoup!

Répondre

2
[DllImport("Advapi32.dll", SetLastError = true)] 
public static extern bool CryptSetProvParam(IntPtr hProv, uint dwParam, IntPtr pvData, uint dwFlags); 

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
public static extern bool CryptAcquireContext(ref IntPtr hProv, 
    string pszContainer, string pszProvider, uint dwProvType, uint dwFlags); 

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
public static extern void CryptReleaseContext(IntPtr hProv, uint dwFlags); 

static public bool ClearPINCache2(RSACryptoServiceProvider key) 
{ 
    const uint PP_KEYEXCHANGE_PIN = 32; 
    const uint PP_SIGNATURE_PIN = 33; 
    bool bretval = false; 

    IntPtr hProv = IntPtr.Zero; 

    if (CryptAcquireContext(ref hProv, key.CspKeyContainerInfo.KeyContainerName, 
     key.CspKeyContainerInfo.ProviderName, (uint)key.CspKeyContainerInfo.ProviderType, 0)) 
    { 
     if ((CryptSetProvParam(hProv, PP_KEYEXCHANGE_PIN, IntPtr.Zero, 0) == true) && 
      (CryptSetProvParam(hProv, PP_SIGNATURE_PIN, IntPtr.Zero, 0) == true)) 
     { 
      bretval = true; 
     } 
    } 

    if (hProv != IntPtr.Zero) 
    { 
     CryptReleaseContext(hProv, 0); 
     hProv = IntPtr.Zero; 
    } 

    return bretval; 
} 
Questions connexes