如何在 ASP.NET MVC 中将会话数据保存到 SQL Server 数据库?

问题描述 投票:0回答:1

我想将会话数据保存到 SQL Server 数据库中,就像在节点 js 中一样,我们可以通过将数据库添加到存储键的会话对象来保存它。

就像这个例子,但它使用的是 mongo,我需要同样的 ASP.NET MVC 和 SQL Server:

app.use(session({
    ...
    store:  MongoStore.create({mongoUrl : process.env.CONNECTIONSTRING}),
    saveUninitialized: false,
    cookie: {
        secure: false,
        sameSite : true,
    },
}))

找了很多都没有答案

asp.net sql-server asp.net-mvc session-state
1个回答
0
投票
You can use SqlSessionStateStore

using System.Configuration;
using System.Web.SessionState;
using Microsoft.AspNet.SessionState;

protected void Application_Start()
{
    string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
    SqlSessionStateStore sessionStore = new SqlSessionStateStore(connectionString);
    SessionStateSection sessionState = (SessionStateSection) ConfigurationManager.GetSection("system.web/sessionState");
    sessionStore.Initialize(null, sessionState);
}

Replace the DefaultConnection with the name of your connection string in the web.config file.
© www.soinside.com 2019 - 2024. All rights reserved.