Click here to Skip to main content
15,880,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys i have a problem with my sign-out button , i have a button named"sign-out" appears when the user enter his username and password in the pop out form and the pop up form closed.

when the user enter his name and password the 2 boxes appears one contain(LABEL) (welcome and the username of the user ) and the other box(BUTTON) is the sign-out.

my problem is when i when i click the sign-out button i don't redirect to my page again to restart the cycle or delete any sessions or cookies.
here is the code.

code for the form sign in form:

ASP.NET
<!-- Username & Password Login form -->
			<div class="user_login">
				<form action="#">
					<label>Email / Username</label>
					<asp:TextBox ID="emailTextBox1" runat="server" MaxLength="20"></asp:TextBox>
					
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please enter your Email/Username." ControlToValidate="emailTextBox1"></asp:RequiredFieldValidator>
                    <br />
					<label>Password</label>
					<asp:TextBox ID="passwordTextBox1" runat="server" MaxLength="20" TextMode="Password"></asp:TextBox>
					
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please enter your password." ControlToValidate="passwordTextBox1"></asp:RequiredFieldValidator>
                    <br />
                     <br />
                    
                    

					<div class="checkbox">
						<asp:CheckBox ID="remember" runat="server"></asp:CheckBox>
						<label for="remember">Remember me on this computer</label>
					</div>

					<div class="action_btns">
						<div class="one_half"><asp:Button id="backButton1"  runat="server" Text="Back"  BorderStyle="None"  CssClass="btn back_btn" EnableTheming="True" Width="150px"></asp:Button></div>
						<div class="one_half last"><asp:Button id="loginButton2"  runat="server" Text="Login"  BorderStyle="None"  CssClass="btn btn_red" EnableTheming="True" Width="140px" OnClick="loginButton2_Click"></asp:Button></div>
					</div>
				</form>

				
			</div>


code for the sign out button :
ASP.NET
<div><asp:Button id="signout" CssClass="btn_red" runat="server" Text="Sign-Out" BorderStyle="None" OnClick="signout_Click" EnableTheming="True"  ValidateRequestMode="Enabled" /></div>


the asp.net code :
C#
protected void loginButton2_Click(object sender, EventArgs e)
       {
           if (remember.Checked == true)
           {

               Response.Cookies["username"].Value = emailTextBox1.Text;
               Response.Cookies["username"].Expires = DateTime.Now.AddDays(15);

               Response.Cookies["password"].Value = passwordTextBox1.Text;
               Response.Cookies["password"].Expires = DateTime.Now.AddDays(15);



               if (!string.IsNullOrEmpty(emailTextBox1.Text))
               {
                   modal_trigger.Attributes["Style"] = "display:none";
                   modal_trigger2.Attributes["Style"] = "display:block";
                   modal_trigger2.Text = "welcome" + "<br/>" + emailTextBox1.Text;
                   signout.Attributes["Style"] = "display:block";
               }
           }
           else
           {
               if (!string.IsNullOrEmpty(emailTextBox1.Text))
               {
                   Session.Add("username", emailTextBox1.Text);
                   modal_trigger.Attributes["Style"] = "display:none";
                   modal_trigger2.Attributes["Style"] = "display:block";
                   modal_trigger2.Text = "welcome" + "<br/>" + Session["username"].ToString();
                   signout.Attributes["Style"] = "display:block";
               }
           }



       }

       protected void signout_Click(object sender, EventArgs e)
       {
           Response.Cookies["username"].Expires = DateTime.Now.AddDays(-1d);
           Response.Cookies["password"].Expires = DateTime.Now.AddDays(-1d);

           Response.Cookies.Remove("username");
           Response.Cookies.Remove("password");


           Session.Abandon();

           Session.Clear();
           Response.Redirect("default.aspx");

       }


   }



the login button work but the signout not working.
Posted
Comments
Richard Deeming 13-Oct-14 13:23pm    
Apart from the fact that you don't appear to be validating the username and password, storing the user's password in a cookie is an exceptionally bad idea. Particularly so as your site isn't being accessed over HTTPS, and you haven't set either the Secure or HttpOnly flags on the cookie.

Take a look at Troy Hunt's blog post on the topic:
How to build (and how not to build) a secure "remember me" feature[^]
VC.J 13-Oct-14 14:12pm    
agree with Richard but for your case I tried your code , the validation is working on SignOut Button click the same way its working on Login button are you facing the same case or other case.
if you are facing the same case than use validation group

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900