tempdata不会持续到第二个动作

发布于 2025-01-26 03:42:33 字数 2372 浏览 1 评论 0原文

我正在尝试通过几个动作传递tempdata,但我无法使其持续超过一个通过。我已经在Stackoverflow上阅读了很多有关此问题的问题,但是我无法获得他们的工作解决方案。我知道tempdata仅用于一个重定向,但建议.keep()或.peek()应允许其持续在另一个重定向上。不幸的是,这对我没有用。我还尝试了重新分配tempdata,并从第二个重定向中进行了tempdata的直接硬编码,但它仍然不会通过。我显然缺少一些东西。我的代码:

//First redirect
 public ActionResult Index(int? userId, int? reportingYear)
    {
        if (Session["State"] == null)
        {
            TempData["Timeout"] = "Your session has timed out. Please login to continue.";
            return RedirectToAction("LogOff", "Account");
        }
    }

//Second redirect
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public ActionResult LogOff()
    {
        //Delete the application cookie and clear session variables
        var cookies = Request.Cookies;
        List<string> tempCookies = new List<string>();

        foreach (string cookie in cookies)
        {
            if (cookie.ToString() != "quailCoord")
            {
                tempCookies.Add(cookie);
            };
        }

        foreach (string cookie in tempCookies)
        {
            HttpCookie deleteCookie = Request.Cookies[cookie];
            deleteCookie.Expires = DateTime.Now.AddDays(-1);
            Response.Cookies.Add(deleteCookie);
        }

        Session.Abandon();
        AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
        
        //When checking that the key exists, it does and enters the if statement to keep the data
        if (TempData.ContainsKey("Timeout")
        {
            TempData.Keep("Timeout");
        }
        return RedirectToAction("Login");
    }

//Third action
public ActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;
        //It does not find any TempData keys
        if (TempData.ContainsKey("Timeout"))
        {
            ViewBag.Timeout = TempData["Timeout"] as string;
        }
        return View();
    }

我还尝试过这些代替tempdata.keep(“ timeout”)方法:

tempdata.peek(“ timeout”)

tempdata [''超时” = tempdata [“ timeout”]

tempdata [“ timeout”] =“您的会话已经定时登录 >登录()操作。 tempdata在输入该操作时始终是空的。调试时,我跨越返回redirecttoAction(“ login”)行的分钟,tempdata中的计数转为0。我缺少什么?删除cookie是一个问题吗?

I'm trying to pass TempData through a couple actions, but I can't get it to persist beyond one pass. I've read a ton of questions on StackOverflow about this, but I just can't get their solutions to work. I know TempData only persists for one redirect, but it is suggested that .Keep() or .Peek() should allow it to persist on another redirect. That unfortunately has not worked for me. I have also tried reassigning TempData and straight hard-coding of TempData from the second redirect and it still will not pass through. I'm obviously missing something. My code:

//First redirect
 public ActionResult Index(int? userId, int? reportingYear)
    {
        if (Session["State"] == null)
        {
            TempData["Timeout"] = "Your session has timed out. Please login to continue.";
            return RedirectToAction("LogOff", "Account");
        }
    }

//Second redirect
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public ActionResult LogOff()
    {
        //Delete the application cookie and clear session variables
        var cookies = Request.Cookies;
        List<string> tempCookies = new List<string>();

        foreach (string cookie in cookies)
        {
            if (cookie.ToString() != "quailCoord")
            {
                tempCookies.Add(cookie);
            };
        }

        foreach (string cookie in tempCookies)
        {
            HttpCookie deleteCookie = Request.Cookies[cookie];
            deleteCookie.Expires = DateTime.Now.AddDays(-1);
            Response.Cookies.Add(deleteCookie);
        }

        Session.Abandon();
        AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
        
        //When checking that the key exists, it does and enters the if statement to keep the data
        if (TempData.ContainsKey("Timeout")
        {
            TempData.Keep("Timeout");
        }
        return RedirectToAction("Login");
    }

//Third action
public ActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;
        //It does not find any TempData keys
        if (TempData.ContainsKey("Timeout"))
        {
            ViewBag.Timeout = TempData["Timeout"] as string;
        }
        return View();
    }

I have also attempted these in place of the TempData.Keep("Timeout") method:

TempData.Peek("Timeout")

TempData["Timeout"] = TempData["Timeout"]

TempData["Timeout"] = "Your session has timed out. Please login to continue."

None of these methods pass to the Login() action. TempData is always empty upon entering that action. When debugging, the minute I step over the return RedirectToAction("Login") line, the count in TempData turns to 0. What am I missing? Is deleting the cookies a problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

压抑⊿情绪 2025-02-02 03:42:33

因为tempdata将将数据存储在session可能存储 sessionId 在cookie中,如果您删除该服务器将创建另一个 sessionIdId (对象)为您而不是原始一个。

因此,如果您想将tempdata保留到多个操作中,我们可能需要保留 sessionId cookie的值。

从您的代码中,我们可以尝试添加
检查cookie密钥的判断是否不删除cookie。

foreach (string cookie in cookies)
{
    if (cookie.ToString() != "quailCoord" && cookie.ToString() != "ASP.NET_SessionId")
    {
        tempCookies.Add(cookie);
    };
}

Because TempData will store data in Session object which might store SessionId in cookie, if you delete that server will create another SessionId (object) for you instead of the original one.

so that if you want to keep TempData to multiple actions we might need to keep SessionId value from the cookie.

From your code, we can try to add
a judgement to check cookie key whether if yes don't delete the cookie.

foreach (string cookie in cookies)
{
    if (cookie.ToString() != "quailCoord" && cookie.ToString() != "ASP.NET_SessionId")
    {
        tempCookies.Add(cookie);
    };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文