gpt4 book ai didi

c# - 在请求期间放弃 session

转载 作者:太空宇宙 更新时间:2023-11-03 15:20:05 27 4
gpt4 key购买 nike

<分区>

我对我的 ASP.NET MVC 应用程序有这样的要求:

Session created before authentication should be closed and new session, with new ID, should be started

所以它应该像这样工作:

  1. 使用授权信息将用户重定向(GET 请求)到我的应用程序( Controller 中的某些方法)
  2. 结束旧 session ,开始新 session
  3. Auth,保存用户数据到session

让我们看看 Controller :

public ActionResult Login(string token) {
Session.Abandon(); // I want new session!

// Some auth stuff

Session["this"] = "x";
Session["that"] = "y";

return View();
}

在调试时,我可以看到 session 值“this”和“that”设置为“x”和“y”。但是现在让我们来看看这个方法使用的 View :

@{
var @this = Session["this"]; // Wut? @this is null
var that = Session["that"]; // Wut? that is null
}

所以,一点挖掘给了我答案:

Abandon causes the End event to be raised. A new Start event will be raised on the next request. https://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.abandon%28v=vs.110%29.aspx

似乎生命周期是这样的:

  1. "Session.Abandon()"- 标记为关闭的 session
  2. 设置 session 值。
  3. “返回 View ()”
  4. session 关闭并且(对此不确定)新 session 开始
  5. 查看 - Controller 中设置的 session 值丢失。

完全不能接受。我该如何克服这个问题?

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com