title: Configuring Windows Identity Foundation from code
body: <p>I'm experimenting with "configuration-less WIF", where I want to accept a SAML2 token that is generated by Windows Azure's AppFabric STS.</p>
  
  <p>What I'm doing is parsing checking the current request for token information, like so:</p>
  
  <pre><code>        if (Request.Form.Get(WSFederationConstants.Parameters.Result) != null)
          {
              SignInResponseMessage message = 
                  WSFederationMessage.CreateFromFormPost(System.Web.HttpContext.Current.Request) as SignInResponseMessage;
  
              var securityTokenHandlers = SecurityTokenHandlerCollection.CreateDefaultSecurityTokenHandlerCollection();                    
  
              XmlTextReader xmlReader = new XmlTextReader(
                  new StringReader(message.Result));
  
              SecurityToken token = securityTokenHandlers.ReadToken(xmlReader);
  
              if (token != null)
              {
                  ClaimsIdentityCollection claims = securityTokenHandlers.ValidateToken(token);
                  IPrincipal principal = new ClaimsPrincipal(claims);
              }
          }
  </code></pre>
  
  <p>The code above uses the SecurityTokenHandlerCollection.CreateDefaultSecurityTokenHandlerCollection(); colection to verify and handle the SAML token. However: this does not work because obviously the application has not bee nconfigured correctly. How would I specify the follwing configuration from XML programmaticaly on my securityTokenHandlers collection?</p>
  
  <pre><code>  &lt;microsoft.identityModel&gt;
  &lt;service&gt;
    &lt;audienceUris&gt;
      &lt;add value="http://www.someapp.net/" /&gt;
    &lt;/audienceUris&gt;
    &lt;federatedAuthentication&gt;
      &lt;wsFederation passiveRedirectEnabled="true" issuer="https://rd-test.accesscontrol.appfabriclabs.com/v2/wsfederation" realm="http://www.thisapp.net" requireHttps="false" /&gt;
      &lt;cookieHandler requireSsl="false" /&gt;
    &lt;/federatedAuthentication&gt;
    &lt;applicationService&gt;
      &lt;claimTypeRequired&gt;
        &lt;claimType type="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name" optional="true" /&gt;
        &lt;claimType type="http://schemas.microsoft.com/ws/2008/06/identity/claims/role" optional="true" /&gt;
      &lt;/claimTypeRequired&gt;
    &lt;/applicationService&gt;
    &lt;issuerNameRegistry type="Microsoft.IdentityModel.Tokens.ConfigurationBasedIssuerNameRegistry, Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"&gt;
      &lt;trustedIssuers&gt;
        &lt;add thumbprint="XYZ123" name="https://somenamespace.accesscontrol.appfabriclabs.com/" /&gt;
      &lt;/trustedIssuers&gt;
    &lt;/issuerNameRegistry&gt;
  &lt;/service&gt;
  </code></pre>
  
  <p></p>