简体   繁体   English

Asp.Net Core 2.0会话破坏了每个构建

[英]Asp.Net Core 2.0 session destroyed each build

I am coming from PHP ecosystem, and now i want upgrade my skills to Asp.Net Core 2.0 . 我来自PHP生态系统,现在我想将自己的技能升级到Asp.Net Core 2.0 There a problem with sessions since these will be destroyed on each build , and that's is really annoyng during development . 会话存在问题, 因为这些会话将在每个构建版本中销毁 ,这在开发过程中确实很烦人。

Asp.net authentication system isn't destroyed at builds/restart , while sessions are. Asp.net身份验证系统不会在build / restart时销毁,而会话会被销毁。

There a way to make Sessions persistent like the Auth behavior? 有没有办法使会话像Auth行为一样持久?

My workaround is; 我的解决方法是; Since i'm using asp.net Auth i could store a unique id and load the session (containing custom object) from the database. 由于我使用的是asp.net Auth,因此我可以存储唯一的ID并从数据库中加载会话(包含自定义对象)。 Then save it again on changes... 然后在更改时再次保存...

Note: My application is multi-user so i need to referee the user ID from Session on each request to the database to select related data. 注意: 我的应用程序是多用户的,因此我需要在每次请求数据库时参考Session中的用户ID来选择相关数据。

ASP.NET Sessions were never related to authentication. ASP.NET会话从未与身份验证相关。 They use their own storage to store temporary data that is used during a singel user session. 他们使用自己的存储设备来存储在singel用户会话期间使用的临时数据。

In ASP.NET Core the Session middleware always uses the service registered for IDistributedCache. 在ASP.NET Core中,会话中间件始终 使用为IDistributedCache 注册的服务 The default implementation is a local in-memory cache : 默认实现是本地内存缓存:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    // Adds a default in-memory implementation of IDistributedCache.
    services.AddDistributedMemoryCache();

    services.AddSession(options =>
    {
        // Set a short timeout for easy testing.
        options.IdleTimeout = TimeSpan.FromSeconds(10);
        options.Cookie.HttpOnly = true;
    });
}

There are two persistent storage providers, one for Redis and one for SQL Server . 有两个持久性存储提供程序, 一个用于Redis ,一个用于SQL Server To use them simply replace the call to AddDistributedMemoryCache() , eg : 要使用它们,只需替换对AddDistributedMemoryCache()的调用,例如:

services.AddDistributedSqlServerCache(options =>
{
    options.ConnectionString = @"Data Source=(localdb)\v11.0;Initial Catalog=DistCache;Integrated Security=True;";
    options.SchemaName = "dbo";
    options.TableName = "TestCache";
});

The linked doc page explains how to configure the providers. 链接的文档页面说明了如何配置提供程序。

All three providers can be found in the ASP.NET\\Caching repository. 这三个提供程序都可以在ASP.NET \\ Caching存储库中找到。 You can use them as a starting point to implement your own provider, eg to use MySQL, assuming there isn't one already available. 您可以将它们用作实现自己的提供程序的起点,例如使用MySQL,并假设尚无可用的提供程序。

You can also configure ASP.NET Core to use the in-memory cache for general caching and SQL Server or Redis for distributed caching and session storage, eg : 您还可以配置ASP.NET Core以将内存缓存用于常规缓存,将SQL Server或Redis用于分布式缓存和会话存储,例如:

services.AddMemoryCache();
services.AddDistributedRedisCache(options =>
{
    options.Configuration = "localhost";
    options.InstanceName = "SampleInstance";
});

The default session store is in-memory, mostly because every other type of store requires some type of configuration. 默认会话存储区是内存中的,主要是因为其他所有类型的存储区都需要某种类型的配置。 When you have anything that's stored in-memory (session, cache, etc.) it's inherently process-bound, so when the process terminates, everything stored in-memory goes along with it. 当您将任何内容存储在内存中(会话,缓存等)时,它本质上就是进程绑定的,因此,当进程终止时,所有存储在内存中的内容都会随之而来。 Rebuilding a running web app causes the process to be terminated and restarted, so there's your issue in a nutshell. 重建正在运行的Web应用程序会导致该过程终止并重新启动,因此简而言之就是您的问题。

If you want the session to persist, then you need to use a persistent store , ie SQL Server, Redis, etc. The ASP.NET Core Distributed Cache (which is what ASP.NET Core uses for sessions) documentation covers how to to set all that up. 如果要使会话持久化,则需要使用一个持久性存储 ,例如SQL Server,Redis等。ASP.NET Core分布式缓存 (这是ASP.NET Core用于会话的内容)文档介绍了如何设置所有这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM