Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Email issue when using Azure account

See original GitHub issue
  • Django Microsoft Authentication Backend version: 2.2.3

  • Django version: 2.2.1

  • Python version: 3.6.8

  • Operating System: Ubuntu 18.04

  • Browser and version: Firefox 66.0.5 (64-bit)

  • Browser extensions/plugins you have installed: Form Filler (2.10.3) by Hussein


I am trying to implement single sign-on on my django application using microsoft auth and Azure AD. I was following the instructions on this page :

At step 9 of “Quickstart”, There is an error coming from the file /microsoft_auth/ in the _verify_microsoft_user function. The function uses the email from the Azure account to create a new user, but not all azure accounts have an email and it is not possible to add one.

Environment Setup Steps

I am using localhost in debug mode MICROSOFT_AUTH_LOGIN_TYPE = ‘ma’ There is no email associated to my azure account as I don’t have an Office 365 account. (screen provided)

Steps to Reproduce

Follow the instruction on this page : Try to log on with an Azure account with no email at step 9. Error should occur.

Environment:Request Method: POST
Request URL: http://localhost:8000/microsoft/auth-callback/Django Version: 2.2.1
Python Version: 3.6.8
Installed Applications:
Installed Middleware:
'django.middleware.clickjacking.XFrameOptionsMiddleware']Traceback:File "/home/nicolasabergel/Projects/env/lib/python3.6/site-packages/django/core/handlers/" in inner
 34.             response = get_response(request)File "/home/nicolasabergel/Projects/env/lib/python3.6/site-packages/django/core/handlers/" in _get_response
 115.                 response = self.process_exception_by_middleware(e, request)File "/home/nicolasabergel/Projects/env/lib/python3.6/site-packages/django/core/handlers/" in _get_response
 113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)File "/home/nicolasabergel/Projects/env/lib/python3.6/site-packages/django/views/generic/" in view
 71.             return self.dispatch(request, *args, **kwargs)File "/home/nicolasabergel/Projects/env/lib/python3.6/site-packages/django/utils/" in _wrapper
 45.         return bound_method(*args, **kwargs)File "/home/nicolasabergel/Projects/env/lib/python3.6/site-packages/django/views/decorators/" in wrapped_view
 54.         return view_func(*args, **kwargs)File "/home/nicolasabergel/Projects/env/lib/python3.6/site-packages/microsoft_auth/" in dispatch
 47.         return super().dispatch(request, *args, **kwargs)File "/home/nicolasabergel/Projects/env/lib/python3.6/site-packages/django/views/generic/" in dispatch
 97.         return handler(request, *args, **kwargs)File "/home/nicolasabergel/Projects/env/lib/python3.6/site-packages/microsoft_auth/" in post
 145.         context = self.get_context_data(**request.POST.dict())File "/home/nicolasabergel/Projects/env/lib/python3.6/site-packages/microsoft_auth/" in get_context_data
 68.         self._authenticate(kwargs.get("code"))File "/home/nicolasabergel/Projects/env/lib/python3.6/site-packages/microsoft_auth/" in _authenticate
 129.                 user = authenticate(self.request, code=code)File "/home/nicolasabergel/Projects/env/lib/python3.6/site-packages/django/contrib/auth/" in authenticate
 73.             user = backend.authenticate(request, **credentials)File "/home/nicolasabergel/Projects/env/lib/python3.6/site-packages/microsoft_auth/" in authenticate
 49.                 user = self._authenticate_user()File "/home/nicolasabergel/Projects/env/lib/python3.6/site-packages/microsoft_auth/" in _authenticate_user
 60.             return self._authenticate_microsoft_user()File "/home/nicolasabergel/Projects/env/lib/python3.6/site-packages/microsoft_auth/" in _authenticate_microsoft_user
 74.             return self._get_user_from_microsoft(claims)File "/home/nicolasabergel/Projects/env/lib/python3.6/site-packages/microsoft_auth/" in _get_user_from_microsoft
 132.             user = self._verify_microsoft_user(microsoft_user, data)File "/home/nicolasabergel/Projects/env/lib/python3.6/site-packages/microsoft_auth/" in _verify_microsoft_user
 162.                 user = User.objects.get(email=data["email"])
Exception Type: KeyError at /microsoft/auth-callback/
Exception Value: 'email''

Screenshot from 2019-06-03 10-34-52

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

p14zcommented, Oct 20, 2020

I fixed the issue on my site overriding the backend and using the “preferred_username” field as the email if there is none. Take into consideration that this is not an actual email address, so you can’t send emails to these users.

# my_users_app/
from microsoft_auth.backends import MicrosoftAuthenticationBackend as BaseMicrosoftAuthenticationBackend

class MicrosoftAuthenticationBackend(BaseMicrosoftAuthenticationBackend):
    def _verify_microsoft_user(self, microsoft_user, data):
        if 'email' not in data:
            data['email'] = data['preferred_username']
        return super()._verify_microsoft_user(microsoft_user, data)
    # 'microsoft_auth.backends.MicrosoftAuthenticationBackend', (REMOVE)

monkeyclasscommented, Oct 21, 2019

Had the same issue when testing my application with a “dummy” domain. Emails are indeed not created in Azure for the domains I created. I “fixed” it by inviting users from our real domain into my dummy domain for testing. Could be nice if we could rely on username in cases where emails do not exists.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot outbound SMTP connectivity problems in Azure
Learn the recommended method for sending email and how to troubleshoot problems with outbound SMTP connectivity in Azure.
Read more >
Email addresses aren't synced to Azure Active Directory
Describes an issue in which users are synced to Azure AD but one or more SMTP proxy addresses aren't synced. This issue occurs...
Read more >
Sign-in to Azure AD with email as an alternate login ID (Preview)
Learn how to enable users to sign in to Azure Active Directory with their email as an alternate login ID.
Read more >
Why am I not receiving notification emails? - Azure DevOps
The email server is down, unreachable, or rejected authenticated. · The email was delivered to an unchecked folder · The subscription is disabled ......
Read more >
Troubleshoot issues when you sign up for a new account in ...
You may experience an issue when you try to sign up for a new account in the Microsoft Azure portal. This short guide...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found