This is my session save and fetch method:
[HttpPost]
[Route("save-to-session")]
public IActionResult SaveToSession(string id)
{
HttpContext.Session.SetString("user", id);
return Content(id);
}
[HttpGet]
[Route("fetch-from-session")]
public IActionResult FetchFromSession()
{
string name = HttpContext.Session.GetString("user");
return Content(name);
}
I'm using Sql Server Cache:
services.AddDistributedSqlServerCache(options => {
options.ConnectionString = Configuration.GetConnectionString("TestMEApiContext");
options.SchemaName = "dbo";
options.TableName = "Session";
});
services.AddSession(options => {
options.Cookie.Name = ".Session";
options.IdleTimeout = TimeSpan.FromMinutes(3000);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
and the data is saved to the database but when i try to fetch it always returns null.
This is how i try to fetch it in my react code:
fetch(`https://localhost:44369/api/users/fetch-from-session`, { method: 'GET', credentials: 'include', mode: 'cors' }).then(function (body) {
return body.text();
}).then((response) => {
console.log("My response")
console.log(response) // <-THIS IS NULL
});
And this is how i try to save it but save works:
await this.getUserId().then(async function (userId) {
fetch(`https://localhost:44369/api/users/save-to-session?id=${userId}`, {
method: 'POST',
mode: 'cors',
credentials: 'include'
});
})