This sample shows how to build a multi-tenant ASP.NET Core web application that uses OpenID Connect to sign up and sign in users from any Azure Active Directory (AD) tenant, using the ASP.Net OpenID Connect middleware and the Active Directory Authentication Library (ADAL) for .NET. The sample also demonstrates how to leverage the authorization code received at sign in time to invoke the Graph API.
For more information about how the protocols work in this scenario and other scenarios, see the Authentication Scenarios for Azure AD document.
This sample has been updated to ASP.NET Core 1.0. Looking for previous versions of this code sample? Check out the tags on the releases GitHub page.
Getting started is simple! To run this sample you will need: - To install .NET Core for Windows by following the instructions at dot.net/core, which will include Visual Studio 2015 Update 3. - An Internet connection - An Azure Active Directory (Azure AD) tenant. For more information on how to get an Azure AD tenant, please see How to get an Azure AD tenant - A user account in your Azure AD tenant. This sample will not work with a Microsoft account, so if you signed in to the Azure portal with a Microsoft account and have never created a user account in your directory before, you need to do that now.
From your shell or command line:
git clone https://github.com/Azure-Samples/active-directory-dotnet-webapp-webapi-multitenant-openidconnect-aspnetcore.git
If you already have an Organizational user account in your Azure Active Directory tenant that you would like to use for consent and authentication, you can skip to the next step. This sample will not work with a Microsoft account, so if you signed in to the Azure portal with a Microsoft account and have never created a user account in your directory before, you need to do that now. You can find instructions to do that here.
If you want to test both the Administrator and User consent flows discussed below, you will want to create two Organizational accounts: one assigned to the "User" role and one assigned to the "Global Administrator" role.
https://localhost:44376/. Click on Create to create the application.
Logout Urlproperty to
https://localhost:44376/Account/EndSession. This is the default single sign out URL for this sample.
At this point we are ready to paste the configuration settings into the VS project that will tie it to its entry in your Azure AD tenant.
ClientIdproperty and replace the value with the Application ID for the TodoListWebApp from the Azure portal.
ClientSecretand replace the value with the key for the TodoListWebApp from the Azure portal.
RedirectUriproperty and replace the value with the new base URL of the sample.
This sample shows how to take advantage of the consent framework in Azure AD to enable an application to be multi-tenant aware, which allows authentication by user accounts from any Azure AD tenant. To see that part of the sample in action, you need to have access to user accounts from a tenant that is different from the one you used for registering the application. A great example of this type of scenario, is an application that needs to allow Office365 user accounts (which are homed in a separate Azure AD) to authenticate and consent access to their Office365 tenant. The simplest way of doing this is to create a new directory tenant in your Azure subscription (just navigate to the main Active Directory page in the portal and click Add) and add test users. This step is optional as you can also use accounts from the same directory, but if you do you will not see the consent prompts as the app is already approved.
The sample implements two distinct tasks: the onboarding of a new customer (aka: Sign up), and regular sign in & use of the application.
Once you signed up, you can either click on the Todo tab or the sign in link to gain access to the application. Note that if you are doing this in the same session in which you signed up, you will automatically sign in with the same account you used for signing up. If you are signing in during a new session, you will be presented with Azure AD's credentials prompt: sign in using an account compatible with the sign up option you chose earlier (the exact same account if you used user consent, any user form the same tenant if you used admin consent).
This sample uses a SQLite database located at
TodoListWebApp\App_Data\TodoListWebApp.db. The initial Entity Framework migration has been included in the
TodoListWebApp\Migrations folder. If you encounter database-related exceptions in the app, take the following steps:
TodoListWebApp.dbfile exists. An empty instance is included in the github repo, and the
.dbfile should be created during first app run if it's not already there.
dotnet ef database updatein the
TodoListWebAppdirectory to create the tables. If that doesn't work, you can also run
dotnet ef migrations scriptto generate the SQL for creating the tables. Execute that SQL against the
.dbfile using a SQLite db explorer (there are many out there), and you should be good to go.