【问题标题】:How to use the session state config in the Asp.net core如何在 Asp.net 核心中使用会话状态配置
【发布时间】:2020-11-19 20:44:54
【问题描述】:
我使用 ASP.Net MVC 5 创建了一个 Web 应用程序。
现在我要把这个项目迁移到 ASP.Net Core 并提供服务。
目前,我正在单独使用会话服务器。而Web.config设置如下图:
<sessionState mode="StateServer" stateConnectionString="tcpip=1.1.1:45000" timeout="20" />
如何使用 ASP.Net Core 进行设置?
【问题讨论】:
标签:
c#
asp.net
asp.net-core
.net-core
【解决方案1】:
据我所知,如果你想使用sql server作为session stat provider,你应该先在asp.net core中设置SQL Server分布式缓存,然后再配置session。
更多细节,您可以参考以下步骤:
1.分布式SQL Server缓存
通过运行 sql-cache create 命令在 SQL Server 中创建表。提供 SQL Server 实例(数据源)、数据库(初始目录)、架构(例如 dbo)和表名(例如 TestCache):
dotnet sql-cache create "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=DistCache;Integrated Security=True;" dbo TestCache
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();
}