gpt4 book ai didi

session - 无法在 Post 中找到 Get 中基于 nancyfx cookie 的 session 值,反之亦然

转载 作者:行者123 更新时间:2023-12-04 18:06:56 26 4
gpt4 key购买 nike

我正在尝试使用 Nancyfx 自托管。问题是当我在 Get["path"] 中设置 Session["key"] = value,然后我在 Post["path"] 中调用它,我变空了,反之亦然。下面是我的代码

public class Bootstrapper : DefaultNancyBootstrapper
{
protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)
{
CookieBasedSessions.Enable(pipelines);
}
}

public class TestModule : NancyModule
{
public TestModule()
{
Get["/"] = _ =>
{
Session["App1"] = "Ola";

return Session["App1"] + " " + "Hello World";//"Ola Hello World"
};

Get["/about"] = _ =>
{
return Session["App1"];//"Ola Hello World"
};

Post["/create"] = _ =>
{
return Session["App1"];//emtpty
};

Post["/add"] = _ =>
{
return Session["App1"];//empty
};
}
}

class Program
{
static void Main(string[] args)
{
var cfg = new HostConfiguration();
cfg.UrlReservations.CreateAutomatically = true;
var host = new NancyHost(new Bootstrapper(), cfg, new Uri("http://localhost:5050"));
host.Start();

Console.ReadKey();

WebRequest wr1 = HttpWebRequest.Create("http://localhost:5050/create");

wr1.Method = "POST";

wr1.GetRequestStream();

StreamReader sr1 = new StreamReader(wr1.GetResponse().GetResponseStream());

Console.WriteLine(sr1.ReadToEnd());

Console.ReadKey();

WebRequest wr2 = HttpWebRequest.Create("http://localhost:5050/add");

wr2.Method = "POST";

wr2.GetRequestStream();

StreamReader sr2 = new StreamReader(wr2.GetResponse().GetResponseStream());

Console.WriteLine(sr2.ReadToEnd());

Console.ReadKey();

host.Stop();
}
}

有了这个问题,现在我无法保存登录状态或一些必要的信息。你们有解决办法吗?

最佳答案

正如@phill 指出的那样,您发布的代码按预期工作。

事实上,您的 Bootstrap 和模块可以正常工作,并且可以从所有处理程序访问 session 。

问题是,当您创建新的 HttpWebRequests 时,先前调用的 cookie 不会保留。要将 cookie 从一个这样的请求转移到下一个请求,请做什么 this answer说。

关于session - 无法在 Post 中找到 Get 中基于 nancyfx cookie 的 session 值,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24078392/

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