Re: CAPICOM problem with .NET 2.0



I'm not sure what the problem with CAPICOM is, but did you consider
converting your code to the new .NET 2.0 X509 stuff? Everything you are
doing below is now supported directly by the framework and might be easier
to integrate.

Joe K.

--
Joe Kaplan-MS MVP Directory Services Programming
Co-author of "The .NET Developer's Guide to Directory Services Programming"
http://www.directoryprogramming.net
--
<efrolov@xxxxxxxxx> wrote in message
news:1172675045.093914.251350@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
RSACryptoProvider creation problem:
I used some code for RSACryptoProvider creation with smart card and
CAPICOM COM object
The code worked well in Framework 1.1
but when I tried the same code in Framework 2.0
I got an following Cryptographic exception:
"Unable to open the access token of the current thread"


string url = string.Empty;
rsa = null;
certificate = null;
RSACryptoServiceProvider.UseMachineKeyStore = true;

CspParameters csp = new CspParameters();
csp.Flags = CspProviderFlags.UseMachineKeyStore;
ICertificate2 selectedCert = null;


//open the ceritificate store
Store st = new Store();

st.Open(CAPICOM_STORE_LOCATION.CAPICOM_CURRENT_USER_STORE,
"My",
CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_MAXIMUM_ALLOWED);
//search for the certificate with the digital signature
enabled
foreach (ICertificate2 currCert in st.Certificates)
{

if (currCert.HasPrivateKey())
{
selectedCert = currCert;
break;
}
}
//check if the certificate was found or not
if (selectedCert == null || selectedCert.HasPrivateKey()
== false ||

selectedCert.PublicKey().Algorithm.FriendlyName != "RSA")
{
StringBuilder sb = new StringBuilder();
sb.Append("Can't find a vaild certificate!\n\n");
sb.Append("Valid Certificate Authorities are :\n");
System.Collections.IEnumerator myEnum =
issuerlist.GetEnumerator();
while (myEnum.MoveNext())
sb.Append(myEnum.Current).Append("\n");
throw new Exception(sb.ToString());
}

certificate = selectedCert;

//create cerificate provider
csp.KeyContainerName =
selectedCert.PrivateKey.ContainerName;
csp.ProviderName = selectedCert.PrivateKey.ProviderName;
csp.ProviderType =
Convert.ToInt32(selectedCert.PrivateKey.ProviderType);

//the certificate key number by the key specifications
switch (selectedCert.PrivateKey.KeySpec)
{
case CAPICOM_KEY_SPEC.CAPICOM_KEY_SPEC_KEYEXCHANGE:
csp.KeyNumber = 1;
break;

case CAPICOM_KEY_SPEC.CAPICOM_KEY_SPEC_SIGNATURE:
csp.KeyNumber = 2;
break;
}

//for machine keys use the local store of the machine
if (selectedCert.PrivateKey.IsMachineKeyset())
{
csp.Flags = CspProviderFlags.UseMachineKeyStore;
}
//create rsa cryptogragic provider based on the CSP
try
{
rsa = new RSACryptoServiceProvider(csp);//Here i get
the Exception
}
catch
{ }



.



Relevant Pages

  • CAPICOM problem in .NET 2.0
    ... RSACryptoProvider creation problem: ... The code worked well in Framework 1.1 ... ICertificate2 selectedCert = null; ... //search for the certificate with the digital signature ...
    (microsoft.public.security)
  • CAPICOM problem with .NET 2.0
    ... RSACryptoProvider creation problem: ... The code worked well in Framework 1.1 ... ICertificate2 selectedCert = null; ... //search for the certificate with the digital signature ...
    (microsoft.public.dotnet.security)
  • CAPICOM problem with .Net 2.0
    ... RSACryptoProvider creation problem: ... The code worked well in Framework 1.1 ... ICertificate2 selectedCert = null; ... //search for the certificate with the digital signature ...
    (microsoft.public.platformsdk.security)
  • Re: Password for certificate
    ... Co-author of "The .NET Developer's Guide to Directory Services Programming" ... Dim certificate As New X509Certificate ... and then tries to access the private key in the local store to do the client ... > I'm trying to talk to a webservice that requires a certificate. ...
    (microsoft.public.dotnet.security)
  • Re: Custom Subject name Policy Module tweaking
    ... You should post this in a group for ILM or CLM. ... Co-author of "The .NET Developer's Guide to Directory Services Programming" ... ou=Authentication Certificate ... Yariv Bashan ...
    (microsoft.public.dotnet.security)