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

初级有关问题 为什么 Response.Cookies.Add 写两次才能跳转到请求页面

2012-04-23 
初级问题 为什么 Response.Cookies.Add 写两次才能跳转到请求页面public static void Login(string userna

初级问题 为什么 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));         } 

热点排行