如何在 C# 中将 cookie 过期设置为“会话”?

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

How do I set cookie expiration to "session" in C#?

c#sessioncookies

提问by Gio

Self-Explanatory. In PHP, the solution would be to set the cookie expiration to 0; I'm unsure about C# since it requires a DateTime value.

不言自明。在 PHP 中,解决方案是将 cookie 过期时间设置为 0;我不确定 C#,因为它需要一个 DateTime 值。

采纳答案by Michael Petrotta

The docs for Cookie.Expirescall it right out.

Cookie.Expires的文档对其进行了直接调用。

Setting the Expires property to MinValue makes this a session Cookie, which is its default value.

将 Expires 属性设置为 MinValue 使其成为会话 Cookie,这是其默认值。

cookie.Expires = DateTime.MinValue

回答by o.k.w

Do you mean any cookie or the session cookie? ASP.NET uses cookie by default for session 'management'.

你是说任何cookie还是会话cookie?ASP.NET 默认使用 cookie 进行会话“管理”。

Either you have the expiry or timeout in the web.config file, or programmatically set it using:
Session.Timeout = [x]; \\where [x] is in minutes

您可以在 web.config 文件中设置过期或超时,或者使用以下方法以编程方式设置它:
Session.Timeout = [x]; \\where [x] is in minutes

This can be called in different ways depending on your needs.

这可以根据您的需要以不同的方式调用。

回答by Vladimir Kurguzov

This doesn't work for me:

这对我不起作用:

cookie.Expires = DateTime.MinValue

This does work:

这确实有效:

cookie.Expires = default(DateTime?)