Django Microsoft Authentication: Page not found (404)

36 Views Asked by At

I am developing a Django app and attempting to integrate Microsoft authentication using the django_microsoft_auth library by following the steps outlined in the documentation here. However, I'm encountering a "Page not found (404)" error when trying to access /microsoft/authenticate/.

Here's the error message:

Page not found (404)
Request Method: GET
Request URL: https://127.0.0.1:8000/microsoft/authenticate/?next=/validate/

I have added the necessary configurations in my settings.py:

INSTALLED_APPS = [
    # ... other apps ...
    'django.contrib.sites',
    'microsoft_auth',
    'sslserver',
]
SITE_ID = 2 # the id matches the record in the django_site table with the corporate email domain.

# ... other settings ...

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'],
        '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',
                'microsoft_auth.context_processors.microsoft',
            ],
        },
    },
]

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

LOGIN_URL = '/microsoft/authenticate/'  # Using Microsoft authentication
LOGIN_REDIRECT_URL = '/dashboard/'
LOGOUT_REDIRECT_URL = '/'

MICROSOFT_AUTH_CLIENT_ID = 'your_client_id'
MICROSOFT_AUTH_TENANT_ID = 'your_tenant_id'
MICROSOFT_AUTH_CLIENT_SECRET = 'your_client_secret'
MICROSOFT_AUTH_LOGIN_TYPE = 'ma'

And my urls.py looks like this:

from django.urls import path
from . import views
from django.contrib.auth import views as auth_views
from django.conf.urls import include

urlpatterns = [
    # ... other paths ...
    path('microsoft/', include('microsoft_auth.urls', namespace='microsoft')),
]

Additionally, I have registered my Django app in Microsoft, and in the authentication section, I added the following Web Redirect URI: https://localhost:8000/.

Despite following the documentation, the /microsoft/authenticate/ URL doesn't seem to be matching any patterns. What could be causing this 404 error?

Any help or insights would be greatly appreciated. Thank you!

0

There are 0 best solutions below