如何退出会议MVC Razor视觉工作室

我正试图从MVC Razor中的会话注销,这是我目前在MainController中的内容:

[HttpPost] public ActionResult Login(Users user) { if (ModelState.IsValid) { if (ValidateUser(user.Email, user.Password)) { FormsAuthentication.SetAuthCookie(user.Email, false); return RedirectToAction("Index", "Members"); } else { ModelState.AddModelError("", ""); } } return View(); } private bool ValidateUser(string Email, string Password) { bool isValid = false; using (var db = new ShareRideDBEntities()) { var User = db.tblProfiles.FirstOrDefault(u => u.PROF_Email == Email); var ut = db.tblProfilesTypes.FirstOrDefault(t => t.TPE_ID == User.PROF_UserType); if (User != null) { if (User.PROF_Password == Password) { Session["UserID"] = User.PROF_UserID; Session["Name"] = User.PROF_FirstName; Session["Email"] = User.PROF_Email; Session["FullName"] = User.PROF_FirstName + " " + User.PROF_LastName; isValid = true; } } } return isValid; } 

有了这个,我可以登录用户并将其重新发送到他的UserCP或用户控制面板。

我有它,所以如果用户没有登录,他们将无法在我的MembersController中使用此代码访问成员区域:

 public ActionResult UserCP() { if (Session["UserID"] == null) { return RedirectToAction("Index", "Main"); } else { return View(); } } public ActionResult LogOut() { FormsAuthentication.SignOut(); return RedirectToAction("index", "main"); } 

如果用户尚未登录,它会将用户重定向回主索引页面,但是当我测试注销按钮时,它会正常重定向,但我仍然可以返回到用户控制面板不希望它发生。

我当然补充说

 using System.Web.Security; 

使用FormAuthentication.SignOut();

如果有人能解释这一点,请提前感谢。

FormsAuthentication.SignOut(); 您需要调用Session.Abandon()来清除当前会话并在下一个请求上重新创建新会话

 public ActionResult LogOut() { FormsAuthentication.SignOut(); Session.Abandon(); // it will clear the session at the end of request return RedirectToAction("index", "main"); }