C# 当 Session.Abandon() 不起作用时,您如何以编程方式结束 asp.net 中的会话?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/852942/
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-08-05 04:13:59  来源:igfitidea点击:

How do you programmatically end a session in asp.net when Session.Abandon() doesn't work?

c#asp.netauthenticationsessionmembership

提问by Slim

Session.Abandon() doesn't seem to do anything. You would expect the Session_end event to fire when Session.Abandon() is called.

Session.Abandon() 似乎没有做任何事情。您会期望在调用 Session.Abandon() 时触发 Session_end 事件。

采纳答案by Noldorin

This is most likely because your SessionModeis notInProc(the only one that can detect when a session ends).

这很可能是因为您SessionMode不是InProc(唯一一个可以检测在会话结束时)。

Quoted from MSDN:

引用自MSDN

Session Events

ASP.NET provides two events that help you manage user sessions. The Session_OnStart event is raised when a new session starts, and the Session_OnEnd event is raised when a session is abandoned or expires. Session events are specified in the Global.asax file for an ASP.NET application.

The Session_OnEnd event is not supported if the session Mode property is set to a value other than InProc, which is the default mode.

会话事件

ASP.NET 提供了两个事件来帮助您管理用户会话。Session_OnStart 事件在新会话开始时引发,Session_OnEnd 事件在会话被放弃或过期时引发。会话事件在 ASP.NET 应用程序的 Global.asax 文件中指定。

如果会话模式属性设置为 InProc 以外的值,则不支持 Session_OnEnd 事件,这是默认模式。

回答by Serapth

Session.Abandon() is the way to end a session. What is the problem you are encountering?

Session.Abandon() 是结束会话的方式。您遇到的问题是什么?

If its Back button related, that is a completely different issue ( page doesn't postback on Back, instead it runs one from clientside cache so no server side methods will execute).

如果它的“后退”按钮相关,那是一个完全不同的问题(页面不会在“后退”上回发,而是从客户端缓存运行一个,因此不会执行任何服务器端方法)。

Additionally, Session_End is problematic. It will only fire on Session.Abandon() when using InProc sessions, so if you are using a different Session mode, it cannot be relied on. Otherwise, Session_End will fire when SessionTimeout is reached ( default is 20 minutes I believe, configured in Web.Config ).

此外,Session_End 是有问题的。它只会在使用 InProc 会话时在 Session.Abandon() 上触发,因此如果您使用不同的会话模式,则不能依赖它。否则, Session_End 将在达到 SessionTimeout 时触发(我相信默认为 20 分钟,在 Web.Config 中配置)。

回答by RYFN

Have you tried using the following?

您是否尝试过使用以下方法?

System.Web.Security.FormsAuthentication.SignOut();

This will clear cookies used for form authentication, although may not be what you're looking for.

这将清除用于表单身份验证的 cookie,尽管可能不是您要查找的内容。

You may need to use this in addtion to Session.Abandon()

您可能需要在 Session.Abandon() 之外使用它

回答by diadiora

It depends, if you have your application at 2 servers: 1 WebApplication that has its own session, and the second WS or WCF application that also has its own session, how it was in an application on which I was working once. Than if you have this case, the session must be ended at second point, and the first is ended the timeout appears. At least you'll have to use a token and to keep the list of tokens, of active sessions. May be it is your case. good luck. PS. To kill the session manage it in the second server.

这取决于,如果您的应用程序在 2 个服务器上:1 个 WebApplication 有自己的会话,第二个 WS 或 WCF 应用程序也有自己的会话,它在我曾经工作过的应用程序中的情况如何。如果您遇到这种情况,则会话必须在第二个点结束,第一个结束时会出现超时。至少您必须使用令牌并保留活动会话的令牌列表。可能是你的情况。祝你好运。附注。要终止会话,请在第二个服务器中管理它。

回答by azamsharp

Here is an article which talks about when the session end is called:

这是一篇关于何时调用会话结束的文章:

http://www.highoncoding.com/Articles/108_When_is_Session_End_Called__.aspx

http://www.highoncoding.com/Articles/108_When_is_Session_End_Called__.aspx

回答by Axl

If sessions appear to persist you might try (in web.config):

如果会话似乎持续存在,您可以尝试(在 web.config 中):

<sessionState regenerateExpiredSessionId="true">