0
我在我的应用程序控制器执行OnActionExecuted,检查用户是否仍然登录。当用户不再登录会话需要被销毁并被重新引导回主页而不必进入新页面。 (所以当前的请求必须被取消)。ASP.NET MVC-服务器无法修改HTTP头后发送的Cookie
这是我的代码:
if (System.Web.HttpContext.Current.Session.IsNewSession)
{
if (System.Web.HttpContext.Current.Session["Userid"] != null)
{
// Check userid session
filterContext.Controller.TempData["sessionError"] = 1;
System.Web.HttpContext.Current.Session.RemoveAll();
System.Web.HttpContext.Current.Response.BufferOutput = true;
System.Web.HttpContext.Current.Response.Flush();
RedirectToAction("Index", "Home");
return;
}
}
但是我这段代码获得的问题是,它给了我一个例外:
Message:
Server cannot modify cookies after HTTP headers have been sent.
Source:
at System.Web.HttpResponse.BeforeCookieCollectionChange()
at System.Web.HttpCookieCollection.Add(HttpCookie cookie)
at Topsite.Classes.GlobalFunctions.SetCookie(String name, String value, DateTime endDate)
at Topsite.Controllers.ApplicationController.OnActionExecuted(ActionExecutedContext filterContext)
at System.Web.Mvc.Controller.System.Web.Mvc.IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<>c__DisplayClass2a.<BeginInvokeAction>b__20()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult)
有什么建议?提前致谢。
我确实发现了一些其他问题,有同样的问题,我跟着他们,但我仍然收到异常。这就是我寻求帮助的原因。