title: How to pass a certificate to WSTrust to get Saml Token
body: <p>Here is an example of getting tokem using WSTrustChannelFactory. <a href="http://leastprivilege.com/2012/11/16/wcf-and-identity-in-net-4-5-external-authentication-with-ws-trust/" rel="nofollow">From here</a>.</p>
  
  <pre><code>var stsBinding = new WS2007HttpBinding();
  stsBinding.Security.Mode = SecurityMode.TransportWithMessageCredential;
  stsBinding.Security.Message.EstablishSecurityContext = false;
  stsBinding.Security.Message.NegotiateServiceCredential = false;
  stsBinding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
  
  
  WSTrustChannelFactory trustChannelFactory = new WSTrustChannelFactory(
      stsBinding
      , new EndpointAddress(tokenurl)
      );
  trustChannelFactory.TrustVersion = System.ServiceModel.Security.TrustVersion.WSTrust13;
  
  X509Store myStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);
  myStore.Open(OpenFlags.ReadOnly);
  X509Certificate2Collection coll = myStore.Certificates.Find(X509FindType.FindBySerialNumber, "MycertSerialNumber", true);
  X509Certificate2 cert = coll[0];
  trustChannelFactory.Credentials.ClientCertificate.Certificate = cert;
  
  WSTrustChannel channel = (WSTrustChannel)trustChannelFactory.CreateChannel();
  
  RequestSecurityToken rst = new RequestSecurityToken(RequestTypes.Issue, keyType);
  rst.AppliesTo = new EndpointAddress(realm);
  RequestSecurityTokenResponse rstr = null;
  rst.TokenType = SecurityTokenTypes.Saml;
  
  SecurityToken token = channel.Issue(rst, out rstr);
  </code></pre>
  
  <p>Now I don't have a username/password but the provider has given me certificate .pfx file.
  How do I pass it to the WSTrushChannelFactory? I have tried using CertificateBinding but no success.</p>
  
  <p>Updated Code above: 11/05/2014:</p>
  
  <p>Getting this error: <strong>ID3242: The security token could not be authenticated or authorized.</strong></p>