I am migrating to ASP.NET CORE 5.0 and have middleware set up however upon setting up this part of my project I have been hit with httpContext.Session.GetString
coming up as an error. This use to work but it appears they have removed both .GetString and .SetString.
Here is my code for middleware.
public class ConfigureSessionMiddleware
{
private readonly RequestDelegate _next;
public ConfigureSessionMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext httpContext, IUserSession userSession, ISessionServices sessionServices)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (userSession == null)
{
throw new ArgumentNullException(nameof(userSession));
}
if (sessionServices == null)
{
throw new ArgumentNullException(nameof(sessionServices));
}
if (httpContext.User.Identities.Any(id => id.IsAuthenticated))
{
if (httpContext.Session.GetString("connectionString") == null) // Session needs to be set..
{
userSession.UserId = httpContext.User.Claims.FirstOrDefault(x => x.Type == "userId")?.Value;
userSession.ConnectionString = sessionServices.ConnectionStringFromUserId(userSession.UserId);
httpContext.Session.SetString("userId", userSession.UserId);
httpContext.Session.SetString("connectionString", userSession.ConnectionString);
}
else // Session set so all we need to is to build userSession for data access..
{
userSession.UserId = httpContext.Session.GetString("userId");
userSession.ConnectionString = httpContext.Session.GetString("connectionString");
}
}
// Call the next delegate/middleware in the pipeline
await _next.Invoke(httpContext).ConfigureAwait(false);
}
}
Its erroring on the following piece of code:
if (httpContext.Session.GetString("connectionString") == null) // Session needs to be set..
The error I am getting is:
'ISession' does not contain a definition for 'SetString' and no accessible extension method 'SetString' accepting a first argument of type 'ISession' could be found (are you missing a using directive or an assembly reference?)
I note with intellisense that GetString
and SetString
show but have question marks behind them..
So my question is, If I cant use GetString
to access (or create) my var connectionstring in this example how do I check/access/create a variable eg "connectionstring" in httpContext.session now that these methods have been deprecated?