FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, // Ticket version
userCode, // Username to be associated with this ticket
DateTime.Now, // Date/time issued
DateTime.Now.AddMinutes(30), // Date/time to expire
true, // "true" for a persistent user cookie (could be a checkbox on form)
UserPrincipal.CreateUserData(staffId), // User-data  (the roles from the user record)
FormsAuthentication.FormsCookiePath);
// Hash the cookie for transport over the wire
string hash = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(
FormsAuthentication.FormsCookieName, // Name of auth cookie
hash); // Hashed ticket
// Add the cookie to the list for outbound response
Response.Cookies.Add(cookie);
 
Response.Redirect(this.Request.QueryString["ReturnUrl"]);

解决方案 »

  1.   

    http://support.microsoft.com/default.aspx?scid=kb;en-us;308157
      

  2.   

    直接用Response.Redirect("default.aspx")的话当然验证失败,因为你根本没有建立身份验证票。FormsAuthentication.RedirectFromLoginPage方法,会自动完成很多功能的。如完成生成身份验证票,写回客户端,浏览器重定向等一系列的动作。当然完成这些功能并不是只有FormsAuthentication.RedirectFromLoginPage方法才能办到,相反如果需要带角色信息的验证则只能采用其他办法。
    我门可采用手动添加身份验证票
    1.FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket (1,"coffee",DateTime.Now, DateTime.Now.AddMinutes(20), false,UserRoles,"/") ;
    2.加密序列化
    string HashTicket = FormsAuthentication.Encrypt (Ticket) ;
    3.生成cookie
    HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket) ;4.身份验证票Cookie输出到客户端
    Response.Cookies.Add(UserCookie)
    5.重定向
    Response.Redirect (Context.Request["ReturnUrl"]) ;