asp.net-mvc 从 MVC 注销

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1951784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:59:38  来源:igfitidea点击:

Logout from MVC

asp.net-mvc

提问by pankaj

I have a MVC application and I have to logout. To logout from the application I have created a link on te master page

我有一个 MVC 应用程序,我必须注销。要从应用程序注销,我在母版页上创建了一个链接

[ <a id="A2" name="lnkLogout" href="http://localhost:1234/Home/LogOut" >Logout</a> ]

and created the LoutOut action in the controller page

并在控制器页面中创建了 LoutOut 操作

 public ActionResult LogOut()
        {
            Session.Clear();
            Session.Abandon();
            Redirect("http://AnotherApplicaton/Home/LogOut");
        }

Now when I click on the LogOut link its redirect to the LogOut action and in the LogOut action its delete all the session, but when I click on the back button of the browser its get back to the previous page and sessions are still alive. Does anyone have the solution of this problem.

现在,当我单击 LogOut 链接时,它会重定向到 LogOut 操作,并在 LogOut 操作中删除所有会话,但是当我单击浏览器的后退按钮时,它会返回上一页并且会话仍然存在。有没有人有这个问题的解决方案。

回答by nWorx

if you're using forms authentication then this is what you are looking for...

如果您使用的是表单身份验证,那么这就是您要寻找的...

public ActionResult LogOut()
        {
            Session.Clear();
            FormsAuthentication.SignOut();
            Redirect("http://AnotherApplicaton/Home/LogOut");
        }

hth

回答by Hemanshu Bhojak

The pages you visit in your browser are cached depending upon your caching settings in the browser. You need to prevent caching in ASP.net MVC so that they are not cached by the browser. After you do that try clearing your browsers cache and try loading the page again. Logout and try the back button. You should get a message saying that the page no longer exists or something.

您在浏览器中访问的页面会根据浏览器中的缓存设置进行缓存。您需要防止在 ASP.net MVC 中缓存,以便浏览器不会缓存它们。执行此操作后,请尝试清除浏览器缓存并再次尝试加载页面。注销并尝试后退按钮。您应该收到一条消息,说该页面不再存在或什么的。

There are many ways of preventing your ASP.net pages from getting cached in the browser. One such way is to do this before the page is rendered.

有很多方法可以防止您的 ASP.net 页面在浏览器中缓存。一种这样的方法是在呈现页面之前执行此操作。

this.Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1))
this.Response.Cache.SetCacheability(HttpCacheability.NoCache)
this.Response.Cache.SetNoStore()

回答by Graviton

The sessions are notalived; they are just cached.

会话活跃;它们只是被缓存。

You can try to force refresh your page (ctrl+f5) and check this.

您可以尝试强制刷新您的页面 (ctrl+f5) 并检查这一点。

回答by pankaj

Hi got the solution. Actually I have used toke to login into my module and the token timeout was 5 second. If I Logged out and click on the back button of the browser before the 5 second then the Session_Start() event of glowal.ascx.cs is called again and read the token again and the create the session again. And if I click on the back button after the 5 second then session does not create. To solve this problem I does not allow the user to go to back through the browser. For this I have used the following code

嗨得到了解决方案。实际上,我已经使用令牌登录到我的模块,令牌超时为 5 秒。如果我在 5 秒之前注销并单击浏览器的后退按钮,则再次调用 glowal.ascx.cs 的 Session_Start() 事件并再次读取令牌并再次创建会话。如果我在 5 秒后单击后退按钮,则不会创建会话。为了解决这个问题,我不允许用户通过浏览器返回。为此,我使用了以下代码

<script type="text/javascript">
history.go(1);
</script>