public static void Login(string username, string roles, bool isPersistent)
{
   FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
        1, // 票据版本号
         username, // 票据持有者
         DateTime.Now, //分配票据的时间
         dt, // 失效时间
         true, // 需要用户的 cookie 
        roles, // 用户数据,这里其实就是用户的角色
         FormsAuthentication.FormsCookiePath //cookie有效路径
   );    string hash = FormsAuthentication.Encrypt(ticket);
    // 下面添加为什么要写两次才能跳转到请求页面,
    HttpContext.Current.Response.Cookies.Add(
     new HttpCookie(FormsAuthentication.FormsCookieName, hash));
    HttpContext.Current.Response.Cookies.Add(
     new HttpCookie(FormsAuthentication.FormsCookieName, hash));    HttpContext.Current.Response.Redirect(FormsAuthentication.GetRedirectUrl(username, false));        }

解决方案 »

  1.   


    public void Login(string username, string roles, bool isPersistent) 

       FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 
            1, // 票据版本号 
             username, // 票据持有者 
             DateTime.Now, //分配票据的时间 
             dt, // 失效时间 
             true, // 需要用户的 cookie  
            roles, // 用户数据,这里其实就是用户的角色 
             FormsAuthentication.FormsCookiePath //cookie有效路径 
       );     string hash = FormsAuthentication.Encrypt(ticket); 
        // 下面添加为什么要写两次才能跳转到请求页面, 
        HttpContext.Current.Response.Cookies.Add( 
         new HttpCookie(FormsAuthentication.FormsCookieName, hash)); 
        
        Response.Redirect(FormsAuthentication.GetRedirectUrl(username, false)); 
            }
      

  2.   

    我遇到类似问题Response.Cookies.Add(cookie);这句话如果是{}里的最后一句,就添加不了,只有这句话后便再跟一句比如加一句label.text=“abc”或是如楼主再写一遍Response.Cookies.Add(cookie);添加cookies的语句才得以执行这是为什么呢????