日期:2014-05-17  浏览次数:21028 次

请问asp.net如何读取加密COOKIE
我的登陆页是这样的,我想在用户登陆后在每个页面显示用户名,请问如何从COOKIE中读出来啊,谢谢
protected void Enter_Click(object sender, EventArgs e)
    {
        FormsAuthentication.Initialize(); 
        string name = Request.Form["name"].Trim();
        string pwd = Request.Form["pwd"].Trim();
        string yanzheng = Request.Form["yanzheng"].Trim();
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
        conn.Open();
        string sql = "select count(*) from userinfo where username='" + name + "' and userpwd='" + pwd + "'";
        SqlCommand cmd = new SqlCommand(sql, conn);
        int count = Convert.ToInt32(cmd.ExecuteScalar());
        if (string.Compare(Request.Cookies["CheckCode"].Value, yanzheng, true) != 0)
        {
            Response.Write("<script>alert('验证码输入不正确');location.href='../admin/login.aspx';</script>");
            Response.End();
        }
        else
        {
            if (count > 0)
            {
                

                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),true,//Roles,//用户所属的角色字符串 
                    FormsAuthentication.FormsCookiePath);
                //加密身份验证票据 
                string hash = FormsAuthentication.Encrypt(ticket);
                //创建要发送到客户端的cookie 
                HttpCookie mycookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
                if (ticket.IsPersistent)
                {
                    mycookie.Expires = ticket.Expiration;
                }