首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

请教asp.net怎么读取加密COOKIE

2013-06-25 
请问asp.net如何读取加密COOKIE我的登陆页是这样的,我想在用户登陆后在每个页面显示用户名,请问如何从COOK

请问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;
                }
                //把准备好的cookie加入到响应流中 
                Response.Cookies.Add(mycookie);



                //转发到请求的页面 
                Response.Redirect(FormsAuthentication.GetRedirectUrl(name, false)); 


            }
            else
            {
                Response.Write("<script>alert('用户名密码不正确');location.href='../admin/login.aspx';</script>");
            }

        }

    }

ASP.NET 加密 Cookie
[解决办法]
User.Identity.Name

热点排行