The WCF Cookie Manager is a class library that enables you to share cookies across Windows Communication Foundation (WCF) clients. You can find source code and downloadable binaries for the class library in the WcfCookieManager project on CodePlex.
Example
The sample code below shows and example of using the WCF Cookie Manager to share context and state across clients of the Authentication and Folder services.
Note: For brevity, this example has no error checking and assumes that the default endpoint location is correct for the library. Also, this code is written for version 4.5 of the .NET Framework.
The example below is from a Visual Studio project that has service references to the Authentication and Folder services using the namespaces Ait.Authentication and Ait.Folder, respectively.
namespace Example_WcfCookieManager
{
internal class Program
{
private static void Main(string[] args)
{
string username = args[0]; //Get the username
string password = args[1]; //Get the password
string token = args[2]; //Get the API token
WcfCookieManager.CookieManagerEndpointBehavior cookieManager = new WcfCookieManager.CookieManagerEndpointBehavior(); //Initialize the instance of the cookie manager to share
Ait.Authentication.AuthenticationClient clientAuthentication = new Ait.Authentication.AuthenticationClient(); //Initialize a new instance of the Authentication service client
clientAuthentication.Endpoint.EndpointBehaviors.Add(cookieManager); //Add the cookie manager to the Authentication service client
Ait.Folder.Folder1Client clientFolder = new Ait.Folder.Folder1Client(); //Initialize a new instance of the Folder service client
clientFolder.Endpoint.EndpointBehaviors.Add(cookieManager); //Add the cookie manager to the Folder service client
clientAuthentication.ApiLogin(username, password, token); //Log in to the library
Ait.Folder.Folder rootFolder = clientFolder.Get(0); //Get the root folder for the library
}
}
}
In the example above, the request to the ApiLogin method can succeed regardless of whether the client for the Authentication service has the cookie manager. However, you do not add the cookie manager to both clients, then the request to the Get method will result in an exception of the type Authorit.API.Exceptions.AuthenticationException. This happens because the Folder service client does not include any context in its request and the server has no way to know that the request is coming from the same code as the Authentication service client. By adding the same instance of the CookieManagerEndpointBehavior class to each of your Server Application Programming Server clients, you enable the clients to share the same context and state.