读书人

初级有关问题 为什么 Response.Cookie

发布时间: 2012-04-23 13:17:38 作者: rapoo

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

读书人网 >asp.net

热点推荐