Asp.net Core 5.0 does not appear to have httpContext.Session.GetString as a method when it use to

Viewed 3791

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..

GetString has questionmarks after it

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?

1 Answers

Those extensions do not yet exist in net.core 5.0 Preview. If you go to the documentation it redirects to 3.1

The requested page is not available for ASP.NET Core 5.0 Preview. You have been redirected to the newest product version this page is available for.

Why not adding them yourself? They are just net core extensions@github

        public static void SetString(this ISession session, string key, string value)
        {
            session.Set(key, Encoding.UTF8.GetBytes(value));
        }

        public static string GetString(this ISession session, string key)
        {
            var data = session.Get(key);
            if (data == null)
            {
                return null;
            }
            return Encoding.UTF8.GetString(data);
        }
Related