【问题标题】:Single page app: Set authentication cookie without redirection单页应用程序:设置身份验证 cookie 而不进行重定向
【发布时间】:2013-04-26 13:24:16
【问题描述】:

目前正在做一个SPA,我想只通过jquery-ajax实现登录。 这实际上工作正常,但是当关闭页面然后返回页面时,我从未登录过。

这明显发生,因为在提供凭据后,没有重定向,我想避免这种情况。

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

谢谢

【问题讨论】:

    标签: jquery authentication redirect cookies


    【解决方案1】:

    回答问题。在服务器上,执行以下操作:

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

    不记得我在哪里找到的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 2013-09-16
      • 1970-01-01
      • 2015-11-22
      • 2018-12-29
      • 2014-11-17
      相关资源
      最近更新 更多