ISSUE/BUG Report X509CertificateCollection.Remove/Contains

G

Guest

LS,

The X509CertificateCollection.Remove and X509CertificateCollection.Contains
are not consistent with each other. Although a X509CertificateCollection
reports that it contains a certificate, it might not be able to remove this
certificate with exception "Cannot remove the specified item because it was
not found in the specified Collection". I believe this is not consistent with
ICollection interface / pattern. Please correct me if I am wrong.

See code snippet below:
_____________________________________
public void Reproduce( X509Certificate certificate )
{
System.Security.Cryptography.X509Certificates.X509CertificateCollection coll
= new X509CertificateCollection ();

System.Security.Cryptography.X509Certificates.X509Certificate
otherCeritificate = new X509Certificate (certificate);

coll.Add ( certificate );

System.Diagnostics.Debug.WriteLineIf ( coll.Contains ( otherCeritificate ),
"*Collection contains otherCertificate" );

try
{
coll.Remove ( otherCeritificate );
}
catch ( Exception e )
{
System.Diagnostics.Debug.WriteLine ( string.Format ( "*An exception
occured while removing otherCertificate from the collection. Message: {0}.",
e.Message ) );
}

}

_____________________________________

This prints:

*Collection contains otherCertificate
*An exception occured while removing otherCertificate from the collection.
Message: Cannot remove the specified item because it was not found in the
specified Collection..
_____________________________________

Our Fix:

// Workaround.
for ( int i = 0; i < certificateCollection.Count; i++ )
{
if ( certificateCollection.Equals ( certificate ))
{
certificateCollection.RemoveAt ( i );
break;
}
}

Not too pretty, but it works.

Regards,
Martijn Kaag
______________________________
www.VECOZO.nl
 
P

Peter Huang [MSFT]

Hi Martijn,

I can reproduce with your code, and I have reported the issue to our
product team.
Thanks for your feedback.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top