4

嗨,我对 MVC 3 比较陌生,我正在注销我的应用程序,我设法登录,即使网站在另一个选项卡上打开,也保持登录状态。

我所有的观点(当然除了家)都是这样的:

<a href = "@Url.Action("Index", "Home")">Logout</a>

在我的控制器中,我有这个:

public ViewResult Logout() { Session.Abandon(); return View("Index", "Home"); }

并且应用程序没有注销,而是返回当前视图。

请帮助我了解该怎么做,我想注意我使用的是 ViewResult 而不是 ActionResult,我也不会使用 JavaScript 或 JQuery,因为我正在制作这个应用程序来展示 MVC 的工作原理。

4

2 回答 2

2

您不应该指向注销操作吗?

<a href = "@Url.Action("Logout", "Home")">Logout</a>

假设您注销操作的路径是\Home\Logout

更新:

另一种老式的方法是..

登录成功后..

Session["Login"] = true; //or any object that describes the user's identity

在您需要检查的每一页上

var login = Session["Login"];
if(Convert.ToBoolean(login)){ //or cast to your expected object
  //do something
}
else{
  //redirect to logout/login page
}

注销时,

Session["Login"] = null;
于 2012-07-28T11:10:27.133 回答
2

如果您使用表单身份验证,您还应该清除身份验证 cookie 并在注销后重定向:

public ActionResult Logout() 
{ 
    Session.Abandon(); 
    FormsAuthentication.SignOut();
    return RedirectToAction("Index", "Home"); 
}
于 2012-07-28T13:32:51.730 回答