OTDS Authentication (C#)
This tutorial explains how to use OTDS authentication with the CWS API using C# and Visual Studio.
Outline
- Create a New Project
- Add a Service Reference
- Authenticate with OTDS
- Validate with Content Server
- Using the Services
- Run the Program
- Source Code
- References
Create a New Project
The first thing we need is a new project in Visual Studio to work with.
-
Open Visual Studio.
-
Open the File menu and select New → Project... (Ctrl + Shift + N).
-
Select Visual C# as the project type, select Console Application as the project template, enter a name for the project, and then select OK.
Add a Service Reference
Next, we need to add a service reference for CWS and point it to each service we want to use.
-
Open the Project menu and select Add Service Reference...
-
Enter the URL of the CWS Authentication service in the Address box and select Go.
-
Enter
CWS
as the namespace and select OK. -
Open the Project menu and make sure the Show All Files option is selected. In the Solution Explorer, open the Service References → CWS → Reference.svcmap file.
-
Add a new
MetadataSource
element for each of the other CWS services you want to use and one for the OTDS Authentication service.... <MetadataSources> <MetadataSource Address="http://localhost/cws/Authentication.svc" Protocol="http" SourceId="1" /> <MetadataSource Address="http://localhost/cws/Collaboration.svc" Protocol="http" SourceId="2" /> <MetadataSource Address="http://localhost/cws/ContentService.svc" Protocol="http" SourceId="3" /> <MetadataSource Address="http://localhost/cws/DocumentManagement.svc" Protocol="http" SourceId="4" /> <MetadataSource Address="http://localhost/cws/MemberService.svc" Protocol="http" SourceId="5" /> <MetadataSource Address="http://localhost/cws/WorkflowService.svc" Protocol="http" SourceId="6" /> <MetadataSource Address="http://localhost:8080/ot-authws/Authentication?wsdl" Protocol="http" SourceId="7" /> </MetadataSources> ...
-
In the Solution Explorer, select the CWS service reference under the Service References folder. In the Project menu select Update Service Reference.
Authenticate with OTDS
Now that the services are ready to use we can go ahead and start writing some code. The first thing to do is to authenticate a user with OTDS. You will need to change the username
and password
to use valid credentials of a user on your system.
// The user's credentials string username = "username@OTCS"; string password = "password"; // Create the OTDS Authentication service client // Note: Since we have two "Authentication" services, the generated client proxies // appended a "1" to the name of the OTDS Authentication service client. Authentication1Client otdsAuthClient = new Authentication1Client(); OTAuthentication otAuth = new OTAuthentication(); // Store the OTDS authentication token string otdsToken = null; // Call the Authenticate() method to get an authentication token try { Console.Write("Authenticating User With OTDS..."); otdsToken = otdsAuthClient.Authenticate(ref otAuth, username, password); Console.WriteLine("Success!\n"); } catch (FaultException e) { Console.WriteLine("Failed!"); Console.WriteLine("{0} : {1}\n", e.Code.Name, e.Message); return; } finally { // Always close the client otdsAuthClient.Close(); }
Validate with Content Server
Next, we need to validate the OTDS token with Content Server. To do this we use the CWS Authentication service and the ValidateUser() method.
// Create the CWS Authentication service client AuthenticationClient cwsAuthClient = new AuthenticationClient(); // Store the CWS authentication token string cwsToken = null; // Call the ValidateUser() method try { Console.Write("Validating OTDS authentication token with Content Server..."); cwsToken = cwsAuthClient.ValidateUser(otdsToken); Console.WriteLine("Success!\n"); } catch (FaultException e) { Console.WriteLine("Failed!"); Console.WriteLine("{0} : {1}\n", e.Code.Name, e.Message); return; } finally { // Always close the client otdsAuthClient.Close(); }
Using the Services
The other CWS services are all similar in the way they are used.
- Create a service client.
- Use an OTAuthentication object to pass along the authentication token with each request. You can use the same OTAuthentication object across all services so you do not have to create a separate one for each service.
- Use the service client to call operations on the service.
- Close the client.
Here is an example using the DocumentManagement service to get a user's favorites:
// Create the DocumentManagement service client DocumentManagementClient docManClient = new DocumentManagementClient(); // Set the authentication token on the OTAuthentication object otAuth.AuthenticationToken = cwsToken; // Store the favorites Node[] favorites = null; // Call the GetAllFavorites() method to get the user's favorites try { Console.Write("Getting the user's favorites..."); favorites = docManClient.GetAllFavorites(ref otAuth); Console.WriteLine("Success!\n"); } catch (FaultException e) { Console.WriteLine("Failed!"); Console.WriteLine("{0} : {1}\n", e.Code.Name, e.Message); return; } finally { // Always close the client docManClient.Close(); } // Output the user's favorites Console.WriteLine("User's Favorites:\n"); if (favorites != null) { foreach (Node node in favorites) { Console.WriteLine(node.Name); } } else { Console.WriteLine("No Favorites."); } Console.WriteLine();
Run the Program
If everything is setup correctly you should be able to successfully run the program.