c# - 在 MVC C# 中实现 PHP 会话
问题描述
目前,我正在使用 MVC C# 开发一个 Web 应用程序。我想问是否有办法在 MVC 中实现 PHP Session。我正在谈论的会话的使用就像,当你只写这样的链接或路径时,即使你没有使用帐户登录,你也不会被重定向到https://localhost:44360/Login/Login
那个https://localhost:44360/Home/Home
网页Home
页。
登录帐户后,我被重定向到正确的主页,之后,我注销了帐户并尝试键入https://localhost:44360/Home/Home
,但不幸的是它被重定向了,甚至单击了浏览器的后退按钮。
我目前正在处理下面的代码。
登录控制器.cs
[HttpPost]
public ActionResult Login(LoginModel userInfo, FormCollection collection, string returnUrl)
{
ILogicInterface<LogicalUserInput, LogicalSystemResult> iLogic = new UserLoginCheck();
LogicalUserInput userInput = new LogicalUserInput();
_ = new LogicalSystemResult();
try
{
userInput[typeof(LoginModel).FullName] = userInfo;
LogicalSystemResult systemResult = iLogic.DoProcess(userInput);
bool userCheckExist = systemResult.ResultCode != LogicalSystemResult.RESULT_CODE_ERR_DATA_NOT_EXIST;
if (userCheckExist)
{
UserLoginModel loginModel = systemResult[typeof(UserLoginModel).FullName] as UserLoginModel;
Session["userInfo"] = loginModel;
FormsAuthentication.SetAuthCookie(loginModel.email, true);
if (!string.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
if (loginModel.AccountType == 0) {
return RedirectToAction("Home", "Home");
} else {
return RedirectToAction("Error", "Error");
}
}
}
else
{
TempData.Clear();
TempData["Title"] = "Error!";
TempData["Message"] = " Invalid Username Or Password.";
return View();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return RedirectToAction("Error", "Error");
}
}
HOME.cshtml 中的注销按钮
<a id="cmdLogOff">
<i class="feather icon-log-out"></i>Logout
</a>
注销按钮的点击事件
<script>
$("#cmdLogOff").on("click", function () {
$("#HomeView").submit();
});
</script>
@using (Ajax.BeginForm("LogOut",
null,
new AjaxOptions
{
},
new { id = "HomeView" }))
{
}
HOMEController.cs
[Authorize]
public ActionResult Home()
{
if (Session["userInfo"] == null) {
return RedirectToAction("Error", "Error");
} else {
UserLoginModel userLoginModel = Session["userInfo"] as UserLoginModel;
TempData["user"] = userLoginModel.lastName + ", " + userLoginModel.firstName + " " + userLoginModel.middleName;
return View();
}
}
[Authorize]
[HttpPost]
public ActionResult LogOut() {
try {
Session.Abandon();
FormsAuthentication.SignOut();
Session["userInfo"] = null;
return RedirectToAction("Login", "Login");
} catch (Exception ex) {
Console.WriteLine(ex.Message);
return View();
}
}
这里的主要问题是,如果他/她只键入他/她想在没有帐户的情况下访问的链接,我怎么能不重定向用户。并且注销账户后,即使用户点击浏览器背面,主页也不会显示?
解决方案
推荐阅读
- python - css的find_element没有按预期工作
- c++ - C中的宏替换
- javascript - 全屏/游戏上的 Chrome 通知
- reactjs - 如何在 react-froala-wysiwyg 中更改图像的样式?
- javascript - 如何从getElementbyId获取值到输入框
- angular - 如何将选定的复选框保存为离子中的数组
- testing - 有哪些方式可以为 K8s 提供外网连接?
- javascript - 将 cell.js 演示转换为 Brython 的问题
- hasura - 有没有办法为所有 hasura 查询和突变编写中间件?
- opencv - 没有名为“cv2”的模块作为树莓派