Thursday, April 23, 2009

Using SharePoint Web Services using Forms Authentication

Scenario:

Solution:
Authentication Web service. It has a method named Login that, when called, places an authentication cookie in the proxy's System.Web.Services.Protocols.HttpWebClientProtocol.CookieContainer collection. That cookie can then be used in subsequent requests to other Web services in the site that is protected by forms authentication to authenticate the request.

For purposes of creating your Web service proxies (such as adding Web references in Visual Studio), use a Windows authentication–protected site. In most cases, the Visual Studio Add Web Reference wizard does not work with a SharePoint site that is protected by forms authentication.

Add a Web reference to the Authentication Web service in the site that is protected by forms authentication; the Authentication Web service can be found in the path http://servername/_vti_bin/authentication.asmx. Name this Web reference fbaAuth.

Code:

fbaAuth.Authentication auth = new fbaAuth.Authentication();

auth.CookieContainer = new System.Net.CookieContainer();
auth.AllowAutoRedirect = true;

fbaAuth.LoginResult lr = auth.Login("myUserName", "myUserPassword");

if (lr.ErrorCode == fbaAuth.LoginErrorCode.NoError)
{
//Now we can talk to the Lists Web service.
fbaLists.Lists lists = new fbaLists.Lists();
// This is IMPORTANT for reusing the cookie
lists.CookieContainer = auth.CookieContainer;

XmlNode xData = lists.GetListCollection();
}
Note:
By default, a zone that is configured to use forms authentication does not enable Client Integration features. This option, which is found on the Authentication Provider page in SharePoint Central Administration, must be turned on if you want to use the SharePoint Web services. When this option is turned off, SharePoint Products and Technologies also turn off support for remote interfaces, such as Web services.

To reuse this cookie, we need to set CookieContainer property for the Lists Web service proxy equal to the CookieContainer property of the Authentication Web service proxy.

4 comments:

Anonymous,  July 16, 2009 at 4:04 AM  

Hi, i have a problem accessing sharepoint web services through an ISA 2006 reverse proxy that is using Forms Based Authentication.
Have you any suggestions?

Sandeep July 16, 2009 at 8:00 AM  

What kind of error are u getting ?

Anonymous,  July 21, 2009 at 3:43 AM  

hi, sorry for delay.
it seems that i'm not getting through the ISA proxy.

error message:
Der vom Client gefundene Anforderungsinhaltstyp ist 'text/html', erwartet wurde 'text/xml'.
Fehler bei der Anforderung mit folgender Fehlermeldung:
--
{!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"}
{!-- {57A118C6-2DA9-419d-BE9A-F92B0F9A418B} --}
{html}
{head}
{title>Microsoft ISA Server 2006{/title}
{meta http-equiv="Content-Type" content="text/html; CHARSET=utf-8"}
{meta content="NOINDEX, NOFOLLOW" name="Robots"}
{link href="/CookieAuth.dll?GetPic?formdir=......

error message end: it seems i'm getting the authentication page from ISA Server

architecture:
proxy -- ISA(FBA) -- SP(FBA)

the first proxy is no problem, but i'm not getting through ISA and so i'm not getting the SP web service from c#.

Sandeep July 21, 2009 at 8:27 AM  

If you try off ISA , are you able to go through. if yes then You need to check ISA documentation for how to create exception for such a case