title: Making user login persistant with ASP .Net Membership
body: <p>I have a website that is built in ASP.NET 3.5 &amp; SQL Server 2005, using the sql membership provider, and presumably forms authentication.</p>
  
  <p>Since security needs on my site are very low, I would like to authenticate once, and then keep the log in persisted indefinitely (without giving the user the choice).</p>
  
  <p>What is happening is that the user logs in, stays logged in for the session, and then the next time they arrive, they are logged out.</p>
  
  <p>How can I get the log-in persisted?</p>
  
  <p>Here's the technical details, I have experimented with many variations of durations.</p>
  
  <pre><code> Try
              If Membership.ValidateUser(UserName.Text, Password.Text) Then
                  Security.UserManager.AuthenticateUser(UserName.Text)
  
                  If FormsAuthentication.GetRedirectUrl(UserName.Text, False) = "/default.aspx" Then
                      Try
                          'Custom Logic'
                      Catch Ex As Exception
                          'Custom Error handling'
                      End Try
                  Else
                      FormsAuthentication.RedirectFromLoginPage(UserName.Text, True)
                  End If
              End If
          Catch ex As Exception
              RaiseEvent ExceptionThrown(New ApplicationException("An error occurred trying to log the user in after account creation.", ex))
          End Try
  
  Public Shared Sub AuthenticateUser(ByVal Username As String)
      Dim Expiration As DateTime = DateTime.Now.AddMonths(3)
  
      Dim authTicket As FormsAuthenticationTicket = New FormsAuthenticationTicket(Username, True, Expiration.Subtract(Expiration).TotalMinutes)
      Dim EncryptedTicket As String = FormsAuthentication.Encrypt(authTicket)
      Dim AuthCookie As New HttpCookie(FormsAuthentication.FormsCookieName, EncryptedTicket)
      AuthCookie.Expires = Expiration
      HttpContext.Current.Response.Cookies.Add(AuthCookie)
  End Sub
  </code></pre>
  
  <p>Web Config:</p>
  
  <pre><code>&lt;membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="15"&gt;
     &lt;providers&gt;
        &lt;clear /&gt;
        &lt;add
           name="SqlProvider"
           type="System.Web.Security.SqlMembershipProvider"
           connectionStringName="GlobalConnString"
           applicationName="/"
           enablePasswordRetrieval="false"
           enablePasswordReset="true"
           requiresQuestionAndAnswer="false"
           requiresUniqueEmail="true"
           passwordFormat="Hashed"
           minRequiredPasswordLength="5"
           minRequiredNonalphanumericCharacters="0"
        /&gt;
     &lt;/providers&gt;
  &lt;/membership&gt;
  
  
  &lt;authentication mode="Forms"&gt;
      &lt;forms timeout="1439200" name="SITENAME" loginUrl="/login.aspx" /&gt;
  &lt;/authentication&gt;
  </code></pre>
  
  <p>Edit:  Here is the cookie information that is getting stored by the client:</p>
  
  <pre><code>Name    ASP.NET_SessionId
  Value   r4dz1555f1pdne45n1zrlkmg
  Host    SITEADDRESS.com
  Path    /
  Secure  No
  Expires At End Of Session
  
  Name    .ASPXAUTH
  Value   648767AC72A60DBA49650A361A2FA446BA992F792055EF5B488CADC95DF495315C1C577F1C8E67E67BD937A7AB6CC5DAED85D8D64E4ED7867FC0FC395F48FED7FB631033CE441DE85223E8B3EBAE616C
  Host    www.SITEADDRESS.com
  Path    /
  Secure  No
  Expires Tue, 09 Jun 2009 17:51:31 GMT
  
  Name    ASP.NET_SessionId
  Value   gn5pcymhfsnua455yp45wpej
  Host    www.SITEADDRESS.com
  Path    /
  Secure  No
  Expires At End Of Session
  
  Name    SITENAME
  Value   9610E8515F3DBC088DAC286E1F44311A20CB2BBB57C97F906F49BC878A6C6AC0B9011777402AEA130DCDC521EF4FBB3393DB310083F72EB502AE971183306C24F07F696B3695C67DD73166F1653DF52B
  Host    www.SITEADDRESS.com
  Path    /
  Secure  No
  Expires Tue, 20 Dec 2011 06:14:10 GMT
  </code></pre>