Thursday, August 30, 2012

Cannot evaluate expression because the code of the current method is optimized

To fix problems with debugging due to optimized code:
1.) Pull up the project Properties under the "Project" menu

2.) Select the "Build" tab

3.) Uncheck "Optimize code" under the General area

Friday, August 10, 2012

Attach a client certificate to web service request


Attach an X509 client certificate to a web service request based on the certificates FriendlyName
1.)
//using

using System.Security.Cryptography.X509Certificates;

2.)
//Create web service
WS_Service service = new WS_Service();
//Add certificate
service.ClientCertificates.Add(GetCertificate(certificateName));
service.ExecuteServiceRequest();

3.)
//Adding Certificate
private X509Certificate GetCertificate(string certName)
{
    // Look in the local machine store.
    X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
    store.Open(OpenFlags.ReadOnly);
    X509CertificateCollection col = (X509CertificateCollection)store.Certificates;
    X509Certificate cert = null;

    for (int i = 0; i < col.Count; i++)
    {
        string friendlyName = ((X509Certificate2)col[i]).FriendlyName;
        if (friendlyName.ToLower().Trim() == certName.ToLower().Trim())
        {
            cert = col[i];
            break;
        }
    }
    return cert;
}