我刚接触FormsAuthenticationTicket类,知道用如下方法可以将用户信息写到cookies中
private void Buttonlogin_Click(object sender, System.EventArgs e)
{
     string user = TextBoxUser.Text; //读取用户名
     string password = TextBoxPassword.Text; //读取密码
     if(Confirm(user,password) == true) //confirm方法用来验证用户合法性的
    {
         string userRoles = UserToRole(user); //调用UserToRole方法来获取role字符串
         FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket (1,user,DateTime.Now,          DateTime.Now.AddMinutes(30), false,userRoles,"/") ; //建立身份验证票对象
         string HashTicket = FormsAuthentication.Encrypt (Ticket) ; //加密序列化验证票为字符串
         HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket) ; 
//生成Cookie
          Context.Response.Cookies.Add (UserCookie) ; //输出Cookie
         Context.Response.Redirect (Context.Request["ReturnUrl"]) ; // 重定向到用户申请的初始页面
     }
    else
    {
        // 用户身份未被确认时的代码
    }
}
现在请问,如何解密cookies得到FormsAuthenticationTicket (1,user,DateTime.Now,          DateTime.Now.AddMinutes(30), false,userRoles,"/") 中写入的所有信息???请写出相应的代码,不胜感激!!!

解决方案 »

  1.   

    比如说如何读到FormsAuthenticationTicket 中的写入的user,时间,有效时间,userRoles??我不知道如何读出来,写进入就再也看不到了??
      

  2.   

    用cookie的方法访问啊!
    没必要解密吧!
    也不一定能解!
      

  3.   

    不是很明白,怎么用cookie的方法去解?
    FormsAuthenticationTicket.Decrypt应该是可以解密的?
    但cookies的Key是什么??我怎么知道呢??因为我不想用Session去记住user中的值?
      

  4.   

    问一下哈,你们知道类CookieAuthentication吗,这个类好像是在System.Web.Security命名空间中,不知为什么我把写进出,跟本就没有这个东东啊,是不是应该是FormsAuthentication啊!
      

  5.   

    添加  
    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                            1,                              // version
                            Context.User.Identity.Name,     // user name
                            DateTime.Now,                   // issue time
                            DateTime.Now.AddHours(1),       // expires every hour
                            false,                          // don't persist cookie
                            roleStr                         // roles
                            );
                        // Encrypt the ticket
                        String cookieStr = FormsAuthentication.Encrypt(ticket);

                        // Send the cookie to the client
                        Response.Cookies["portalroles"].Value = cookieStr;获取
      

  6.   

    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(Context.Request.Cookies["portalroles"].Value);
    ticket.Name----------Context.User.Identity.Name
    ticket.UserData---------------roleStr