Asp.net core ASP.NET 5会话不';它不会持续多久?
我试图让会话使用ASP.NET5,但它们从不存储数据。看起来每个调用都在将会话cookie更新为新值,并忽略客户端传递的cookie 下面是我如何在Asp.net core ASP.NET 5会话不';它不会持续多久?,asp.net-core,session-cookies,asp.net-core-mvc,Asp.net Core,Session Cookies,Asp.net Core Mvc,我试图让会话使用ASP.NET5,但它们从不存储数据。看起来每个调用都在将会话cookie更新为新值,并忽略客户端传递的cookie 下面是我如何在Startup.cs中的ConfigureServices中设置它们的: // Add framework services. services.AddMvc(); services.AddCaching(); services.AddSession(options => { options.IdleTimeout = TimeSpan
Startup.cs
中的ConfigureServices
中设置它们的:
// Add framework services.
services.AddMvc();
services.AddCaching();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromDays(30);
options.CookieName = "MySessionCookieName";
});
我将其添加到Configure
(在app.UseMvc()之前,按照说明),并尝试将其移动到顶部,结果相同:
app.UseSession();
尝试注入ISession
会出现错误,因此我使用了HttpContext.Session
,就像文档中所说的那样。我编写了一个简单的api操作,该操作应将当前日期设置为“now”会话密钥,并对注入的内存缓存执行相同的操作:
sw.WriteLine("_Env.EnvironmentName: {0}", _Env.EnvironmentName);
sw.WriteLine("Keys: {0}", sess.Keys.Count());
sw.WriteLine("_Session['now'] (before update): {0}", sess.GetValue<DateTime>("now"));
sess.SetValue<DateTime>("now", DateTime.Now);
sw.WriteLine("_Session['now'] (after update): {0}", sess.GetValue<DateTime>("now"));
sw.WriteLine("Cache['now'] (before setting): {0}", _Cache.Get<DateTime>("now"));
_Cache.Set<DateTime>("now", DateTime.Now);
sw.WriteLine("Cache['now'] (after setting): {0}", _Cache.Get<DateTime>("now"));
查看Chrome网络选项卡,我看到我在每个请求中传递cookie,但响应总是设置一个新cookie。知道我可能做错了什么吗?我在ASP.Net 5 RC1,更新1上使用了Session,没有问题。我在代码中看到的关键问题是,您需要在startup.cs中的MVC之前添加所有会话内容
为了完整起见,以下是我基于“Web应用程序”模板所做的工作
添加Microsoft.AspNet.Session
会话需要nuget包。要获得它,请将其添加到project.json中的依赖项部分
"Microsoft.AspNet.Session": "1.0.0-rc1-final"
在Startup.cs中
将这两行添加到services.AddMvc()之前代码>在公共无效配置服务(iSeries收集服务)
将此添加到app.UseMvc(…
在public void Configure(IApplicationBuilder应用程序、IHostingEnvironment环境、ILoggerFactory)中的
字符串到字节[]的帮助程序
现在,会话对象需要byte[]
对象,因此所有内容都需要转换为byte[]
,因此我设置了以下两个助手函数:
static byte[] StringToBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
static string BytesToString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
在我的控制器中
现在简单点
设置会话
HttpContext.Session.Set("MySessionKey", StringToBytes("fibble"));
朗读课文
byte[] bytes;
HttpContext.Session.TryGetValue("MySessionKey", out bytes);
var mySessionKeyValue = BytesToString(bytes);
想要一个有效的解决方案吗?
您可以在以下GitHub repo中看到所有这些代码:是sess.GetValue()
和sess.SetValue()
由您编写?只是问一下,因为在一个新的RC1 web应用程序中,会话与您描述的配置一起工作,尽管只有的扩展方法。在我的测试中,我尝试使用session.GetString
和session.SetString
是,由我编写。它们所做的只是序列化类型,然后再提取值r设置它(\u会话['now'](更新后)
)。查看“网络”选项卡,它显示会话cookie每次都会被重置,因此我认为这一定与此有关。我创建了一个新的RC1 web应用程序,像您一样配置了会话,并在默认的主/索引操作中设置/读取值。它工作正常,第一次设置cookie并重新加载页面时会发送cookie,因此我查看以前存储在会话中的值。您如何调用api操作?这是一个get,所以我只是在chrome中使用URL。在“网络”选项卡中,我看到会话cookie已发送,但响应设置了不同的cookie。下一次调用将发送设置cookie,但响应设置了新cookie。我认为这可能是由于我的cookie如此mehow.Startup.cs
中也有UseCookieAuthentication()
。我们停止使用会话,因为我无法让它工作,但我在cookie身份验证方面也遇到了问题。我从会话中获得了10个cookie,用于localhost:5000
,来自无关的项目(和竹子)。我清除了我的cookie,cookie身份验证开始正常工作,尽管我们不再使用会话,所以我不能说这是否也能修复它。只需澄清一点,services.AddCaching();
和services.AddSession();
不需要在services.AddMvc()之前
,它们可以在前面或后面。您可以通过查看以下位置的示例应用程序代码来验证这一点:,app.UseSession()
必须位于app.UseMvc(…
static byte[] StringToBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
static string BytesToString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
HttpContext.Session.Set("MySessionKey", StringToBytes("fibble"));
byte[] bytes;
HttpContext.Session.TryGetValue("MySessionKey", out bytes);
var mySessionKeyValue = BytesToString(bytes);