title: Facebook Connect & ASP.NET Forms Authentication - Timing/Cookie Problem
body: <p>Here's <strong>what i have</strong>:</p>
  
  <blockquote>
    <ul>
    <li>ASP.NET 4.0 Web Application</li>
    <li>Forms Authentication (Cookie Based)</li>
    </ul>
  </blockquote>
  
  <p>Here's <strong>what i'm trying to do</strong>:</p>
  
  <blockquote>
    <ul>
    <li>Integrate with Facebook Connect (using the JavaScript &amp; Graph API's)</li>
    <li>Provide a Single-Sign-On Service for my users (automatically log in
    users if their logged in and connected
    to Facebook)</li>
    </ul>
  </blockquote>
  
  <p>On every page there is a control which shows anonymous/logged in user details.</p>
  
  <p><strong>Here's the issue:</strong>
  On <code>Page_PreRender</code> of this user control (which is on every page, so consider this event being executed on every single page request), i attempt the auto-sign on based on the Facebook Authentication status (JavaScript API - Cookies).</p>
  
  <p>If the sign on is successful, i log them in using <code>FormsAuthentication.SetAuthCookie</code>.</p>
  
  <p>This is all working fine.</p>
  
  <p>However, right after i do single sign on (literally the next line), i need to render out either the anonymous stuff (please sign in) or the authenticated user stuff (display name, etc). The problem is, the user isnt really "authenticated" to my site yet. To be "authenticated" to my site i do standard stuff (check the <code>HttpContext.User.Identity.IsAuthenticated</code> property). </p>
  
  <p>I think its not yet set to <code>"true</code>" because the Forms Auth cookie has been created, but thats in the response, the <code>HttpContext.User.Identity.IsAuthenticated</code> is based on the request at this point in time - am i right there?</p>
  
  <p>So of course, its showing the anonymous details even though the user is logged in.</p>
  
  <p>When i refresh the page of course, the correct user details are shown.</p>
  
  <p>So its almost a timing issue. How can i auto-login a user (via <code>FormsAuthentication.SetAuthCookie</code>) ensuring the <code>HttpContext.User.Identity.IsAuthenticated</code> is true?</p>
  
  <p>Maybe im doing the authentication/single-sign-on in the wrong place?</p>
  
  <p>Or do i need to almost do a <code>Response.Redirect</code> to myself in order to refresh the cookies in the HTTP Request?</p>
  
  <p>As you all know, the doco for Facebook Connect is scattered and minimal, so its all a matter of piecing things together.</p>
  
  <p>Any help would be appreciated...cheers!</p>