Using .SVC Web Service in C# for ADFS
This topic contains a small example for a SVC. web sevice call with claims based authentication using ADFS
Before you begin
- Prerequisites
-
This working example was created September 2013 using the following prerequisites:
- Microsoft .NET FrameWork
- Microsoft Visual Studio
- Web service URL
-
Make sure you know the full URL to the Content Manager web services which is a combination of the following input parameters:
baseurlcontaining something likehttps://ish.example.com(orhttp://ish.example.com) whereishrefers to an example server dedicated to Content Manager.infosharewswebappnamewhich contains the name of the website for the Content Manager web service (e.g. ISHWS)
This URL (e.g.
http://ish.example.com/ISHWS) is referenced below asWebServiceURL
About this task
This example describes how to make 2 WCF SVC. web service calls to get the version of the Content Manager components and the metadata for the logged-in user when your system is configured to use ADFS and windows authentication.
Procedure
- Create a Visual Studio Project, for example, a Console Application.
- Navigate to the project then right click on References section.
- Add a web service reference to the
Application25web service:- Select Add Service Reference...
- In the Adress text box type the
WebServiceURLfollowed by/Wcf/API25/application.svcFor example:http://ish.example.com/ISHWS/Wcf/API25/application.svc - Enter in the Namespace, the value
Application25ServiceReference
- If the relying party's policy of the STS contains multiple issuer endpoints without specifying which issuer endpoint to use, one of the endpoints is selected as the issuer endpoint to use. Check the configuration to ensure that the right issuer endpoint was selected.
- Open the
app.configfile - Goto
CustomBinding_Applicationbinding (on top) locate theissuedTokenParametersnode and replace the full issuer node with the correct issuer. Normally the other endpoints are available in commented form in the configuration as<alternativeIssuedTokenParameters>. Locate and copy the right issuer endpoint for bindingws2007HttpBinding.For this example we used the issuer that has a binding configuration for
13/windowsmixed. - Repeat previous step for
CustomBinding_Application1
- Open the
- Add a web service reference to the
User25web service:- Select Add Service Reference...
- In the Adress text box, enter the
WebServiceURLfollowed by/Wcf/API25/user.svcFor example:http://ish.example.Com/ISHWS/Wcf/API25/user.svc - Enter in the Namespace, the value
User25ServiceReference
- Open the
app.configfile. - Check that there is exactly one
endpointfor each web service reference- The Content Manager web services are available via
httpandhttps. Regardless of the choice you made in theWebServiceURLbothendpointsare present in theapp.config. Remove the endpoint which does not match yourWebServiceURL.<endpoint address="http://ish.example.com/ISHWS/Wcf/API25/Application.svc" binding="customBinding" bindingConfiguration="CustomBinding_Application" contract="Application25ServiceReference.Application" name="CustomBinding_Application" /> <endpoint address="https://ish.example.com/ISHWS/Wcf/API25/Application.svc" binding="customBinding" bindingConfiguration="CustomBinding_Application1" contract="Application25ServiceReference.Application" name="CustomBinding_Application1" /> - Check that there is an endpoint for both web services
The
endpointfor a second web service reference is not always added automatically. If the endpoint is missing, copy the chosenendpointof the other web service and correct the address and the contract to match the missing endpoint.Resulting in the following 2 endpoints:
- One endpoint with
addressWebServiceURL/ISHWS/Wcf/API25/Application.svcandcontractApplication25ServiceReference.Application - One endpoint with
addressWebServiceURL/ISHWS/Wcf/API25/User.svcandcontractUser25ServiceReference.User
- One endpoint with
- The Content Manager web services are available via
- Inside the code you should now be able to write something such as:
using System; using System.ServiceModel; using System.Text; namespace MyMetadataWithAFDS { class Program { static void Main(string[] args) { try { // Create proxy instance Application25ServiceReference.ApplicationClient applicationClient = new Application25ServiceReference.ApplicationClient(); // Optionally specify manual credentials //applicationClient.ClientCredentials.Windows.ClientCredential.Domain = Domain; //applicationClient.ClientCredentials.Windows.ClientCredential.UserName = UserName; //applicationClient.ClientCredentials.Windows.ClientCredential.Password = Password; // Execute the GetVersion call string version = applicationClient.GetVersion(); Console.WriteLine(version); // Create proxy instance User25ServiceReference.UserClient userClient = new User25ServiceReference.UserClient(); // Optionally specify manual credentials //userClient.ClientCredentials.Windows.ClientCredential.Domain = Domain; //userClient.ClientCredentials.Windows.ClientCredential.UserName = UserName; //userClient.ClientCredentials.Windows.ClientCredential.Password = Password; // Create requested metadata xml string xmlRequestedMetadata = "<ishfields>" + "<ishfield name='USERNAME' level='none'/>" + "<ishfield name='FISHUSERDISPLAYNAME' level='none'/>" + "<ishfield name='FISHEMAIL' level='none'/>" + "<ishfield name='FUSERGROUP' level='none'/>" + "<ishfield name='FISHUSERROLES' level='none' ishvaluetype='element'/>" + "<ishfield name='FISHEXTERNALID' level='none'/>" + "</ishfields>"; // Execute the GetMyMetadata call string xmlObjectList = userClient.GetMyMetadata(xmlRequestedMetadata); Console.WriteLine(xmlObjectList); } //Catch all Application25 server exceptions that are generated after the request has been validated on the server and executes. catch (FaultException<Application25ServiceReference.InfoShareFault> fex) { Console.WriteLine("API25 FaultException: {0}", fex); Console.WriteLine("Action: {0}", fex.Action); Console.WriteLine("Reason: {0}", fex.Reason); Console.WriteLine("Description: {0}", fex.Detail.Description); Console.WriteLine("InfoShareErrorNumber: {0}", fex.Detail.InfoShareErrorNumber); Console.WriteLine("Origin: {0}", fex.Detail.Origin); Console.WriteLine("XMLDetail: {0}", fex.Detail.XMLDetail); } //Catch all User25 server exceptions that are generated after the request has been validated on the server and executes. catch (FaultException<User25ServiceReference.InfoShareFault> fex) { Console.WriteLine("User25 FaultException: {0}", fex); Console.WriteLine("Action: {0}", fex.Action); Console.WriteLine("Reason: {0}", fex.Reason); Console.WriteLine("Description: {0}", fex.Detail.Description); Console.WriteLine("InfoShareErrorNumber: {0}", fex.Detail.InfoShareErrorNumber); Console.WriteLine("Origin: {0}", fex.Detail.Origin); Console.WriteLine("XMLDetail: {0}", fex.Detail.XMLDetail); } //Catch all server exception that are generated before the request has been validated on the server and executes. //e.g. Token validation catch (FaultException fex) { Console.WriteLine("FaultException: {0}", fex); Console.WriteLine("Action: {0}", fex.Action); Console.WriteLine("Reason: {0}", fex.Reason); } //Catch the test catch (Exception ex) { Console.WriteLine("Exception: {0}", ex); } finally { Console.WriteLine("Press any key..."); Console.ReadLine(); } } } }