在哪里查看MVC3应用程序中的cookie

本文关键字:cookie 应用程序 MVC3 在哪里 | 更新日期: 2023-09-27 18:19:43

我有一个MVC3 web应用程序,它被有效地"固定"到一个经典的ASP网站上(即旧页面是经典的ASP,新页面是ASP.Net MVC)。该网站要求用户登录,因此为了保护新的MVC页面,我检查了一个cookie,并使用id从经典ASP登录页面创建的数据库中检索会话数据。

因为用户可以在旧&新的页面,我没有在两个应用程序之间共享会话数据,我检查cookie&在我的操作方法LogIn:中检索每个请求的数据

public PartialViewResult LogIn()
        {
            var cookieId = DecodeCookieId(System.Web.HttpContext.Current.Request.Cookies["cookiename"].Value);
            LoggedInViewModel viewModel = new LoggedInViewModel
            {
                Session = sessionRepository.Sessions.FirstOrDefault(s => s.GGAPSession_ID == cookieId)
            };
            return PartialView(viewModel);
        }

这将数据传递给ViewModel,它在每个MVC页面的顶部显示登录的用户名,这就是为什么我认为这是一个进行检查的好地方。

当我尝试修改LogIn()来检查cookie时,问题就出现了;如果找不到重定向:

public ActionResult LogIn()
{
   if (System.Web.HttpContext.Current.Request.Cookies["cookiename"] != null)
  {
     //  same method contents as above
  }
  else
  {
    return Redirect("http://localhost/index.asp");
  }
}

我得到了一个"儿童操作不允许执行重定向操作",我理解这一点,但我该如何绕过这一点?我应该在其他地方检查饼干吗?我应该在哪里进行会话管理?

在哪里查看MVC3应用程序中的cookie

描述

对,你可以用HttpContext.Response.Redirect来解决这个问题。

样品

更改

return Redirect("http://localhost/index.asp");

HttpContext.Response.Redirect("http://localhost/index.asp");
return EmptyResult();

更多信息

  • MSDN-HttpResponse.RRedirect方法

我认为您可以在mvc站点中使用全局操作过滤器来实现这一点。这是《ASP.NET mvc in action》一书中所述的一般原则。在中检索

OnActionExecuting

如果cookie存在。否则,

filterContext.Result = new RedirectResult("http://localhost/index.asp")