让我保持登录状态,直到注销 [英] Keep me signed in until Loggged out

查看:123
本文介绍了让我保持登录状态,直到注销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的任务是为我的网站的用户创建一个设施,如果他签入保持我的登录状态"文本框,则除非他退出登录,否则他应该保持登录状态.
我如何使用Cookie进行淘汰?或以简单的方式使用Cookie!
请提出建议!

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 !

推荐答案


本示例将指导您.
Hi ,
This Example will Guide you .
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;
}





<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>



最好的问候
M.Mitwalli



Best Regards
M.Mitwalli


只需将cookie设置为不过期-或说在50年后过期:
Just set the cookie to not expire - or rather, to expire in say 50 years time:
Response.Cookies["userName"].Value = "TheUser";
Response.Cookies["userName"].Expires = DateTime.Now.AddYears(50);


这篇关于让我保持登录状态,直到注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆