简体   繁体   English

如何在 Asp.net 内核中使用 session state 配置

[英]How to use the session state config in the Asp.net core

I created a web application using ASP.Net MVC 5. Now I am going to migrate this project to ASP.Net Core and serve it.我使用 ASP.Net MVC 5 创建了一个 web 应用程序。现在我要将此项目迁移到 ASP.Net Core 并为其提供服务。

Currently, I am using the session server separately.目前,我正在单独使用 session 服务器。 And the Web.config setting is as shown below: Web.config设置如下图:

<sessionState mode="StateServer" stateConnectionString="tcpip=1.1.1:45000" timeout="20" />

How would I set it up with ASP.Net Core?我将如何使用 ASP.Net Core 进行设置?

As far as I know, if you want to use sql server as the session stat provider, you should set the SQL Server distributed cache firstly in asp.net core and then configure the session. As far as I know, if you want to use sql server as the session stat provider, you should set the SQL Server distributed cache firstly in asp.net core and then configure the session.

More details, you could refer to below steps:更多细节,您可以参考以下步骤:

1.Distributed SQL Server Cache 1.分布式SQL服务器缓存

Create a table in SQL Server by running the sql-cache create command.通过运行 sql-cache create 命令在 SQL Server 中创建表。 Provide the SQL Server instance (Data Source), database (Initial Catalog), schema (for example, dbo), and table name (for example, TestCache):提供 SQL 服务器实例(数据源)、数据库(初始目录)、架构(例如,dbo)和表名(例如,TestCache):

dotnet sql-cache create "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=DistCache;Integrated Security=True;" dbo TestCache

2.Add below codes into configure service: 2.将以下代码添加到配置服务中:

    public void ConfigureServices(IServiceCollection services)  
    {  
services.AddDistributedSqlServerCache(options =>
{
    options.ConnectionString = 
        _config["DistCache_ConnectionString"];
    options.SchemaName = "dbo";
    options.TableName = "TestCache";
}); 
          
        services.AddSession(options => {   
            options.CookieName = "Test.Session";  
            options.IdleTimeout = TimeSpan.FromMinutes(60);   
        });  
          
          
        services.AddMvc();  
    } 

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

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