0

There are a few questions on this topic, but none of them solved my challenge. I cannot login to Django Admin with a correct username and password.

settings.py:

import os
from django.core.exceptions import ImproperlyConfigured

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

def get_env_variable(variable_name):
    try:
        return os.environ[variable_name]
    except KeyError:
        error_msg = 'Set the {} environment variable'.format(variable_name)
        raise ImproperlyConfigured(error_msg)


SECRET_KEY = get_env_variable('SECRET_KEY')

DEBUG = False

ALLOWED_HOSTS = ['XXXXXXXX']

INSTALLED_APPS = [
    'dal',
    'dal_select2',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'alerts.apps.AlertsConfig',
    'widget_tweaks',
]

SESSION_COOKIE_SECURE = False
SESSION_EXPIRE_AT_BROWSER_CLOSE = True

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.RemoteUserMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

AUTHENTICATION_BACKENDS = [
    'django.contrib.auth.backends.ModelBackend',
    'django.contrib.auth.backends.RemoteUserBackend',
]

ROOT_URLCONF = 'myProject.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'myProject.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'XXXXXXXXXXx',
        'HOST': get_env_variable('DATABASE_HOST'),
        'NAME': get_env_variable('DATABASE_NAME'),
        'USER': get_env_variable('DATABASE_USERNAME'),
        'PASSWORD': get_env_variable('DATABASE_PASSWORD'),
        'OPTIONS': {
            'driver': 'XXXXXXXXXXX'
        },
    },
}

# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = False

DATE_FORMAT = "m/d/Y"

USE_TZ = True

STATIC_ROOT = os.path.abspath(os.path.join(BASE_DIR, 'myProject', 'static'))
STATIC_URL = '/static/'
STATICFILES_DIRS = []

SECURE_CONTENT_TYPE_NOSNIFF = True
SECURE_BROWSER_XSS_FILTER = True
CSRF_COOKIE_SECURE = False
X_FRAME_OPTIONS = 'DENY'

I have tried turning on and off the various security settings. I have access to the DB, and the superuser exists and is staff. When I attempt to login, the DB table says the superuser logged in at that time, but the browser won't go beyond the splash page and says the username/password are incorrect. Other staff members (i.e. not superusers) can login. I think the problem might reside in the fact that I am using a RemoteUser backend in addition to the Model backend, but I'm not sure.

Why can't my superuser login?

OverflowingTheGlass
  • 2,324
  • 1
  • 27
  • 75
  • "Django’s user management, such as the views in contrib.admin and the createsuperuser management command, doesn’t integrate with remote users" at https://docs.djangoproject.com/en/1.11/howto/auth-remote-user/#authentication-using-remote-user – S_alj Dec 05 '17 at 17:25
  • no, that means you can't use the remote system (i.e. IIS) to login to the admin page. the next line states that superuser works with the DB table regardless of backend. including Model backend should take care of this. please see the answer to this question: https://stackoverflow.com/questions/18971928/cant-login-to-django-admin-after-creating-a-super-user-with-a-custom-user-model – OverflowingTheGlass Dec 05 '17 at 17:31

0 Answers0