ASP.NET记住登陆用户名的具体实现

ASP.NET记住登陆用户名的具体实现,需要的朋友可以参考一下

.aspx文件中

复制代码 代码如下:








.aspx.cs文件中

复制代码 代码如下:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.txtUser_Id.Focus();
            if (!Object.Equals(Request.Cookies["UserID"], null))
            {
                //创建一个Cookie对象,实现记住用户名
                HttpCookie readcookie = Request.Cookies["UserID"];
                this.txtUser_Id.Text = readcookie.Value;
            }
        }
    }
    private void CreateCookie()
    {
        //创建一个Cookie对象
        HttpCookie cookie = new HttpCookie("UserID");
        //判断Checkbox控件是否被选中
        if (this.cbxRemeberUser.Checked)
        {
            //将用户编号存储到创建的Cookie对象中
            cookie.Value = this.txtUser_Id.Text;
        }
        //获取创建的Cookie对象的过期时间
        cookie.Expires = DateTime.MaxValue;
        //将创建的Cookie对象添加到内部Cookie集合中
        Response.AppendCookie(cookie);
}

    protected void btnInsert_Click(object sender, ImageClickEventArgs e)
    {

        if (object.Equals(Request.Cookies["UserID"], null))
        {
          //调用自定义方法 CreateCookie()存储用户名
          CreateCookie();
        }
        else
        {
           CreateCookie();
        }

}

以上就是ASP.NET记住登陆用户名的具体实现的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » ASP编程