Working with SharePoint 2010 Client Object Model on an SSL enabled site
When working SharePoint 2010 Client Object Model on an SSL enabled SharePoint 2010 site, we also need to pass in the certificate and validate it for the client object model calls to work.
Below is a sample.
1: private void button1_Click(object sender, EventArgs e)
2: {
3: ClientContext context = new ClientContext("https://[sharepointserver]");
4: ServicePointManager.ServerCertificateValidationCallback = delegate(object sender1, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
5: {
6: bool validationResult = true;
7: return validationResult;
8: };
9:
10: ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(customXertificateValidation);
11: context.ExecutingWebRequest +=new EventHandler<WebRequestEventArgs>(context_ExecutingWebRequest);
12:
13: Web web = context.Web;
14: context.Load(web, w => w.Title);
15: context.Credentials = CredentialCache.DefaultNetworkCredentials;
16: context.ExecuteQuery();
17: richTextBox1.Text = web.Title;
18: }
19:
20: static void context_ExecutingWebRequest(object sender, WebRequestEventArgs e)
21: {
22:
23: HttpWebRequest webReq = e.WebRequestExecutor.WebRequest;
24: //webReq.Proxy = new WebProxy("https://[ProxyAddress]"); //Specify a proxy address if you need to
25: X509Certificate cert = X509Certificate.CreateFromCertFile(@"d:\test.cer");
26: webReq.ClientCertificates.Add(cert);
27: }
28:
29: private static bool customXertificateValidation(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error)
30: {
31: return true;
32: }
Hope this helps! Cheers.
Comments
Anonymous
February 21, 2013
Sridhar thank you so much - your solution works well and was easy to adapt to my needs. You saved me a lot of time today and I appreciate it!Anonymous
May 26, 2013
Getting "The remote server returned an error: (400) Bad Request."Anonymous
March 07, 2014
You were the only guy on the internet, who helped me how to use a proxy in this call. Thanks a ton.Anonymous
July 31, 2014
How did you create test.cer file.. Please help me with this.