Client Example: No Final URI

The following shows how to create a client for handling SAML protocol without a final URI:

Copy
webBrowser.Navigate("http://host/CherwellApi/saml/login.cshtml");

private void OnNavigated(object sender, NavigationEventArgs e)
{
 dynamic doc = webBrowser.Document;

 // Look for SAMLPOSTBACK meta tag - this is how we know we have the correct page
 var elements = doc.getElementsByTagName("meta");
 foreach (HTMLMetaElement element in elements)
 {
  if (string.Compare(element.name, "SAMLPOSTBACK", StringComparison.OrdinalIgnoreCase) == 0)
  {
// if this were a popup dialog - could hide the dialog here and just return the content to a calling page

   // the json data is stored in the metadata
   string samlAssertionData = element.content;
   LoginToRest(samlAssertionData);
  }
 }
}

private void LoginToRest(string samlAssertionData)
{
 var samlAssertion = JObject.Parse(samlAssertionData);
 // Setup the default header
 var baseuri = "http://host/CherwellApi/";
 var apiversion = "1";
 var apiClient = new ApiClient(baseuri);

 string clientid = "your client id";
 string user = samlAssertion.GetValue("NameId", StringComparison.InvariantCultureIgnoreCase).ToString();
 string password = samlAssertion.GetValue("Ticket", StringComparison.InvariantCultureIgnoreCase).ToString();


 try
 {
  // Login to REST service
  ServiceApi service = new ServiceApi(apiClient);
  TokenResponse tokenResponse = service.ServiceToken("password", clientid, user, password, string.Empty, "saml");
  // Setup the default header
  apiClient.DefaultHeader.Add("Authorization", "Bearer " + tokenResponse.AccessToken);

  // Create search results request
  SearchResultsRequest searchResults = new SearchResultsRequest { BusObId = "6dd53665c0c24cab86870a21cf6434ae" };

  // Query for data
  SearchesApi searches = new SearchesApi(apiClient);
  SearchResultsResponse response = searches.SearchesGetSearchResultsAdHocV1(searchResults);
  string message = "There are " + response.TotalRows + " incidents";
  MessageBox.Show(message);
 }
 catch (ApiException ex)
 {
  MessageBox.Show(ex.Message);
 }
}