title: Error serializing the security token when switching from ADFS STS to a custom STS
body: <p>I have a web app (named MyWebClient) calling a service (named MyService) over http. MyService accepts an issued token from ADFS and it is working fine. I want to switch from ADFS to MyCustomSTS (using WIF 4.5). I expect that MyWebClient code won't be changed but I get an error.</p>
  
  <p>The code to Ping to MyService as below</p>
  
  <pre><code>var token = GetSecurityToken();
  
  var binding = new WS2007FederationHttpBinding(WSFederationHttpSecurityMode.Message, false);
  
  var messageSecurity = serviceBinding.Security.Message;
  
  messageSecurity.IssuedTokenType = TokenTypes.Saml11TokenProfile11;
  
  messageSecurity.IssuerAddress = Constants.StsAddressX509;
  
  messageSecurity.IssuedKeyType = SecurityKeyType.SymmetricKey;
  
  messageSecurity.IssuerBinding = stsBinding; 
  
  var factory = new ChannelFactory&lt;IMyService&gt;(binding, new EndpointAddress("https://mylocalhost/MyService.svc")); 
  
  factory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode =
                   X509CertificateValidationMode.None; 
  
  factory.Credentials.ServiceCertificate.Authentication.RevocationMode =
   X509RevocationMode.NoCheck;
  
  
   var service = factory.CreateChannelWithIssuedToken(token);
  
   var result = service.Ping();
  </code></pre>
  
  <p>I have (what looks like) a valid token from the STS.</p>
  
  <p>However, it throws an exception in the call to Ping to MyService, as follows:</p>
  
  <pre><code>There was an error serializing the security token. Please see the
   inner exception for more details. Server stack trace: at
   System.ServiceModel.Security.WSSecurityTokenSerializer.WriteTokenCore(XmlWriter
   writer, SecurityToken token) at
   System.ServiceModel.Security.SendSecurityHeader.OnWriteHeaderContents(XmlDictionaryWriter
   writer, MessageVersion messageVersion) at
   System.ServiceModel.Channels.MessageHeader.WriteHeader(XmlDictionaryWriter
   writer, MessageVersion messageVersion) at
   System.ServiceModel.Security.SecurityAppliedMessage.OnWriteMessage(XmlDictionaryWriter
   writer) at ...
  </code></pre>
  
  <p>After searching around internet, I was suggested to change code a bit to add </p>
  
  <pre><code>factory.Credentials.UseIdentityConfiguration = true;
  </code></pre>
  
  <p>and it works fine again.</p>
  
  <p>My question here is why it was working fine with ADFS without "UseIdentityConfiguration"? Is there anyway to fix it on MyCustomSTS but changing client code? </p>
  
  <p>The reason is that I have a lot of client apps like MyWebClient so changing their code would be a problem to me.</p>
  
  <p>Thanks to you for all of your help and replies. I am really appreciated it.</p>