title: Forms authentication, shared cookie issues
body: <p>I'm noticing some strange behavior with a shared authentication cookie setup, here's my scenario.</p>
  
  <p>I've got two applications with the domains similar to the following:</p>
  
  <p>login.mydomain.com
  system.mydomain.com</p>
  
  <p>I am redirecting the user to login.mydomain.com and dropping a the cookie from there on mydomain.com like so.</p>
  
  <p><strong>system.mydomain.com:</strong></p>
  
  <pre><code>void Application_AuthenticateRequest(object sender, EventArgs e)
      {
          if (Context.User == null || !Request.IsAuthenticated)
          {
              HttpContext.Current.Response.Redirect("http://login.mydomain.com");
          }
      }
  </code></pre>
  
  <p><strong>login.mydomain.com</strong></p>
  
  <pre><code>protected void btnSubmit_Click(object sender, EventArgs e)
      {
          pnlLoginNotice.Visible = true;
          if (Membership.ValidateUser(txtUsername.Text, txtPassword.Text))
          {
              HttpCookie cookie = FormsAuthentication.GetAuthCookie(txtUsername.Text, chkRememberMe.Checked);
              cookie.Domain = "mydomain.com";
  
              Response.Cookies.Set(cookie);
          }
      }
  </code></pre>
  
  <p><strong>Web.config:</strong> </p>
  
  <pre><code>&lt;authentication mode="Forms" &gt;
    &lt;forms timeout="2880" name=".COMMONAUTH"  /&gt;      
  &lt;/authentication&gt;
  </code></pre>
  
  <p>Now the behavior I'm seeing is that I'm finding the .COMMONAUTH cookie dropped under system.mydomain.com sometimes, while there's the same cookie under mydomain.com. I've noticed that it shows up after some time of inactivity on the site.</p>
  
  <p>Is it possible that asp.net is dropping the cookie on it's own to keep the forms authentication alive?</p>
  
  <p><strong>UPDATE</strong>
  I've tried the following approaches</p>
  
  <p><strong>1:</strong></p>
  
  <p>system.mydomain.com Web.config</p>
  
  <pre><code>&lt;authentication mode="Forms" &gt;
    &lt;forms timeout="1" name=".COMMONAUTH" /&gt;      
  &lt;/authentication&gt;
  </code></pre>
  
  <p>login.mydomain.com Web.config</p>
  
  <pre><code>&lt;authentication mode="Forms" &gt;
    &lt;forms timeout="2"  name=".COMMONAUTH"/&gt;
  &lt;/authentication&gt;
  </code></pre>
  
  <p>When refreshing a page in system.mydomain.com after one minute has passed, I get the .COMMONAUTH cookie under system.mydomain.com</p>
  
  <p><strong>2:</strong></p>
  
  <p>system.mydomain.com Web.config</p>
  
  <pre><code>&lt;authentication mode="Forms" &gt;
    &lt;forms timeout="2" name=".COMMONAUTH"  /&gt;      
  &lt;/authentication&gt;
  </code></pre>
  
  <p>login.mydomain.com Web.config</p>
  
  <pre><code>&lt;authentication mode="Forms" &gt;
    &lt;forms timeout="1"  name=".COMMONAUTH"/&gt;
  &lt;/authentication&gt;
  </code></pre>
  
  <p>When refreshing a page in system.mydomain.com after one minute has passed, I get logged out.</p>
  
  <p><strong>3:</strong></p>
  
  <p>system.mydomain.com Web.config</p>
  
  <pre><code>&lt;authentication mode="Forms" &gt;
    &lt;forms timeout="1" name=".COMMONAUTH" domain="mydomain.com" /&gt;      
  &lt;/authentication&gt;
  </code></pre>
  
  <p>login.mydomain.com Web.config</p>
  
  <pre><code>&lt;authentication mode="Forms" &gt;
    &lt;forms timeout="2"  name=".COMMONAUTH" domain="mydomain.com"/&gt;
  &lt;/authentication&gt;
  </code></pre>
  
  <p>When refreshing a page in system.mydomain.com after one minute has passed, all remains the same and I'm still authenticated. Not sure what will happen when a 3rd application will be introduced to this setup</p>
  
  <p><strong>Conclusion</strong></p>
  
  <p>I think my issue is comming from not setting the domain in the web.config, so system.mydomain.com is trying to refresh the cookie but is using its own domain since I am not telling it where it should be doing it.</p>
  
  <p>My problem is that these applications will have different domain bindings and they will be hosted once for multiple clients. I cannot set FormsAuthentication.CookieDomain as it is read only.</p>
  
  <p>Should I go with option 2, and give my cookie issuer a lower timeout from the other applications? Will this have any implications?</p>