title: ADFS SSO SAML Windows Integrated authentication does not work
body: <p>The project we are working now is Single Sign On via ADFS using SAML Token.<br>
  <strong>The basic rule this project should follow is the following:</strong><br>
      1. Agent logs in to windows using his\her credentials.<br>
      2. Agent logs in to a web application (Relying Party)<br>
      3. The web application should redirect to the STS in the ADFS (Active Directory is the Identity Provider) and login using the credentials which the agent used in his\her windows authentication (seamless authentication).<br>
      4. Therefore the STS login page <strong>should not appear</strong> and the user should be authenticated<br>
      5. After that claims and security token should be received in order for us to authorize the agent  </p>
  
  <p><strong>Actual Result:</strong><br>
      1. Redirection is done for the first time and authentication is required again (IE authentication page and Firefox\Chrome authentication page).<br>
  <a href="https://i.stack.imgur.com/17rrg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/17rrg.png" alt="enter image description here"></a><br>
  <a href="https://i.stack.imgur.com/7BKIK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7BKIK.png" alt="enter image description here"></a></p>
  
  <ol start="2">
  <li>Can do authentication with all kind of domain users and not only the windows authenticated user.  </li>
  <li>After first login to the sts login page, authentication is not required again. However we DON’T want second authentication. Only at windows logon (true only to IE).  </li>
  </ol>
  
  <p><strong>Configured Environment:</strong><br>
      1. Domain Controller + ADFS server 3.0 on the same machine (Win2k12R2)<br>
      2. Web Application machines (Win2k12 + IIS8.5)<br>
      3. The machines are on the same domain  </p>
  
  <p><strong>ADFS Configuration:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/r98wo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/r98wo.png" alt="enter image description here"></a><br>
  <a href="https://i.stack.imgur.com/xvIeC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xvIeC.png" alt="enter image description here"></a><br>
  <a href="https://i.stack.imgur.com/VCqP4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VCqP4.png" alt="enter image description here"></a>  </p>
  
  <p><strong>Relying Party Configuration:</strong>  </p>
  
  <p><a href="https://i.stack.imgur.com/EtvvG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EtvvG.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/HcNRE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HcNRE.png" alt="enter image description here"></a><br>
  <a href="https://i.stack.imgur.com/p51td.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/p51td.png" alt="enter image description here"></a>  </p>
  
  <p><strong>IE Configuration:</strong><br>
  <a href="https://i.stack.imgur.com/NA8EU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NA8EU.png" alt="enter image description here"></a><br>
  <a href="https://i.stack.imgur.com/iFcmr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iFcmr.png" alt="enter image description here"></a><br>
  <a href="https://i.stack.imgur.com/PQAcw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PQAcw.png" alt="enter image description here"></a>  </p>
  
  <p><strong>Web App Configuration:<br>
  Authentication:</strong>  </p>
  
  <p><a href="https://i.stack.imgur.com/6REbL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6REbL.png" alt="enter image description here"></a>
  <strong>ASP.Net Project:<br>
  Web Config File:</strong>  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!--
    For more information on how to configure your ASP.NET application, please visit
    http://go.microsoft.com/fwlink/?LinkId=169433
    --&gt;
  &lt;configuration&gt;
    &lt;configSections&gt;
      &lt;!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --&gt;
      &lt;section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /&gt;
      &lt;section name="system.identityModel" type="System.IdentityModel.Configuration.SystemIdentityModelSection, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /&gt;
      &lt;section name="system.identityModel.services" type="System.IdentityModel.Services.Configuration.SystemIdentityModelServicesSection, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /&gt;
    &lt;/configSections&gt;
    &lt;connectionStrings&gt;
      &lt;add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-TestApp-20150730141753;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-TestApp-20150730141753.mdf" /&gt;
    &lt;/connectionStrings&gt;
    &lt;location path="FederationMetadata"&gt;
      &lt;system.web&gt;
        &lt;authorization&gt;
          &lt;allow users="*" /&gt;
        &lt;/authorization&gt;
      &lt;/system.web&gt;
    &lt;/location&gt;
    &lt;system.web&gt;
      &lt;authorization&gt;
        &lt;deny users="?" /&gt;
      &lt;/authorization&gt;
      &lt;authentication mode="None" /&gt;
      &lt;compilation debug="true" targetFramework="4.5" /&gt;
      &lt;httpRuntime targetFramework="4.5" /&gt;
      &lt;pages&gt;
        &lt;namespaces&gt;
          &lt;add namespace="System.Web.Optimization" /&gt;
        &lt;/namespaces&gt;
      &lt;controls&gt;&lt;add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" /&gt;&lt;/controls&gt;&lt;/pages&gt;
      &lt;!--&lt;authentication mode="Forms"&gt;
        &lt;forms loginUrl="~/Account/Login" timeout="2880" defaultUrl="~/" /&gt;
      &lt;/authentication&gt;--&gt;
      &lt;profile defaultProvider="DefaultProfileProvider"&gt;
        &lt;providers&gt;
          &lt;add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" /&gt;
        &lt;/providers&gt;
      &lt;/profile&gt;
      &lt;membership defaultProvider="DefaultMembershipProvider"&gt;
        &lt;providers&gt;
          &lt;add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" /&gt;
        &lt;/providers&gt;
      &lt;/membership&gt;
      &lt;roleManager defaultProvider="DefaultRoleProvider"&gt;
        &lt;providers&gt;
          &lt;add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" /&gt;
        &lt;/providers&gt;
      &lt;/roleManager&gt;
      &lt;!--
              If you are deploying to a cloud environment that has multiple web server instances,
              you should change session state mode from "InProc" to "Custom". In addition,
              change the connection string named "DefaultConnection" to connect to an instance
              of SQL Server (including SQL Azure and SQL  Compact) instead of to SQL Server Express.
        --&gt;
      &lt;sessionState mode="InProc" customProvider="DefaultSessionProvider"&gt;
        &lt;providers&gt;
          &lt;add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" /&gt;
        &lt;/providers&gt;
      &lt;/sessionState&gt;
  
    &lt;/system.web&gt;
    &lt;system.webServer&gt;
      &lt;modules&gt;
        &lt;!--&lt;add name="WSFederationAuthenticationModule" type="System.IdentityModel.Services.WSFederationAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" /&gt;--&gt;
        &lt;add name="FixedWSFederationAuthenticationModule" type="TestApp.FixedWSFederationAuthenticationModule, TestApp" preCondition="managedHandler" /&gt;
        &lt;add name="SessionAuthenticationModule" type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" /&gt;
      &lt;/modules&gt;
    &lt;/system.webServer&gt;
    &lt;system.identityModel&gt;
      &lt;identityConfiguration saveBootstrapContext="true"&gt;
        &lt;!-- The identity configuration. No name means default configuration which is always used for passive federation scenarios. see federationConfiguration element --&gt;
        &lt;audienceUris&gt;
          &lt;add value="https://ccsp12.pj12.loc/testapp" /&gt;
        &lt;/audienceUris&gt;
        &lt;issuerNameRegistry type="System.IdentityModel.Tokens.ConfigurationBasedIssuerNameRegistry, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt;
          &lt;trustedIssuers&gt;
            &lt;add thumbprint="91992FCF8B03FF9BD98A259FE93B92620E9DD89A" name="http://sts.pj12.loc/adfs/services/trust" /&gt;
          &lt;/trustedIssuers&gt;
        &lt;/issuerNameRegistry&gt;
        &lt;certificateValidation certificateValidationMode="None" /&gt;
      &lt;/identityConfiguration&gt;
    &lt;/system.identityModel&gt;
    &lt;system.identityModel.services&gt;
      &lt;federationConfiguration&gt; &lt;!-- Configures the WSFederationAuthenticationModule (WSFAM) and the SessionAuthenticationModule (SAM) when using federated authentication through the WS-Federation protocol --&gt;
        &lt;cookieHandler requireSsl="false" /&gt;
        &lt;!-- passiveRedirectEnabled true means that a relaying party (test app) instead of having its own login page, it will redirect to the sts issuer for authentication and the sts will reply to the relaying party --&gt;
        &lt;!-- Due to WSFederationAuthenticationModule bug, the relaying party address must be with '/' at the end --&gt;
        &lt;wsFederation passiveRedirectEnabled="true" issuer="https://sts.pj12.loc/adfs/ls/" realm="https://ccsp12.pj12.loc/testapp/" reply="https://ccsp12.pj12.loc/testapp/" requireHttps="true" /&gt;
      &lt;/federationConfiguration&gt;
    &lt;/system.identityModel.services&gt;
    &lt;!--&lt;runtime&gt;
      &lt;assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name="DotNetOpenAuth.Core" publicKeyToken="2780ccd10d57b246" /&gt;
          &lt;bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" /&gt;
        &lt;/dependentAssembly&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name="DotNetOpenAuth.AspNet" publicKeyToken="2780ccd10d57b246" /&gt;
          &lt;bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" /&gt;
        &lt;/dependentAssembly&gt;
      &lt;/assemblyBinding&gt;
    &lt;/runtime&gt;--&gt;
    &lt;entityFramework&gt;
      &lt;defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /&gt;
    &lt;/entityFramework&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p><strong>C# Code:</strong>  </p>
  
  <pre><code>namespace TestApp
  {
      public partial class _Default : Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
              // local variables
              string claimsTypes = string.Empty;
              string claimsValues = string.Empty;
              string claimsValueTypes = string.Empty;
              string claimsSubjectNames = string.Empty;
              string claimsIssuers = string.Empty;
              // initialize claims and identity
              ClaimsPrincipal claimsPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal;
              ClaimsIdentity claimsIdentity = Thread.CurrentPrincipal.Identity as ClaimsIdentity;
              BootstrapContext bootstrapContext =
              ClaimsPrincipal.Current.Identities.First().BootstrapContext
                                                      as BootstrapContext;
  
              if (claimsPrincipal != null)
              {
                  signedIn.Text = "You are signed in.";
  
                  foreach (Claim claim in claimsPrincipal.Claims)
                  {
                      claimsTypes = string.Concat(claimsTypes, "; ", claim.Type);
                      claimsValues = string.Concat(claimsValues, "; ", claim.Value);
                      claimsValueTypes = string.Concat(claimsValueTypes, "; ", claim.ValueType);
                      claimsSubjectNames = string.Concat(claimsSubjectNames, "; ", claim.Subject.Name);
                      claimsIssuers = string.Concat(claimsIssuers, "; ", claim.Issuer);
                  }
  
                  //claims principals
                  claimType.Text = claimsTypes;
                  claimValue.Text = claimsValues;
                  claimValueType.Text = claimsValueTypes;
                  claimSubjectName.Text = claimsSubjectNames;
                  claimIssuer.Text = claimsIssuers;
  
                  // ClaimsIdentity
                  isUserAuthenticated.Text = claimsIdentity.IsAuthenticated.ToString();
                  authenticationType.Text = claimsIdentity.AuthenticationType;
                  claimName.Text = claimsIdentity.Name;
  
                  // Token
                  // known bug : http://stackoverflow.com/questions/13514553/wif-4-5-bootstrapcontext-security-token-null
                  SecurityToken token = null;
                  if (bootstrapContext.SecurityToken != null)
                  {
                      token = bootstrapContext.SecurityToken;
                  }
                  else if (!bootstrapContext.Token.Equals(string.Empty))
                  {
                      var handlers = FederatedAuthentication.FederationConfiguration.IdentityConfiguration.SecurityTokenHandlers;
                      token = handlers.ReadToken(new XmlTextReader(new StringReader(bootstrapContext.Token)));
                  }
  
                  SamlSecurityToken sst = token as SamlSecurityToken;
                  tokenId.Text = sst.Id;
                  tokenAssertionId.Text = sst.Assertion.AssertionId;
                  tokenIssuer.Text = sst.Assertion.Issuer;
  
              }
              else
              {
                  signedIn.Text = "You are not signed in.";
              }
          }
      }  
  </code></pre>