Trying to get persistent login to workhttp://forums.asp.net/t/297725.aspx/1?Trying+to+get+persistent+login+to+workTue, 05 Aug 2003 15:03:57 -0400297725297725http://forums.asp.net/p/297725/297725.aspx/1?Trying+to+get+persistent+login+to+workTrying to get persistent login to work I'm trying to implement a Remember Me checkbox on my login form that when checked, remembers the users login and password so next time they visit the site they don't have to log in. It doesn't work. Whether I visit another site then come back or close the browser and open a new one to go back to the site, the login page always comes up. Can anyone see where I've gone wrong here? ----------- Here's the VB code ------------- <pre class="prettyprint">&lt;script runat=&quot;server&quot;&gt; Sub Login_Click(Src as Object, E as EventArgs) 'store NRDS # in Session variable Session.Contents(&quot;MAJOR_KEY&quot;) = MAJOR_KEY.Text 'establish ADO.Net connection Dim strConn As String = ConfigurationSettings.AppSettings(&quot;ConnectionString&quot;) 'sql to get user info Dim strSql as String = &quot;SELECT USERNAME, PASSWORD FROM [member] WHERE (USERNAME = '&quot; &#43; USERNAME.Text &#43; &quot;') AND (PASSWORD = '&quot; &#43; PASSWORD.Text &#43; &quot;')&quot; Dim objConn as New SqlConnection(strConn) Dim objCommand as New SqlCommand(strSql, objConn) Dim objDataReader as SqlDataReader objConn.Open() 'open the connection 'open the DataReader to access data objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection) If PASSWORD.Text=&quot;1234&quot; then response.write (&quot;You must register and change your password to access the members area.&quot;) Else If objDataReader.Read() = True Then 'USERNAME/PASSWORD match, verify again If objDataReader(&quot;USERNAME&quot;) = USERNAME.Text And objDataReader(&quot;PASSWORD&quot;) = PASSWORD.Text Then FormsAuthentication.RedirectFromLoginPage(USERNAME.Text, RememberMe.Checked) 'authenticate login, redirect to default page Session.Contents(&quot;loginFail&quot;) = &quot;N&quot; 'this won't be used if successful login Response.Redirect(&quot;members/default.aspx&quot;) 'send user to main page with their customized selections Else Session.Contents(&quot;loginCount&quot;) &#43;= 1 'login failed increment counter objDataReader.Close() 'clean up close datareader objConn.Close() 'clean up close ado.net connection Session.Contents(&quot;loginFail&quot;) = &quot;Y&quot; 'set variable for lockout page Response.Redirect(&quot;index.aspx&quot;) 'redirect to login again End If Else Session.Contents(&quot;loginFail&quot;) = &quot;Y&quot; 'set variable for lockout page Response.Write(&quot;Invalid NRDS # or Password. Please Try again.&quot;) End If End If objDataReader.Close() 'clean up objConn.Close() End Sub &lt;/script&gt;</pre>----------- Here's the form: ------------ <pre class="prettyprint">&lt;form id="frmLogin" name="frmLogin" runat="server"&gt; <img height="100" src="images/header_sm.gif" width="300" /> <table width="295"> <tbody> <tr> <td class="title" colspan="2"> Member Login</td> </tr> <tr> <td class="body" width="120"> &lt;div align="left"&gt;NRDS #: &lt;/div&gt;</td> <td class="body" width="163"> <asp:textbox id="MAJOR_KEY" runat="server" TextMode="SingleLine"></asp:textbox> </td> </tr> <tr> <td> &lt;div class="body" align="left"&gt;Password: &lt;/div&gt;</td> <td class="body"> <asp:textbox id="PASSWORD" runat="server" TextMode="Password"></asp:textbox> </td> </tr> <tr> <td colspan="2"> <font size="1">Remember passwords are CASE sensitive</font> </td> </tr> <tr> <td> &lt;div class="body" align="left"&gt;Remember Login: &lt;/div&gt;</td> <td class="body"> <asp:checkbox id="RememberMe" runat="server"></asp:checkbox> </td> </tr> <tr> <td> &lt;div align="right"&gt; <asp:button id="Login" onclick="Login_Click" runat="server" Text="Login"></asp:button> &lt;/div&gt;</td> <td> &lt;input style="DISPLAY: none" type="text" /&gt; &lt;input id="Reset1" type="reset" value="Reset" name="Reset1" runat="server" /&gt;</td> </tr> <tr> <td colspan="2"> <p> You must <a class="navLink" href="register/reg.aspx">register</a> in order to gain access to the members section. </p> <p> <a class="nav" href="register/reg.aspx">Register Now</a> </p></td> </tr> </tbody> </table> <p><span class="legal"> This site requires</span> <asp:HyperLink id="flash" runat="server" Target="_blank" NavigateUrl="http://www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash"></asp:HyperLink> <span class="legal"><img src="images/get_flashplayer.gif" width="88" height="31" border="0" align="texttop"></span> </p> &lt;/form&gt;</pre>-------- And here are the key parts of my web.config file ----------- <pre class="prettyprint"><authentication mode="Forms"> &lt;forms name=".theDefault" loginUrl="members/login.aspx" path="/" protection="All" timeout="15" &gt; &lt;/forms&gt; </authentication> <location path="members"> <system.web> <authorization> <deny users="?" /> </authorization> </system.web> </location></pre> 2003-08-01T19:34:37-04:00298005http://forums.asp.net/p/297725/298005.aspx/1?Re+Trying+to+get+persistent+login+to+workRe: Trying to get persistent login to work Without actually running your code (hence being lazy :-) ), couple of things to check: * Check the value of RememberMe.Checked to be sure it really comes back as true. * Double-check that your browser is accepting cookies. (After running this code, look at your cookie store and see if the server has written one out.) If those things aren't the problem, I'll try running your code ... ;-) 2003-08-02T03:19:59-04:00300213http://forums.asp.net/p/297725/300213.aspx/1?Re+Trying+to+get+persistent+login+to+workRe: Trying to get persistent login to work Mikepope, Thanks for your reply. I checked both of those things and all looks as it should. RememberMe.Checked does return as true, and cookie is being written. Any other thoughts...? Thanks 2003-08-05T13:20:33-04:00300313http://forums.asp.net/p/297725/300313.aspx/1?Re+Trying+to+get+persistent+login+to+workRe: Trying to get persistent login to work Well, bad news, it seems -- it works ok for me. This is how I tested: 1) Create a members folder under the root. 2) Put a default.aspx page in it. 3) Put login.aspx in the members folder. 4) Add sections to web.config. 5) In browser, ask for localhost/<i>site</i>/members. ASP.NET displays the login page. 6) Enter login credentials, click Remember Me, click Login. Default.aspx page is displayed. 7) Close browser. 8) In browser, ask for localhost/<i>site</i>/members. This time, ASP.NET displays the default.aspx w/o a login. 9) Delete cookie for my site. 10) In browser, ask for localhost/<i>site</i>/members. ASP.NET displays the login page. 11) Enter login creds, do not check Remember Me, click Login. Default.aspx page is displayed. 12) Close browser. 13) Open browser, ask for localhost/<i>site</i>/members. ASP.NET displays the login page. This is the expected the behavior, I believe. Just so you can see what I was testing, I attached the slightly modified logintest.aspx page that I was using. What I would do next is to create a very simple login page and protected page and try to whittle the scenario down to its bare essentials. (For instance, in the test scenario, just hardcode a username and password.) The idea would be to see if there's something inherently wrong with Forms authentication on your site or whether it involves some application bug in your current app. Just a thought. ===============<pre class="prettyprint">&lt;script runat=&quot;server&quot;&gt; Sub Login_Click(Src as Object, E as EventArgs) 'store NRDS # in Session variable Session.Contents(&quot;MAJOR_KEY&quot;) = MAJOR_KEY.Text Dim strConn As String = ConfigurationSettings.AppSettings(&quot;ConnectionString&quot;) Dim strSql as String = &quot;SELECT USERNAME FROM [Users] WHERE (USERNAME = '&quot; &#43; USERNAME.Text &#43; &quot;')&quot; Dim objConn as New SqlConnection(strConn) Dim objCommand as New SqlCommand(strSql, objConn) Dim objDataReader as SqlDataReader objConn.Open() 'open the connection objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection) If PASSWORD.Text=&quot;1234&quot; then response.write (&quot;You must register and change your password to access the members area.&quot;) Else If objDataReader.Read() = True Then 'USERNAME/PASSWORD match, verify again If objDataReader(&quot;USERNAME&quot;) = USERNAME.Text Then FormsAuthentication.RedirectFromLoginPage(USERNAME.Text, RememberMe.Checked) 'authenticate login, redirect to default page Session.Contents(&quot;loginFail&quot;) = &quot;N&quot; 'this won't be used if successful login Response.Write(&quot;login successful&quot;) Response.Redirect(&quot;default.aspx&quot;) 'send user to main page with their customized selections Else Session.Contents(&quot;loginCount&quot;) &#43;= 1 'login failed increment counter objDataReader.Close() 'clean up close datareader objConn.Close() 'clean up close ado.net connection Session.Contents(&quot;loginFail&quot;) = &quot;Y&quot; 'set variable for lockout page Response.Write(&quot;login failed&quot;) 'Response.Redirect(&quot;index.aspx&quot;) 'redirect to login again End If Else Session.Contents(&quot;loginFail&quot;) = &quot;Y&quot; 'set variable for lockout page Response.Write(&quot;Invalid NRDS # or Password. Please Try again.&quot;) End If End If objDataReader.Close() 'clean up objConn.Close() End Sub &lt;/script&gt; &lt;form id=&quot;frmLogin&quot; name=&quot;frmLogin&quot; runat=&quot;server&quot;&gt; Member Login &lt;div align=&quot;left&quot;&gt;NRDS #: &lt;/div&gt; &lt;div class=&quot;body&quot; align=&quot;left&quot;&gt;User name:&lt;/div&gt; &lt;div class=&quot;body&quot; align=&quot;left&quot;&gt;Password:&lt;/div&gt; Remember passwords are CASE sensitive &lt;div class=&quot;body&quot; align=&quot;left&quot;&gt; Remember Login: &lt;/div&gt; &lt;div align=&quot;right&quot;&gt; &lt;/div&gt; &lt;input style=&quot;DISPLAY: none&quot; type=&quot;text&quot; /&gt; &lt;input id=&quot;Reset1&quot; type=&quot;reset&quot; value=&quot;Reset&quot; name=&quot;Reset1&quot; runat=&quot;server&quot; /&gt; <p> You must register in order to</p> gain access to the members section. <p></p> <p> Register Now </p> <p>This site requires</p> &lt;asp:HyperLink id=&quot;flash&quot; runat=&quot;server&quot; Target=&quot;_blank&quot; NavigateUrl=&quot;http://www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash&quot;&gt; <p></p> &lt;/form&gt;</pre> 2003-08-05T14:46:11-04:00300321http://forums.asp.net/p/297725/300321.aspx/1?Re+Trying+to+get+persistent+login+to+workRe: Trying to get persistent login to work Thanks, I had a feeling it would work correctly. Must be something else going on here. I'll take the steps you recommended. Thanks for your help. 2003-08-05T14:55:09-04:00