title: ClaimsPrincipal not getting saved properly
body: <p>I have an ASP.NET MVC 4 application, in which I'm trying to implement claims-based authentication and authorization.  We have an SSO proxy that inserts certain info about the user (such as username) in the header.  I have implemented my own ClaimsAuthenticationManager, and what I want to do is take the username from the header, create a ClaimsPrinsipal containing the UserName claim, pass it into my ClaimsAuthenticationManager, which will hit the database to figure out what roles the user should have, and populate the principal with role claims.  Then I want to use the transformed claim for the rest of the user's session.  </p>
  
  <p>What I have so far:</p>
  
  <p><strong>Web.config:</strong></p>
  
  <blockquote>
  <pre><code>&lt;configuration&gt;
    ...
    &lt;system.web&gt;
      ...
      &lt;authentication mode="Forms"&gt;
        &lt;forms loginUrl="~/Account/Authorize" timeout="20" /&gt;
      &lt;/authentication&gt;
    &lt;/system.web&gt;
  &lt;/configuration&gt;
  </code></pre>
  </blockquote>
  
  <p><strong>MyClaimsTransformer.cs</strong></p>
  
  <pre><code>public class MyClaimsTransformer : ClaimsAuthenticationManager
  {
      private readonly IAuthenticationUow _authenticationUow;
      private readonly IWebServiceUrls _webServiceUrls;
  
      public MyClaimsTransformer(IAuthenticationUow authenticationUow, IWebServiceUrls webServiceUrls)
      {
          _authenticationUow = authenticationUow;
          _webServiceUrls = webServiceUrls;
      }
  
      public override ClaimsPrincipal Authenticate(string resourceName, ClaimsPrincipal incomingPrincipal)
      {
          if (incomingPrincipal == null || incomingPrincipal.Identity == null || !incomingPrincipal.Identity.IsAuthenticated)
              throw new SecurityException("No claims principal or not authenticated");
  
          var userName = incomingPrincipal.Identity.Name;
  
          if (string.IsNullOrWhiteSpace(userName))
              throw new SecurityException("No user name found");
  
          // Add role claims
  
          return incomingPrincipal;
      }
  }
  </code></pre>
  
  <p><strong>Global.asax.cs</strong></p>
  
  <pre><code>public class MvcApplication : System.Web.HttpApplication
  {
      protected void Application_Start()
      {
          AreaRegistration.RegisterAllAreas();
  
          WebApiConfig.Register(GlobalConfiguration.Configuration);
          FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
          RouteConfig.RegisterRoutes(RouteTable.Routes);
          BundleConfig.RegisterBundles(BundleTable.Bundles);
  
          FederatedAuthentication.FederationConfigurationCreated += FederatedAuthentication_FederationConfigurationCreated;
      }
  
      private void FederatedAuthentication_FederationConfigurationCreated(object sender, System.IdentityModel.Services.Configuration.FederationConfigurationCreatedEventArgs e)
      {
          WebServiceUrls serviceUrls = new WebServiceUrls();
          AppProperties appProperties = new AppProperties();
          serviceUrls.UserNameWebServiceUrl = appProperties.UserNameWebServiceUrl;
          serviceUrls.PointOfContactWebServiceUrl = appProperties.PointOfContactWebServiceUrl;
          e.FederationConfiguration.IdentityConfiguration.ClaimsAuthenticationManager =
              ObjectFactory.With&lt;IWebServiceUrls&gt;(serviceUrls).GetInstance&lt;MyClaimsTransformer&gt;();
      }
  }
  </code></pre>
  
  <p><strong>AccountController.cs</strong></p>
  
  <pre><code>[Authorize]
  public class AccountController : BaseController
  {
      [AllowAnonymous]
      public ActionResult Authorize(string returnUrl)
      {
          string userId = Request.Headers["userid"];
  
          if (Url.IsLocalUrl(returnUrl))
          {
              var claims = new List&lt;Claim&gt;
              {
                  new Claim(ClaimTypes.Name, userId)
              };
  
              var id = new ClaimsIdentity(claims, "Custom", ClaimTypes.Name, ClaimTypes.Role);
              var principal = new ClaimsPrincipal(id);
  
              // call out to registered ClaimsAuthenticationManager
              var claimsAuthManager = FederatedAuthentication.FederationConfiguration.IdentityConfiguration.ClaimsAuthenticationManager;
  
              // set the transformed principal
              var url = Request.Url;
              if (url != null)
              {
                  var newPrincipal = claimsAuthManager.Authenticate(url.AbsolutePath, principal);
                  SetSessionCookie(newPrincipal);
                  Thread.CurrentPrincipal = newPrincipal;
              }
  
              return Redirect(returnUrl);
          }
          throw new InvalidDataException("BAD URL");
      }
  
      private void SetSessionCookie(ClaimsPrincipal incomingPrincipal)
      {
          SessionSecurityToken token = new SessionSecurityToken(incomingPrincipal);
          FederatedAuthentication.SessionAuthenticationModule.WriteSessionTokenToCookie(token);
      }
  }
  </code></pre>
  
  <p>This code runs fine, and when the <code>Thread.CurrentPrincipal = newPrincipal;</code> line gets executed, all the proper claims are in the principal.  But when I use my custom ClaimsAuthorizationManager to check the claims, none of them are there (also, the Principal is no longer Authenticated).  It just re-directs back to the AccountController to authenticate again.  </p>
  
  <p>What am I missing?  </p>