Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
My task is to create a facility for user for my site that if he checks in the textbox "keep me signed in" he should stay signed in unless until he logs out...
How can i do it widout using cookies ? or using cookies in simple manner !
Please Suggest !
Posted

Hi ,
This Example will Guide you .
C#
protected void Page_Load(object sender, EventArgs e)
{
    HttpCookie CookieName = Request.Cookies["username"];

    if (!IsPostBack)
    {
        if (CookieName != null)
        {
            LoginDiv.Visible = false;
            Label3.Visible = true;
            Label3.Text = CookieName.Value;
            Button2.Visible = true;

        }
        else
        {
            LoginDiv.Visible = true;
            Label3.Visible = false;
            Button2.Visible = false;

        }
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    //login
    if (CheckBox1.Checked ==true)
    {
        HttpCookie CookieName = new HttpCookie("username");
        CookieName.Value = TextBox1.Text;
        CookieName.Expires = DateTime.Now.AddDays(90);
        Response.Cookies.Add(CookieName);
        LoginDiv.Visible = false;
        Label3.Visible = true;
        Button2.Visible = true;
        Label3.Text = CookieName.Value;
    }
}
//log out
protected void Button2_Click(object sender, EventArgs e)
{
    HttpCookie CookieName = Request.Cookies["username"];
    CookieName.Expires.AddMilliseconds(1);
    CookieName.Value = null;
    Response.Cookies.Add(CookieName);
    LoginDiv.Visible = true;
    Label3.Visible = false;
    Button2.Visible = false;
}


ASP.NET
<div id="LoginDiv" runat="server">
       <asp:Label ID="Label1" runat="server" Text="user name"></asp:Label>
       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
       <br />
       <asp:Label ID="Label2" runat="server" Text="Password"></asp:Label>
       <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
       <asp:CheckBox ID="CheckBox1" runat="server" Text="Remember me" />
       <br />
       <asp:Button ID="Button1" runat="server" Text="Login" onclick="Button1_Click" />

   </div>
   <div>
   <asp:Label ID="Label3" runat="server" Text="Label" Visible="False"></asp:Label>

   <asp:Button ID="Button2" runat="server" onclick="Button2_Click"
       Text="log out" />
           </div>


Best Regards
M.Mitwalli
 
Share this answer
 
Comments
Taresh Uppal 2-Apr-12 6:30am    
I have done somwwat like this only but wenever I close my browser and open it again it doesnt keeps me login however it redirects me to the login page...so cookies are not playing there role...
Mohamed Mitwalli 2-Apr-12 7:48am    
did you try this code if u tried and it didn't work with you Go to your browser and check if enable cookies .
Just set the cookie to not expire - or rather, to expire in say 50 years time:
C#
Response.Cookies["userName"].Value = "TheUser";
Response.Cookies["userName"].Expires = DateTime.Now.AddYears(50);
 
Share this answer
 

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