使用Sharepoint 2010 Client Object Model 通过SSL验证
Posted on 2011-04-12 21:44 SkyPurple 阅读(1127) 评论(1) 编辑 收藏 举报做过Sharepoint的朋友应该都知道,如果我们想在客户端获取Sharepoint的数据,那么在2010中我们可以使用两种方式:
1:客户端对象模型 Client Object Model
2: 此Web Services也可以在Sharepoint 2007中使用 Web Serivices
ok, 在使用Client Object Model时,需要引入两个组件,分别是Microsoft.Sharepoint.Client.dll,Microsoft.Sharepoint.Client.Runtime.dll
首先我们按照Client Object Model的说明写一个DEMO,访问公司的Sharepoint
View Code
1 static void Main(string[] args)
2 {
3 ClientContext clientContext = new ClientContext("https://[Sharepoint Server]");
4
5 List list = clientContext.Web.Lists
6 .GetByTitle("Announcements");
7 clientContext.Load(list);
8 clientContext.ExecuteQuery();
9 Console.WriteLine("List Title: {0}", list.Title);
10 CamlQuery camlQuery = new CamlQuery();
11 camlQuery.ViewXml = "<View/>";
12 ListItemCollection listItems = list.GetItems(camlQuery);
13 clientContext.Load(listItems);
14 clientContext.ExecuteQuery();
15 foreach (ListItem listItem in listItems)
16 Console.WriteLine("Id: {0} Title: {1}", listItem.Id, listItem["Title"]);
17 }
2 {
3 ClientContext clientContext = new ClientContext("https://[Sharepoint Server]");
4
5 List list = clientContext.Web.Lists
6 .GetByTitle("Announcements");
7 clientContext.Load(list);
8 clientContext.ExecuteQuery();
9 Console.WriteLine("List Title: {0}", list.Title);
10 CamlQuery camlQuery = new CamlQuery();
11 camlQuery.ViewXml = "<View/>";
12 ListItemCollection listItems = list.GetItems(camlQuery);
13 clientContext.Load(listItems);
14 clientContext.ExecuteQuery();
15 foreach (ListItem listItem in listItems)
16 Console.WriteLine("Id: {0} Title: {1}", listItem.Id, listItem["Title"]);
17 }
执行ExecuteQuery()时发生The remote server returned an error: (403) Forbidden. 异常。
哦,权限异常,原来公司使用了安全套接字层(SSL),那么我们就需要证书(Certificate)来进行访问授权,需要提供Sharepoint服务器的证书,经过搜索Client Object Model With SSL后发现需要使用X509证书类,结合解决方案后代码如下:
View Code
1 static void Main(string[] args)
2 {
3 ClientContext clientContext = new ClientContext("https://[Sharepoint Server]");
4 ServicePointManager.ServerCertificateValidationCallback =
5 delegate(object sender1, X509Certificate certificate, X509Chain chain,
6 SslPolicyErrors sslPolicyErrors)
7 {
8 bool validationResult = true;
9 return validationResult;
10 };
11 ServicePointManager.ServerCertificateValidationCallback +=
12 customXertificateValidation2;
13 clientContext.ExecutingWebRequest += context_ExecutingWebRequest2;
14
15 clientContext.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;
16 clientContext.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo(Username, Passwrod);
17 clientContext.ExecuteQuery();
18
19 List list = clientContext.Web.Lists
20 .GetByTitle("Announcements");
21 clientContext.Load(list);
22 clientContext.ExecuteQuery();
23 Console.WriteLine("List Title: {0}", list.Title);
24 CamlQuery camlQuery = new CamlQuery();
25 camlQuery.ViewXml = "<View/>";
26 ListItemCollection listItems = list.GetItems(camlQuery);
27 clientContext.Load(listItems);
28 clientContext.ExecuteQuery();
29 foreach (ListItem listItem in listItems)
30 Console.WriteLine("Id: {0} Title: {1}", listItem.Id, listItem["Title"]);
31 }
32
33 static void context_ExecutingWebRequest2(object sender, WebRequestEventArgs e)
34 {
35 HttpWebRequest webReq = e.WebRequestExecutor.WebRequest;
36 var certificateFile = AppDomain.CurrentDomain.BaseDirectory + @"\sharepoint.cer";
37 X509Certificate cert = X509Certificate.CreateFromCertFile(certificateFile);
38 webReq.ClientCertificates.Add(cert);
39 }
40
41 private static bool customXertificateValidation2(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error)
42 {
43 return true;
44 }
2 {
3 ClientContext clientContext = new ClientContext("https://[Sharepoint Server]");
4 ServicePointManager.ServerCertificateValidationCallback =
5 delegate(object sender1, X509Certificate certificate, X509Chain chain,
6 SslPolicyErrors sslPolicyErrors)
7 {
8 bool validationResult = true;
9 return validationResult;
10 };
11 ServicePointManager.ServerCertificateValidationCallback +=
12 customXertificateValidation2;
13 clientContext.ExecutingWebRequest += context_ExecutingWebRequest2;
14
15 clientContext.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;
16 clientContext.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo(Username, Passwrod);
17 clientContext.ExecuteQuery();
18
19 List list = clientContext.Web.Lists
20 .GetByTitle("Announcements");
21 clientContext.Load(list);
22 clientContext.ExecuteQuery();
23 Console.WriteLine("List Title: {0}", list.Title);
24 CamlQuery camlQuery = new CamlQuery();
25 camlQuery.ViewXml = "<View/>";
26 ListItemCollection listItems = list.GetItems(camlQuery);
27 clientContext.Load(listItems);
28 clientContext.ExecuteQuery();
29 foreach (ListItem listItem in listItems)
30 Console.WriteLine("Id: {0} Title: {1}", listItem.Id, listItem["Title"]);
31 }
32
33 static void context_ExecutingWebRequest2(object sender, WebRequestEventArgs e)
34 {
35 HttpWebRequest webReq = e.WebRequestExecutor.WebRequest;
36 var certificateFile = AppDomain.CurrentDomain.BaseDirectory + @"\sharepoint.cer";
37 X509Certificate cert = X509Certificate.CreateFromCertFile(certificateFile);
38 webReq.ClientCertificates.Add(cert);
39 }
40
41 private static bool customXertificateValidation2(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error)
42 {
43 return true;
44 }
增加一个验证服务器证书的静态回调函数, 以及一个context的EventHandler<WebRequestEventArgs>,要使用SSL证书加密,必须要根据证书创建X509Certificate实例,执行ExecuteQuery后取得访问授权。结果如下: