Search code examples
c#asp.net-mvcazureasp.net-mvc-5azure-active-directory

Check if username exist in Microsoft Azure Active Directory


I am using Microsoft Azure Active Directory login for my MVC 5 application. Can somebody give me an idea how I can check if a username already exists in Microsoft Azure Active Directory?

What is the general approach to do this?


Solution

  • You can use the Graph API and query for the user you want. For information about Graph API read: http://msdn.microsoft.com/en-us/library/azure/hh974476.aspx
    The common queries page (http://msdn.microsoft.com/en-us/library/azure/jj126255.aspx) has a query for a user given the userPrincipalName. You should use this query and check if it returns a 404 (not found).

    Basically the query you are looking for is: "https://graph.windows.net/contoso.com/users/admin@contoso.com?api-version=2013-04-05" where you need to replace contoso.com with your domain and admin@contoso.com with the upn you want to search for.

    You should also look at the Azure AD samples on GitHub. In this case, you're probably interested in how to use the Graph API: https://github.com/AzureADSamples/WebApp-GraphAPI-DotNet

    This sample MVC web application demonstrates how to query Azure Active Directory using the Graph API. To facilitate application development, it includes showing how to use included Graph Library, and uses OpenID Connect to authorize users to conenct to their directory data.