我有一个 MVC3 网络应用程序,它有效地“固定”到经典的 ASP 网站(即旧页面是经典的 asp,新页面是 ASP.Net MVC)。 该站点要求用户登录,因此为了保护新的 MVC 页面,我检查了一个 cookie 并使用 id 从经典 ASP 登录页面创建的数据库中检索 session 数据。
因为用户可以在新旧页面之间切换,而且我不会在这两个应用程序之间共享 session 数据,所以我在名为 LogIn 的操作方法中检查 cookie 并检索每个请求的数据:
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");
}
}
我收到“不允许子操作执行重定向操作”,我明白这一点,但我该如何解决这个问题?我应该在别处进行 cookie 检查吗?我应该在哪里进行 session 管理?
最佳答案
描述
对了,你可以使用HttpContext.Response.Redirect
来解决这个问题。
示例
改变
return Redirect("http://localhost/index.asp");
到
HttpContext.Response.Redirect("http://localhost/index.asp");
return EmptyResult();
更多信息
关于c# - 在哪里检查 MVC3 应用程序中的 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8762581/