How to add SAML token to SOAP Request in C#

785 Views Asked by At

I try to call a SOAP service by authenticating myself with a SAML token. First I get a SAML token for the target by calling the ADFS:

var stsEndpoint = "https://ADFS.EXAMPLE/adfs/services/trust/13/kerberosmixed";
var reliantPartyUri = "http://reliant-party.com";

var binding = new CustomBinding();
var ssbe = SecurityBindingElement.CreateKerberosOverTransportBindingElement();
ssbe.DefaultAlgorithmSuite = SecurityAlgorithmSuite.Basic128;
ssbe.MessageSecurityVersion = MessageSecurityVersion.WSSecurity10WSTrust13WSSecureConversation13WSSecurityPolicy12BasicSecurityProfile10;            
binding.Elements.Add(ssbe);

binding.Elements.Add(new TextMessageEncodingBindingElement());
binding.Elements.Add(new HttpsTransportBindingElement());

var factory = new WSTrustChannelFactory(binding, new EndpointAddress(stsEndpoint));
factory.TrustVersion = TrustVersion.WSTrust13;

var rst = new RequestSecurityToken
{
    RequestType = RequestTypes.Issue,
    AppliesTo = new EndpointReference(reliantPartyUri)
};

var channel = factory.CreateChannel();
var token = channel.Issue(rst);

Now I want to use the SAML token to call a secured SOAP webservice. How is it possible to add the token? I've tried the following without success (the soap request does not contain any token):

//Service was created by an imported WSDL File - Methods and Types renamed for StackOverflow
var request = new Service.WsdlCreatedRequest();
[...]

var wsdlClient = new Service.WsdlCreatedService("HTTPS_Port");            
var wsdlChannel = wsdlClient.ChannelFactory.CreateChannelWithIssuedToken(token);      
wsdlChannel.WsdlCreatedMethod(request);

Any idea how to use the token in the request?

0

There are 0 best solutions below