我使用的2005,使用的是登录控件,里边有这“下次记住我”,不知道如何使用

解决方案 »

  1.   

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestAutoLogin.aspx.cs" Inherits="Test_TestAutoLogin" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:Panel ID="Panel1" runat="server" Height="50px" Width="125px">
            userName:<asp:TextBox ID="TxtName" runat="server"></asp:TextBox>&nbsp;
            
            
            <br />
            <br />
            Password:
            <asp:TextBox ID="TxtPassword" runat="server"></asp:TextBox>&nbsp;
            <br />
            <asp:CheckBox ID="CbxRemember" runat="server" Text="记住我" />
            <asp:Button ID="BtnSure" runat="server" OnClick="BtnSure_Click" Text="登陆" />
            </asp:Panel>
            &nbsp;<br />
            &nbsp;<br />
            <br />
            <asp:Button ID="BtnLoginout" runat="server" OnClick="BtnLoginout_Click" Text="注销" />
            <br />
            <br />
            <br />
            <br />
            <br />
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
        </form>
    </body>
    </html>
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class Test_TestAutoLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null)
            {
                if (Request.Cookies.Get("ChinaVT_UserName") != null && Request.Cookies.Get("ChinaVT_UserPssword") != null)
                {
                    Session["UserName"] = Request.Cookies.Get("ChinaVT_UserName").Value;
                }
            }
            if (Session["UserName"] != null)
            {
               this.Label1.Text =  string.Format("<br>{0}使用Cookie已登陆",Session["UserName"].ToString());
                Panel1.Visible = false;
                BtnLoginout.Visible = true;
            }
            else
            {
                this.Label1.Text ="<br>未登陆";
                Panel1.Visible = true;
                BtnLoginout.Visible = false;
            }
        }
        protected void BtnSure_Click(object sender, EventArgs e)
        {
            //验证用户名密码成功后写Cookie
            if (CbxRemember.Checked)
            {
                HttpCookie cookieName = new HttpCookie("ChinaVT_UserName", TxtName.Text.Trim());
                HttpCookie cookiePassword = new HttpCookie("ChinaVT_UserPssword", TxtPassword.Text.Trim());
                cookieName.Expires = System.DateTime.Now.AddDays(7);
                cookiePassword.Expires = System.DateTime.Now.AddDays(7);
                Response.Cookies.Add(cookieName);
                Response.Cookies.Add(cookiePassword);            Panel1.Visible = false;
                BtnLoginout.Visible = true;
                this.Label1.Text = string.Format("<br>{0}使用Cookie已登陆", TxtName.Text.Trim());        }
        }
        protected void BtnLoginout_Click(object sender, EventArgs e)
        {
            Response.Cookies.Remove("ChinaVT_UserName");
            Response.Cookies.Remove("ChinaVT_UserPssword");
            Panel1.Visible = true;
            BtnLoginout.Visible = false;
            this.Label1.Text = "<br>未登陆";
        }
    }