在 ajax 调用特定操作期间跳过 RenewTicketIfOld

Dmi*_*try 3 ajax forms-authentication asp.net-mvc-4

我使用setIntervalajax 调用来每分钟检查新消息。这个调用扩展了身份验证cookie(因为我将slidingExpiration设置为true),因此身份验证永远不会过期。

这个问题有类似的问题,但解决方案是针对 .net 类项目的,我不知道如何将其应用到我的 MVC 项目中。

如果解决方案采用属性的形式,就像[DoNotExtendAuthentication]我可以执行操作一样,那就太好了,但任何帮助都会受到赞赏。

Dar*_*rov 5

public class DoNotExtendAuthenticationAttribute : ActionFilterAttribute
{
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        filterContext.HttpContext.Response.Cookies.Remove(
            FormsAuthentication.FormsCookieName
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

进而:

[Authorize]
[DoNotExtendAuthentication]
public ActionResult SomeAction()
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

  • 除非您(或其他代码)向其中添加内容,否则 Response.Cookies 数组为空。它仅用于添加/更新 cookie。通过从响应中删除表单身份验证 cookie,您实际上是在告诉浏览器身份验证 cookie 仍然相同并继续使用原始的。服务器实际上仍然更新了 auth cookie(通过创建一个新的),但新的 auth cookie 本质上已被放弃,因为新的身份验证票证/值永远不会在响应中发回。 (2认同)