首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FormAuthentication .ASPXAUTH cookie在ASP.NET MVC中几分钟后显示为空

FormAuthentication .ASPXAUTH cookie在ASP.NET MVC中几分钟后显示为空
EN

Stack Overflow用户
提问于 2017-08-20 15:45:37
回答 1查看 433关注 0票数 0

我已经在asp.net mvc 5中实现了asp.net,并在LogIn上创建了FormsAuthenticationticket,它成功地创建了,但是过了一会儿,cookie在浏览器中显示,但在应用程序中,它正在变为空。

请帮助解决这个问题。

如有任何帮助,将不胜感激。

登录表格

代码语言:javascript
复制
  public ActionResult Login([Bind(Include = "Username, Password")] LoginModel loginModel, string ReturnUrl)
    {
        if (ModelState.IsValid)
        {
            Egov_Users eGov_Users = db.Egov_Users
                .Where(p => p.UserType.Type != "O" && p.UserName == loginModel.Username)
                .FirstOrDefault();

            if (eGov_Users == null)
            {
                ModelState.AddModelError("", "Invalid username");
                return View();
            }
            else
            {
                if (eGov_Users.Password != loginModel.Password)
                {
                    ModelState.AddModelError("", "Invalid Password");
                    return View();
                }

                var loginDetail = new LoginDetails();
                var serializer = new JavaScriptSerializer();

                loginDetail.userID = eGov_Users.UserId;
                loginDetail.username = eGov_Users.UserName;
                loginDetail.firstName = eGov_Users.FirstName;
                loginDetail.lastName = eGov_Users.LastName;

                var userData = SerializeUserInfoInternal(loginDetail);

                FormsAuthentication.SetAuthCookie(loginDetail.username, false);

                var cookie = FormsAuthentication.GetAuthCookie(
                           FormsAuthentication.FormsCookieName, false);

                var ticket = FormsAuthentication.Decrypt(cookie.Value);

                var durationInHours = 8;
                FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(
                    ticket.Version,
                    loginDetail.username,
                    DateTime.Now,
                      DateTime.Now.AddHours(durationInHours),
                    true,
                    userData);

                // Encrypt the ticket.
                string encTicket = FormsAuthentication.Encrypt(newTicket);

                cookie.Value = encTicket;
                Response.Cookies.Add(cookie);

                int cookieSize = System.Text.UTF8Encoding.UTF8.GetByteCount(cookie.Values.ToString());
                Session["CookieSize"] = cookieSize;
                if (string.IsNullOrEmpty(ReturnUrl))
                {
                    return RedirectToAction("Index", "Users");
                }
            }
        }

        return RedirectToAction("Login", "Login");
    }

全球ASAX

代码语言:javascript
复制
 protected void Application_PostAuthenticateRequest()
    {
        var ticket = GetTicketFromCurrentCookie();
        if (ticket == null)
        {
            Global.WriteLog("Application_PostAuthenticateRequest", "ticket becomes null");
            return;
        }
        var user = DeserializeUserInfoInternal(ticket.Name, ticket.UserData);
        if (user == null)
        {
            return;
        }
        var principal = new AppUserPrincipal(user);
        HttpContext.Current.User = principal;
    }
    private static FormsAuthenticationTicket GetTicketFromCurrentCookie()
    {
        var cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];

        if (cookie == null)
        {
            Global.WriteLog("GetTicketFromCurrentCookie", "Cookie becomes null");
            return null;
        }

        var ticket = FormsAuthentication.Decrypt(cookie.Value);
        return ticket;
    }
    static LoginDetails DeserializeUserInfoInternal(string name, string userData)
    {
        var deserialize = new JavaScriptSerializer();

        var loginDetails = deserialize.Deserialize<LoginDetails>(userData);

        return loginDetails;
    }
EN

回答 1

Stack Overflow用户

发布于 2018-11-02 05:04:41

使用下面的代码获取cookie值

代码语言:javascript
复制
HttpContext.Current.Request.Cookies.Get(".ASPXAUTH");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45783961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档