Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I wrote an app that gets information from some Sharepoint 2010 lists and displays it to the user. It works fine on my development machine.But when I copied the exe to another desktop machine i got the following error

The Type initializer for 'System.Net.ServicePointManager' threw an exception at at System.Net.ServicePointManager.set_ServerCertificateValidationCallback(RemoteCertificateValidationCallback value))

I used the workaround from Brandon Atkinson's blog to connect to an SSL sharepoint site. The code that validates the certificate is this.

using (ClientContext context = new ClientContext("https://sharepointsite/sites/test/"))
            {
                ServicePointManager.ServerCertificateValidationCallback = delegate(object sender1, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
                {
                    bool validationResult = true;
                    return validationResult;
                };
                context.ExecutingWebRequest += new EventHandler<WebRequestEventArgs>(context_ExecutingWebRequest);}

This is the method that creates the web request

static void context_ExecutingWebRequest(object sender, WebRequestEventArgs e)
    {
        HttpWebRequest webReq = e.WebRequestExecutor.WebRequest;
        //webReq.Proxy = new WebProxy("http://[ProxyAddress]"); 
        //Specify a proxy address if you need to 
        X509Certificate cert = X509Certificate.CreateFromCertFile(@"D:\Cert.cer");
        webReq.ClientCertificates.Add(cert);
    }

The stacktrace showed that the error was on this line

ServicePointManager.ServerCertificateValidationCallback = delegate(object sender1, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)

Why is it that it works on one machine and not on another?The only difference between these two machines is that one has Visual Studio installed and the other doesn't. Please share your thoughts.

share|improve this question
    
Just guessing - but does the other server have D:\Cert.cer ? –  Ryan Jul 3 '13 at 11:04
    
@Ryan Oh yes it does. –  user1630812 Jul 4 '13 at 2:04
    
Maybe you must take the fqdn like domain.com to access the domain without any error. –  Sascha Jul 15 '13 at 18:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.