如何在FBA认证方式的SharePoint站点调用WebService
如果使用Windows认证方式访问的SharePoint站点,那么调用它下面的webservice都是很简单的事情,但是如果站点的认证方式是FBA的,那么应该如何通过认证,访问站点下面的webservice呢?其实方法很简单:模拟web访问方式,构建Cookie对象,使用这个通过认证的Cookie对象访问WebService即可。
代码如下:
using (AuthenticationSvc.Authentication authSvc = new AuthenticationSvc.Authentication())
{
authSvc.Url = @"http://localhost:9101/_vti_bin/Authentication.asmx";
authSvc.CookieContainer = new System.Net.CookieContainer(); //create a new cookie container
authSvc.AllowAutoRedirect = true;
//set the FBA login information
AuthenticationSvc.LoginResult result = authSvc.Login("wdz1", "P@ssW0rd");
//check our loginresult to make sure that we don't have any errors
//if we don't have any errors, then consider us authenticated and we can then call our
//other SharePoint web services by passing in our authentication cookie
if (result.ErrorCode == AuthenticationSvc.LoginErrorCode.NoError)
{
try
{
//now that we're authenticated through FBA try and call the lists web service
using (myformwebservice.DocAveWebService service = new myformwebservice.DocAveWebService())
{
service.Url = "http://localhost:9101/_vti_bin/DocAveWebService.asmx";
//set our authentication cookie that we got above
service.CookieContainer = authSvc.CookieContainer;
Console.WriteLine(service.ServerTime());
}
}
catch (Exception ex)
{
Console.WriteLine("Exception occured while calling lists.asmx" + ex.Message);
}
}
else
Console.WriteLine("Error authentication to web service.");
}
AuthenticationSvc.Authentication 对象是微软提供的FBA认证WebService引用,它提供了Login方法生成Cookie。
myformwebservice.DocAveWebService 是测试的一个自定义WebService引用,使用上面的Cookie,方便得访问FBA认证的webservice。