asp.net-mvc 为什么 ASP.NET MVC 中的 SessionID 不断变化?

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

SessionID keeps changing in ASP.NET MVC why?

asp.net-mvcsession

提问by dswatik

I am trying to keep track of something and using the SessionID as they key to that object

我正在尝试跟踪某些内容并使用 SessionID 作为该对象的关键

However the SessionID every 2-3 reqiests changes shouldn't it remain the same?

但是,每 2-3 次请求更改的 SessionID 不应该保持不变吗?

HttpContext.Session.SessionID

Is the code I am using.

是我正在使用的代码。

回答by Maxam

I've seen that happen even without MVC. If I remember correctly, ASP.NET keeps assigning new session ids until you place something into the Session variable.

我已经看到即使没有 MVC 也会发生这种情况。如果我没记错的话,ASP.NET 会一直分配新的会话 ID,直到您将某些内容放入 Session 变量中。

回答by jrojo

You should initialize the Session object within Global.asax.cs.

您应该在 内初始化 Session 对象Global.asax.cs

void Session_Start(object sender, EventArgs e)
{
    HttpContext.Current.Session.Add("__MyAppSession", string.Empty);
}

This way the session will not change unless your browser window is closed.

这样除非您的浏览器窗口关闭,否则会话不会改变。

回答by robnardo

I am working on a .NET MVC cart application and I added

我正在开发一个 .NET MVC 购物车应用程序,我添加了

Session["myVar"] = "1234";

to the Page_Load method found in the Default.aspx.cs code. I also added

到 Default.aspx.cs 代码中的 Page_Load 方法。我还加了

<%=  this.Session.SessionID %>

to the Site.Master footer. I then rebuilt my app and browsed the various pages of my app and the footer displays the same session id for all pages as expected!

到 Site.Master 页脚。然后我重建了我的应用程序并浏览了我的应用程序的各个页面,并且页脚按预期为所有页面显示了相同的会话 ID!

回答by ScottEg

If you look the seession ID cookie is not even sent to the browser unless it's used on the server.

如果您看一下,除非在服务器上使用,否则会话 ID cookie 甚至不会发送到浏览器。

So when a page roundtrips there is no current session ID cookie, so a new session ID is created, hence it is random.

因此,当页面往返时没有当前会话 ID cookie,因此会创建一个新的会话 ID,因此它是随机的。

This is logical, why bother tying up the app to a session if the session is not in use?

这是合乎逻辑的,如果会话未使用,为什么还要将应用程序绑定到会话?

回答by MrJavaGuy

I would look into using TempData to keep track of something.

我会考虑使用 TempData 来跟踪某些东西。

回答by Hrvoje Hudo

Try with adding machine key into your web.config:
Online key generator: http://aspnetresources.com/tools/keycreator.aspxIt seems that server resets machine key for client and generates new one with new session id, every few minutes, which is much lower than it should. Don't know if that's bug or feature:)
Also, you can increase your session state timeout, which is i think 20min by default.

尝试将机器密钥添加到您的 web.config 中:
在线密钥生成器:http: //aspnetresources.com/tools/keycreator.aspx似乎服务器为客户端重置机器密钥并每隔几分钟生成一个具有新会话 ID 的新密钥,这比它应该的要低得多。不知道这是错误还是功能:)
此外,您可以增加会话状态超时,我认为默认为 20 分钟。

回答by Flea

I was having the same problem using ASP.NET Web Forms. I just had to add a global.asaxfile to the solution and the fixed it for me.

我在使用 ASP.NET Web 窗体时遇到了同样的问题。我只global.asax需要在解决方案中添加一个文件并为我修复它。

回答by sobelito

Summing up answers from @jrojo and @Maxam above, with what I am using.

总结上面@jrojo 和@Maxam 的答案,以及我正在使用的内容。

I am using AWS DynamoDB as the session store (out of scope of the question a little, but gives sample).

我使用 AWS DynamoDB 作为会话存储(有点超出问题的范围,但提供了示例)。

Add package via NUGET: Install-Package AWS.SessionProvider

通过 NUGET 添加包:Install-Package AWS.SessionProvider

Update Web.config to have keys in appSettings:

更新 Web.config 以在 appSettings 中包含密钥:

<add key="AWSAccessKey" value="XXX" />
<add key="AWSSecretKey" value="YYY" />

And session provider to system.web:

和 system.web 的会话提供者:

<sessionState timeout="20"
              mode="Custom"
              customProvider="DynamoDBSessionStoreProvider">
  <providers>
    <add name="DynamoDBSessionStoreProvider"
      type="Amazon.SessionProvider.DynamoDBSessionStateStore, AWS.SessionProvider"
      AWSProfilesLocation=".aws/credentials"
      Table="ASP.NET_SessionState"
      Region="us-east-1"
      />
  </providers>
</sessionState>

Add anything to session in global.asax on session start:

在会话开始时向 global.asax 中的会话添加任何内容:

void Session_Start(object sender, EventArgs e) {
  HttpContext.Current.Session.Add("somethingToForceSessionIdToStick", string.Empty);
}

Verify by adding this to razor of any page. Refresh that page, then open an ignito window and see a different session:

通过将此添加到任何页面的剃刀进行验证。刷新该页面,然后打开一个 Ignito 窗口并查看不同的会话:

@HttpContext.Current.Session.SessionID

BobsYourUncle

鲍勃你的叔叔