ASP身份CookieAuthentication无法正常工作

时间:2015-11-04 04:24:59

标签: c# asp.net-mvc asp.net-mvc-5 asp.net-identity

所以我有一个MVC5 Web应用程序,我使用内置的ASP.Identity框架进行身份验证。我想让我的用户保持30天的登录状态。

这是Startup.Auth.cs中的代码:

app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                CookieName = "Test",
                LoginPath = new PathString("/Account/Login"),
                Provider = new CookieAuthenticationProvider
                {
                    // Enables the application to validate the security stamp when the user logs in.
                    // This is a security feature which is used when you change a password or add an external login to your account.  
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                        validateInterval: TimeSpan.FromDays(30),
                        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
                },
                ExpireTimeSpan = TimeSpan.FromDays(30)
            });     

我在登录后检查了cookie,并且到期时间是30天,但是在我尝试再次访问该网站几分钟/小时之后,登录状态就消失了。知道为什么吗?

1 个答案:

答案 0 :(得分:1)

似乎 var draggableAnswers = machingQuestionElement.find(".answer"); draggableAnswers.draggable({ revert: "invalid", handle: draggableAnswers.find(".Answerhandle"), axis: 'y', containment: "document" }); var dropboxes = machingQuestionElement.find(".answerDropbox"); dropboxes.droppable({ accept: draggableAnswers, drop: function (event, ui) { $(this).removeClass("border").removeClass("over"); var droppedAnswer = ui.draggable; var currentDroppbox = $(this); currentDroppbox.height(droppedAnswer.find('.AnswerText').height()); droppedAnswer.height(droppedAnswer.find('.AnswerText').height()); //Previous box var previousDropbox = droppedAnswer.parent(".answerDropbox"); previousDropbox.droppable("option", "disabled", false); previousDropbox.find(".placeholder").show(); $(droppedAnswer).detach().css({ top: 0, left: 0 }).appendTo(currentDroppbox); var currentSubQuestion = currentDroppbox.closest(".subQuestion"); machingQuestionElement.find("input[value=" + droppedAnswer.attr("id") + "]").prop('checked', false); var inputDroppedAnswer = currentSubQuestion.find("input[value=" + droppedAnswer.attr("id") + "]") inputDroppedAnswer.prop('checked', true); if (inputDroppedAnswer.attr("speciality") == "true") droppedAnswer.addClass("correctAnswer"); else droppedAnswer.removeClass("correctAnswer"); currentDroppbox.droppable("option", "disabled", true); currentDroppbox.find(".placeholder").hide(); }, over: function (event, elem) { $(this).addClass("over"); }, out: function (event, elem) { $(this).removeClass("over"); } }); 中存在错误。每当它尝试重新生成cookie时,它都会将OnValidateIdentity设置为IsPersistent,即使原始cookie是持久的。

如果我没有错,关闭并重新打开浏览器后,您可能会遇到此问题。可以通过手动添加IsPersistent声明来解决此问题,请尝试查看此link