2013-04-26 78 views
0

目前正在使用SPA,我只想通过jquery-ajax实现登录。 这实际上工作正常,但当关闭页面,然后回到页面,我从来没有登录。单页应用:设置无重定向的认证Cookie

这obetouly发生,因为提供凭据后,没有重定向,我想避免。

所以问题是:是否有可能以某种方式将cookie从服务器发送到客户端与jquery,并从那里设置它在浏览器中,以便当用户回来时,他仍然登录?

谢谢

回答

0

回答这个问题。在服务器上,这样做:

FormsAuthentication.SetAuthCookie(account.name, true); 
      System.Web.HttpCookie authCookie = this.RenewCurrentUser(); 

private System.Web.HttpCookie RenewCurrentUser() 
    { 
     System.Web.HttpCookie authCookie = 
      System.Web.HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]; 
     if (authCookie != null) 
     { 
      FormsAuthenticationTicket authTicket = null; 
      authTicket = FormsAuthentication.Decrypt(authCookie.Value); 

      if (authTicket != null && !authTicket.Expired) 
      { 
       FormsAuthenticationTicket newAuthTicket = authTicket; 

       if (FormsAuthentication.SlidingExpiration) 
       { 
        newAuthTicket = FormsAuthentication.RenewTicketIfOld(authTicket); 
       } 
       string userData = newAuthTicket.UserData; 
       string[] roles = userData.Split(','); 

       System.Web.HttpContext.Current.User = 
        new System.Security.Principal.GenericPrincipal(new FormsIdentity(newAuthTicket), roles); 
      } 
     } 

     return authCookie; 
    } 

不记得在那里我发现这个

相关问题