title: SAML2 using Thinktecture and WCF
body: <p>I'm setting up a SAML2 STS using Thinktecture IdentityServer and a WCF Service.
  The idea is to have Thinktecture supply the Assertions that are then used in the WCF Service.</p>
  
  <p>Now, I have it running all fine with SAML 11 Assertions - urn:oasis:names:tc:SAML:1.0:assertion</p>
  
  <p>The assertions are something like this:</p>
  
  <pre><code>&lt;saml:Assertion MajorVersion="1" MinorVersion="1" AssertionID="_4e06770b-7fa0-425f-a4f3-263710f135d7" Issuer="https://localhost:123" IssueInstant="2014-01-18T16:16:38.099Z" xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion"&gt;
  - &lt;saml:Conditions NotBefore="2014-01-18T16:16:37.662Z" NotOnOrAfter="2014-01-18T16:26:37.662Z"&gt;
  - &lt;saml:AudienceRestrictionCondition&gt;
    &lt;saml:Audience&gt;https://localhost:44300/&lt;/saml:Audience&gt; 
    &lt;/saml:AudienceRestrictionCondition&gt;
    &lt;/saml:Conditions&gt;
  - &lt;saml:AttributeStatement&gt;
  - &lt;saml:Subject&gt;
  - &lt;saml:SubjectConfirmation&gt;
    &lt;saml:ConfirmationMethod&gt;urn:oasis:names:tc:SAML:1.0:cm:bearer&lt;/saml:ConfirmationMethod&gt; 
    &lt;/saml:SubjectConfirmation&gt;...
  </code></pre>
  
  <p>Now, the requirement is to use SAML2, not the previous version. So changing the Token type in Thinktecture is simple and the new Assertion generates fine and get's picked by the client just fine.</p>
  
  <p>New SAML2 Assertion:</p>
  
  <pre><code>&lt;Assertion ID="_f9c48402-857a-4cc3-8525-1af307ef3a01" IssueInstant="2014-01-18T16:32:18.038Z" Version="2.0" xmlns="urn:oasis:names:tc:SAML:2.0:assertion"&gt;
    &lt;Issuer&gt;https://localhost:123&lt;/Issuer&gt; 
  - &lt;Signature xmlns="http://www.w3.org/2000/09/xmldsig#"&gt;
  - &lt;SignedInfo&gt;
    &lt;CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" /&gt; 
    &lt;SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" /&gt; 
  - &lt;Reference URI="#_f9c48402-857a-4cc3-8525-1af307ef3a01"&gt;
  - &lt;Transforms&gt;
    &lt;Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /&gt; 
    &lt;Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" /&gt; 
    &lt;/Transforms&gt;
    &lt;DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" /&gt; 
    &lt;DigestValue&gt;Nib1Bs9jy13unYShm08EcncqFsOS5IwVuWQ4tGoe4Pg=&lt;/DigestValue&gt; 
    &lt;/Reference&gt;
    &lt;/SignedInfo&gt; ...
  </code></pre>
  
  <p>But when we then try to consume the WCF by passing the new asertion we get the exception:</p>
  
  <ul>
  <li>XmlException: Cannot read the token from the 'Assertion' element with the 'urn:oasis:names:tc:SAML:2.0:assertion' namespace for BinarySecretSecurityToken, with a '' ValueType. If this element is expected to be valid, ensure that security is configured to consume tokens with the name, namespace and value type specified</li>
  </ul>
  
  <p>Here's an excerpt of WCF web.config:</p>
  
  <pre><code>    ...
      &lt;services&gt;
            &lt;service name="testsys.RFD.FormManager.RFDFormManagerService" behaviorConfiguration="testsys.RFD.FormManagerBehavior"&gt;
              &lt;endpoint address="" binding="ws2007FederationHttpBinding" bindingConfiguration="ws2007FederationHttpBinding" contract="FormManager_PortType" /&gt;
            &lt;/service&gt;
          &lt;/services&gt;
          &lt;protocolMapping&gt;
            &lt;!--&lt;add scheme="http" binding="ws2007FederationHttpBinding" /&gt;
            &lt;add scheme="http" binding="ws2007FederationHttpBinding" /&gt;--&gt;
            &lt;add binding="ws2007FederationHttpBinding" scheme="https" /&gt;
            &lt;add binding="wsHttpBinding" scheme="https" /&gt;
          &lt;/protocolMapping&gt;
          &lt;bindings&gt;
            &lt;basicHttpBinding&gt;
              &lt;binding name="BasicHttpBinding_ILoggerService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxBufferSize="65536" maxReceivedMessageSize="65536" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true" messageEncoding="Text"&gt;
                &lt;readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /&gt;
                &lt;security mode="None"&gt;
                  &lt;transport clientCredentialType="None" proxyCredentialType="None" realm="" /&gt;
                  &lt;message clientCredentialType="UserName" algorithmSuite="Default" /&gt;
                &lt;/security&gt;
              &lt;/binding&gt;
            &lt;/basicHttpBinding&gt;
            &lt;ws2007FederationHttpBinding&gt;
              &lt;binding name="ws2007FederationHttpBinding"&gt;
                &lt;security mode="TransportWithMessageCredential"&gt;
                  &lt;message establishSecurityContext="false" issuedKeyType="BearerKey"&gt;
                    &lt;!-- issuedTokenType="http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0"--&gt;
                    &lt;issuerMetadata address="https://localhost:123/adfs/services/trust/mex" /&gt;
                  &lt;/message&gt;
                &lt;/security&gt;
              &lt;/binding&gt;
              &lt;binding name=""&gt;
                &lt;security mode="TransportWithMessageCredential"&gt;
                  &lt;message&gt;
                    &lt;issuerMetadata address="https://localhost:123/adfs/services/trust/mex" /&gt;
                  &lt;/message&gt;
                &lt;/security&gt;
              &lt;/binding&gt;
            &lt;/ws2007FederationHttpBinding&gt;
            &lt;wsHttpBinding&gt;
              &lt;binding name="RFD_Binding_HTTP" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"&gt;
                &lt;readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /&gt;
                &lt;reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" /&gt;
                &lt;security mode="None"&gt;
                  &lt;transport clientCredentialType="None" proxyCredentialType="None"&gt;
                    &lt;extendedProtectionPolicy policyEnforcement="Never" /&gt;
                  &lt;/transport&gt;
                  &lt;message negotiateServiceCredential="false" establishSecurityContext="false" /&gt;
                &lt;/security&gt;
              &lt;/binding&gt;
              &lt;binding name="wsHttpBinding_IRFDFormManager" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"&gt;
                &lt;security mode="Transport"&gt;
                  &lt;transport clientCredentialType="None" /&gt;
                &lt;/security&gt;
              &lt;/binding&gt;
              &lt;binding name="HttpBinding_IRFDFormManager" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"&gt;
                &lt;security mode="None" /&gt;
              &lt;/binding&gt;
              &lt;binding name="WSHttpBinding_IRESTFormManager"&gt;
                &lt;security mode="None" /&gt;
              &lt;/binding&gt;
            &lt;/wsHttpBinding&gt;
      ...
          &lt;behavior name="testsys.RFD.FormManagerBehavior"&gt;
            &lt;serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" httpsGetUrl="https://localhost:44300/RFDFormManager.svc" /&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="true" /&gt;
            &lt;useRequestHeadersForMetadataAddress /&gt;
            &lt;serviceCredentials&gt;
              &lt;serviceCertificate findValue="65A34BC197C2E1334D971613521D53D3B5B807FF" storeLocation="LocalMachine" storeName="My" x509FindType="FindByThumbprint" /&gt;
              &lt;issuedTokenAuthentication certificateValidationMode="None"&gt;
                &lt;!-- The X.509 certificate CN=localhost chain building failed. The certificate that was used has a trust chain that cannot be verified. Replace the certificate or change the certificateValidationMode. A certificate chain processed, but terminated in a root certificate which is not trusted by the trust provider --&gt;
                &lt;allowedAudienceUris&gt;
                  &lt;add allowedAudienceUri="https://localhost:44300/" /&gt;
                &lt;/allowedAudienceUris&gt;
              &lt;/issuedTokenAuthentication&gt;
              &lt;clientCertificate&gt;
                &lt;authentication certificateValidationMode="None" /&gt;
              &lt;/clientCertificate&gt;
            &lt;/serviceCredentials&gt;
            &lt;serviceAuthorization principalPermissionMode="Always" /&gt;
            &lt;serviceSecurityAudit auditLogLocation="Application" suppressAuditFailure="true" serviceAuthorizationAuditLevel="Failure" messageAuthenticationAuditLevel="Failure" /&gt;
          &lt;/behavior&gt;
  ...
  &lt;system.identityModel&gt;
      &lt;identityConfiguration saveBootstrapContext="true"&gt;
        &lt;securityTokenHandlers&gt;
          &lt;add type="System.IdentityModel.Tokens.Saml2SecurityTokenHandler, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /&gt;
        &lt;/securityTokenHandlers&gt;
        &lt;audienceUris&gt;
          &lt;!-- &lt;add value="https://localhost:44300/" /&gt;--&gt;
          &lt;add value="https://localhost:44300/" /&gt;
        &lt;/audienceUris&gt;
        &lt;!--Commented by Identity and Access VS Package--&gt;
        &lt;!--&lt;issuerNameRegistry type="System.IdentityModel.Tokens.ValidatingIssuerNameRegistry, System.IdentityModel.Tokens.ValidatingIssuerNameRegistry"&gt;&lt;authority name="https://localhost:123"&gt;&lt;keys&gt;&lt;add thumbprint="7C399B2EA330F507902C81280E77407242FF6916" /&gt;&lt;/keys&gt;&lt;validIssuers&gt;&lt;add name="https://localhost:123" /&gt;&lt;/validIssuers&gt;&lt;/authority&gt;&lt;/issuerNameRegistry&gt;--&gt;
        &lt;!--certificationValidationMode set to "None" by the the Identity and Access Tool for Visual Studio. For development purposes.--&gt;
        &lt;!--&lt;certificateValidation certificateValidationMode="None" /&gt;--&gt;
        &lt;issuerNameRegistry type="System.IdentityModel.Tokens.ConfigurationBasedIssuerNameRegistry, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt;
          &lt;trustedIssuers&gt;
            &lt;!--&lt;add thumbprint="7C399B2EA330F507902C81280E77407242FF6916" name="https://localhost:123" /&gt;--&gt;
            &lt;add thumbprint="65A34BC197C2E1334D971613521D53D3B5B807FF" name="https://localhost:123" /&gt;
            &lt;add thumbprint="15abb1aef64def860ca543f54fc8dee222d49b86" name="https://localhost:123" /&gt;
          &lt;/trustedIssuers&gt;
        &lt;/issuerNameRegistry&gt;
        &lt;!--certificationValidationMode set to "None" by the the Identity and Access Tool for Visual Studio. For development purposes.--&gt;
        &lt;certificateValidation certificateValidationMode="None" revocationMode="NoCheck" /&gt;
        &lt;!--revocationMode="NoCheck" should not be used to prod.--&gt;
      &lt;/identityConfiguration&gt;
    &lt;/system.identityModel&gt;
    &lt;system.identityModel.services&gt;
      &lt;federationConfiguration&gt;
        &lt;cookieHandler requireSsl="true" /&gt;
        &lt;!--&lt;wsFederation passiveRedirectEnabled="true" issuer="https://localhost:123/issue/wsfed" realm="https://localhost:44300" reply="https://localhost:44300" requireHttps="false" /&gt;--&gt;
        &lt;wsFederation passiveRedirectEnabled="true" issuer="https://localhost:123/issue/wsfed" realm="https://localhost:44300/" reply="https://localhost:44300/" requireHttps="true" /&gt;
      &lt;/federationConfiguration&gt;
    &lt;/system.identityModel.services&gt;
    ...
  </code></pre>
  
  <p>So what we're doing wrong ?</p>