djangoazuredjango-authentication

AADSTS50011: The redirect URI 'http://127.0.0.1:8000/oauth2/callback' specified in the request does not match the redirect URIs


Message: AADSTS50011: The redirect URI 'http://127.0.0.1:8000/oauth2/callback' specified in the request does not match the redirect URIs configured for the application '456b3ef5-cdbe-4d58-aa7b-69f95fffac29'. Make sure the redirect URI sent in the request matches one added to your application in the Azure portal. Navigate to https://aka.ms/redirectUriMismatchError to learn more about how to fix this.

I don't understand why I get this error, although I set everything up strictly according to the documentation. User's image enter image description here

My current django setting.py file

Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites', 
 
    'django_auth_adfs',
]
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.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    
    # third party 
    'django_auth_adfs.middleware.LoginRequiredMiddleware',
]

AUTHENTICATION_BACKENDS = (
    'django_auth_adfs.backend.AdfsAuthCodeBackend',
    'django_auth_adfs.backend.AdfsAccessTokenBackend',
)


# Microsoft Azure AD configuration
AZURE_CLIENT_ID = os.environ.get('AZURE_CLIENT_ID')
AZURE_TENANT_ID = os.environ.get('AZURE_TENANT_ID')
AZURE_CLIENT_SECRET = os.environ.get('AZURE_CLIENT_SECRET')
AZURE_REDIRECT_URI = os.environ.get('AZURE_REDIRECT_URI')
AZURE_AUTHORITY = os.environ.get('AZURE_AUTHORITY')
AZURE_SCOPES = os.environ.get('AZURE_SCOPES').split()

AUTH_ADFS = {
    'AUDIENCE': [f'api://{AZURE_CLIENT_ID}', AZURE_CLIENT_ID],
    'CLIENT_ID': AZURE_CLIENT_ID,
    'CLIENT_SECRET': AZURE_CLIENT_SECRET,
    'CLAIM_MAPPING': {'first_name': 'given_name',
                      'last_name': 'family_name',
                      'email': 'upn'},
    'GROUPS_CLAIM': 'roles',
    'MIRROR_GROUPS': True,
    'USERNAME_CLAIM': 'upn',
    'TENANT_ID': AZURE_TENANT_ID,
    'RELYING_PARTY_ID': AZURE_CLIENT_ID,
    'LOGIN_EXEMPT_URLS': [
        '^api',  
    ],
}


LOGIN_URL = "django_auth_adfs:login"
LOGIN_REDIRECT_URL = "dashboard"
LOGOUT_REDIRECT_URL = '/'

Could you please tell me what could be the reason for this error? I have been trying to figure it out for several days now. I would also appreciate any additional information that could help me. Thank you in advance for your prompt assistance!

If you need any additional information, I am ready to provide it.

I don't understand why I get this error, although I set everything up strictly according to the documentation. https://django-auth-adfs.readthedocs.io/en/latest/install.html


Solution

  • AADSTS50011: The redirect URI 'http://127.0.0.1:8000/oauth2/callback' specified in the request does not match the redirect URIs configured for the application '456b3ef5-cdbe-4d58-aa7b-69f95fffac29'. Make sure the redirect URI sent in the request matches one added to your application in the Azure portal. Navigate to https://aka.ms/redirectUriMismatchError to learn more about how to fix this.

    The error you are facing is due to a URL mismatch. You are accessing your Django app using the following URL.

    http://127.0.0.1:8000/
    

    So, when I ran the app, I used the command below to change 127.0.0.1 to localhost.

    python manage.py runserver localhost:8000
    

    enter image description here

    Make sure the URL defined in the .env file of the Django app matches the URL specified in the Azure app registration.

    AZURE_REDIRECT_URI='http://localhost:8000/oauth2/callback'
    

    enter image description here