如何在mvc中注销后阻止页面打开 [英] How can i prevent the page open after logout in mvc
问题描述
我有一个登录页面。登录后,我被重定向到仪表板页面。注销后,如果我在地址栏中输入网址,则会显示该页面。我如何在MVC项目中阻止这种情况?
任何人都可以帮助我
我是什么尝试过:
我尝试使用动作过滤器
Hi,
I have a login page . after login i am redirected to a dashboard Page. After logout ,if i enter the url in the address bar, the page is showing. How can i prevent this in MVC project?
Can anybody help me
What I have tried:
I tried by using action filters
推荐答案
有三种方法为了实现我已经在我自己的项目中使用的功能:
(1):将此代码添加到您的每个ActionResult方法中。使用此用户无法通过在LogOut之后单击浏览器的向后图标返回
There are three ways for doing that functionality which i m already using in my own project:
(1):Add this code to your every "ActionResult" method. By using this user can not go back by clicking backward icon of browser after "LogOut"
public void SetPageCacheNoStore()
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.AppendCacheExtension("no-store, must-revalidate");
Response.AppendHeader("Pragma", "no-cache");
Response.AppendHeader("Expires", "0");
}
(2):在启动时的每个方法中使用此代码来检查Session是否被销毁。
(2):Use this code in every method at startup for checking Session destroyed or not.
if (Session["UserName"] == null)
{
return RedirectToAction("Login", "Account");
}
(3):在每个视图或部分视图中使用此代码,例如补充工具栏在每个视图中使用。
(3):Use this code in every view or in a "Partial" view like "Sidebar uses in every view".
string viewNames = Session["UserName"];
if (ViewNames == null)
{
<script src="~/Scripts/jquery-1.11.1.min.js"></script>
<script>
(document).ready(function(){
window.location.href =../Account/Login;
});
< / script>
}
(document).ready(function () { window.location.href = "../Account/Login"; }); </script> }
我希望通过使用所有这些解决方案,您的问题将得到解决。
I hope by using all of these solutions your problem will be resolved.
看看这个。在申请之前你需要先学习
ASP.NET MVC中的自定义身份验证和授权 [ ^ ]
Have a look at this. You need to learn first before applying it
Custom Authentication and Authorization in ASP.NET MVC[^]
这篇关于如何在mvc中注销后阻止页面打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!