日期:2014-05-18  浏览次数:20366 次

初级问题 为什么 Response.Cookies.Add 写两次才能跳转到请求页面
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));



  }

------解决方案--------------------
C# code

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)); 


        }