SharePoint Web Service的身份验证

SharePoint内置了一套相对比较完整的Web Services提供给开发者,这样就可以在客户端、跨平台的程序中读取甚至修改SharePoint中的内容。不过当SharePoint网站不允许匿名访问的时候,调用Web Services自然也需要提供身份验证。

      SharePoint身份验证最常用的是Windows验证(Windows Authentication)和表单验证(Form Authentication)两种。

      Windows验证即使用Windows账号或者AD账号来进行身份验证,对于这种验证方式,在SharePoint SDK中已经给出了如何提供身份验证的方法.

      假设我们使用最常用的lists.asmx来获取SharePoint列表数据,添加的Web引用的命名空间是 spwsList.

 

使用当前帐户身份:

[c-sharp]  view plain copy
  1. spwsList.Lists wsLists = new spwsList.Lists();  
  2. wsLists.Credentials = CredentialCache.DefaultCredential;  
  3. Console.WriteLine(wsLists.GetListCollection().OuterXml);  
 

使用指定帐户:

[c-sharp]  view plain copy
  1. spwsList.Lists wsLists = new spwsList.Lists();  
  2. wsLists.Credentials = new NetworkCredential("username""password""domain");  
  3. Console.WriteLine(wsLists.GetListCollection().OuterXml)  
 上面的CredentialCache、NetworkCredential都是在System.Net命名空间下。

 

    表单认证是SharePoint 2007新加入的一种身份认证方式,其后台是使用.Net Framework中的Membership机制进行用户身份的识别,对于外网来说,大都是使用表单认证的方式来实现的。在使用表单认证的SharePoint网站中通过Web Service获取数据稍微麻烦一些,不过也可以通过SharePoint提供的表单认证Web Service来创建用户身份相关的Cookie。

    表单认证Web Service的地址是:http://[server]/[site]/_vti_bin/authentication.asmx

    在使用Web Service的程序中再次加入上面这个Web引用,假设其命名空间是spwsAuth,那么使用表单认证构造身份并访问数据的代码实例如下:

[c-sharp]  view plain copy
  1. spwsAuth.Authentication auth = new spwsAuth.Authentication();  
  2.  auth.CookieContainer = new CookieContainer();  
  3.  auth.AllowAutoRedirect = true;  
  4.  spwsAuth.LoginResult lr = auth.Login("username""password");  
  5.  if (lr.ErrorCode == spwsAuth.LoginErrorCode.NoError)  
  6.   {  
  7.      spwsList.Lists wsList = new spwsList.Lists();  
  8.       wsList.CookieContainer = auth.CookieContainer;  
  9.     XmlNode res = wsList.GetListCollection();  
  10.     Console.WriteLine(res.OuterXml);  
  11.   }  
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值