Can't read session data from database

I have inherited a legacy Django application (currently on 2.2.24), that I am trying to modernize step by step. One requirement is to move from MySQL to an Azure SQL database.

Unfortunately, I ran into an issue, where I just can’t read back the session data from the database!

This is what my looks like. As you can see, django.contrib.sessions is set, as well as django.contrib.sessions.middleware.SessionMiddleware. Also, I have explicitly set SESSION_ENGINE = 'django.contrib.sessions.backends.db', which should be the default.

# ...
	# ...
SESSION_ENGINE = 'django.contrib.sessions.backends.db'
    'default': {
        'ENGINE': 'mssql', #
        'NAME': '<hiden>',
        'USER': '<hiden>',
        'PASSWORD': '<hiden>',
        'HOST': '<hiden>',
        'PORT': '1433,
        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
# ...

This is what the misbehaving looks like, which is part of an Azure AD login procedure (def auth(request) => sso/login & def complete(request) => sso/complete)

After execution leaves def auth(request), I can see a new entry in the table dbo.django_session. However, when execution enters def complete(request), the session dictionary is empty. Also request.session.session_key doesn’t hold anything. However, I see a session cookie is getting set!!!

def auth(request):
    nonce = str(uuid.uuid4())
    request.session['nonce'] = nonce # store to session db

    state = str(uuid.uuid4())
    request.session['state'] = state # store to session db

    backend = AzureActiveDirectoryBackend()
    redirect_uri = AZURE_AD_REDIRECT_URI or request.build_absolute_uri(reverse(complete))
    login_url = backend.login_url(

    if settings.DEBUG:
        print('login_url:', login_url)

    return HttpResponseRedirect(login_url)

def complete(request):
    backend = AzureActiveDirectoryBackend()
    method = 'GET' if backend.RESPONSE_MODE == 'fragment' else 'POST' # form_post / fragment
    # EMPTY !!!
    s_key = request.session.session_key
    keys = request.session.keys()
    items = request.session.items()

    original_state = request.session.get('state') # dictionary from SessionMiddleware

    resp_meta = getattr(request, method) # request.GET / request.POST
    state = resp_meta.get('state')

    if original_state != state:
        return HttpResponse('<h1>State is lost</h1>\n\nRefresh the page (F5).')

    if "error" in resp_meta:
        return HttpResponse('<h1>%s</h1> \n %s \n\n error_uri:<a>%s</a>' % (
            resp_meta['error'], resp_meta['error_description'], resp_meta['error_uri']))

	# ...

Other things I have tried without success:

  • Starting with a fresh database
  • Recreating the SECRET_KEY
  • Calling request.session.modified = True

What is going on here? Any help on this is greatly appreciated!

Can you check the database directly to verify that the table has been created? You can then determine whether the issue is that the data isn’t being written (empty table) or that something is going wrong with reading the data (table is populated, but nothing is coming back).

You should also be able to see the sessionid cookie from the browser’s developer tools in the request. You can then ensure that the sessionid is the same between the requests, and that both match the corresponding key in the table.

I assume the login url redirect sends the user to an external login page. Right before that the session is deleted with the flush call. The redirect should set the new session cookie, but there might be an issue with the SameSite setting (or lack thereof in Django 2.2) during the redirect or when the user is sent back to your app. That would explain why you see the cookie but it isn’t actually available.

Thanks everyone for replying. Setting CSRF_COOKIE_SAMESITE = None and SESSION_COOKIE_SAMESITE = None made it work.

As I was updating from 2.0.x to the latest 2.x version this broke my setup.