This example is based on code in the VimConnection.cs sample file. This file is located in the following vCloud Suite SDK for .NET directory: client/samples/src/Samples/vmware/vcloud/suite/samples/common.

This example uses the steps that are described in the Create a Web Services Session procedure.

using System;
using System.IdentityModel.Tokens;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using vmware.vim25;

// Wrap a SAML token for the Web Services authentication.
var token = ssoConnection.SamlTokenXml;
var created = DateTime.Parse(ssoConnection.RequestSecurityTokenResponse.Lifetime.Created.Value;
var expires = DateTime.Parse(ssoConnection.RequestSecurityTokenResponse.Lifetime.Expires.Value;
var securityToken = new GenericXmlSecurityToken(token, null, created, expires, null, null, null);

// Create a communication port with the SAML token context.
var binding = GetCustomBinding(SecurityKeyType.BearerKey);
var endpoint = new EndpointAddress(vcenterUrl);
var factory = new ChannelFactory<VimPortType>(binding, endpoint);
factory.Endpoint.Behaviors.Add(new ClientViaBehavior(new Uri(vcenterUrl));
factory.Credentials.SupportInteractive = false;
var vimPort = factory.CreateChannelWithIssuedToken(securityToken);

// Create a reference to the ServiceInstance managed object. 
var serviceRef = new ManagedObjectReference();
serviceRef.type = “ServiceInstance”;
serviceRef.value = “ServiceInstance”;

// Retrieve a Service Content data object.
// ServiceContent provides access to the vSphere Web Services.

var serviceContent = vimPort.RetrieveServiceContent(serviceRef);

// Create a session to the Web Service by using the SAML token.
var mySession = vimPort.LoginByToken(serviceContent.sessionManager);