csharp
powershell
javascript
html
sample
Use the Microsoft identity platform endpoint to access the data of Microsoft business customers in a long-running, non-interactive process.
azure
azure-active-directory
dotnet
build-multi-tenant-daemon-aad
Build a multi-tenant daemon with the Microsoft identity platform endpointFor a simpler console daemon application, check out the following sample: active-directory-dotnetcore-daemon-v2
We have renamed the default branch to main. To rename your local repo follow the directions here.
This sample application shows how to use the Microsoft identity platform endpoint to access the data of Microsoft business customers in a long-running, non-interactive process. It uses the OAuth2 client credentials grant to acquire an access token, which it then uses to call the Microsoft Graph and access organizational data.
The app is built as an ASP.NET MVC application, and uses the OWIN OpenID Connect middleware to sign in users. Its "daemon" component in this sample is just an API controller, which, when called, pulls in a list of users in customer's Azure AD tenant from Microsoft Graph. This SyncController.cs
is triggered by an AJAX call in the web application, and uses the Microsoft Authentication Library (MSAL) for .NET to acquire an access token for Microsoft Graph.
Because the app is a multi-tenant app intended for use by any Microsoft business customer, it must provide a way for customers to "sign up" or "connect" the application to their company data. During the connect flow, a company administrator first grants application permissions directly to the app so that it can access company data in a non-interactive fashion, without the presence of a signed-in user. The majority of the logic in this sample shows how to achieve this connect flow using the identity platform admin consent endpoint.
For more information on the concepts used in this sample, be sure to read the identity platform endpoint client credentials protocol documentation.
Looking for previous versions of this code sample? Check out the tags on the releases GitHub page.
To run this sample, you'll need:
From your shell or command line:
git clone https://github.com/Azure-Samples/active-directory-dotnet-daemon-v2.git
or download and exact the repository .zip file.
Step 2: Register the sample application with your Azure Active Directory tenantGiven that the name of the sample is pretty long, and so are the name of the referenced NuGet packages, you might want to clone it in a folder close to the root of your hard drive, to avoid file size limitations on Windows.
There is one project in this sample. To register it, you can:
If you want to use this automation:
On Windows, run PowerShell and navigate to the root of the cloned directory
In PowerShell run:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
Run the script to create your Azure AD application and configure the code of the sample application accordingly.
In PowerShell run:
.\AppCreationScripts\Configure.ps1
Other ways of running the scripts are described in App Creation Scripts
Open the Visual Studio solution and click start to run the code.
If you don't want to use this automation, follow the steps below.
Choose the Azure AD tenant where you want to create your applicationsAs a first step you'll need to:
Navigate to the Microsoft identity platform for developers App registrations page.
Select New registration.
When the Register an application page appears, enter your application's registration information:
dotnet-web-daemon-v2
.https://localhost:44316/
https://localhost:44316/Account/GrantPermissions
Note that if there are more than one redirect URIs, you'd need to add them from the Authentication tab later after the app has been created successfully.
Select Register to create the application.
On the app Overview page, find the Application (client) ID value and record it for later. You'll need it to configure the Visual Studio configuration file for this project.
In the list of pages for the app, select Authentication.
https://localhost:44316/Account/EndSession
Select Save.
From the Certificates & secrets page, in the Client secrets section, choose New client secret:
app secret
),In the list of pages for the app, select API permissions
In the steps below, "ClientID" is the same as "Application ID" or "AppId".
Open the solution in Visual Studio to configure the projects
Configure the client projectNote: if you used the setup scripts, the changes below will have been applied for you
UserSync\Web.Config
fileida:ClientId
and replace the existing value with the application ID (clientId) of the dotnet-web-daemon-v2
application copied from the Azure portal.ida:ClientSecret
and replace the existing value with the key you saved during the creation of the dotnet-web-daemon-v2
app, in the Azure portal.Clean the solution, rebuild the solution, and run UserSync application, and begin by signing in as an administrator in your Azure AD tenant. If you don't have an Azure AD tenant for testing, you can follow these instructions to get one.
When you sign in, the app will first ask you for permission to sign you in & read your user profile. This consent allows the application to ensure that you are a business user.
The application will then try to sync a list of users from your Azure AD tenant, via the Microsoft Graph. If it is unable to do so, it will ask you (the tenant administrator) to connect your tenant to the application.
The application will then ask for permission to read the list of users in your tenant.
You will be signed out from the app after granting permission. This is done to ensure that any existing access tokens for Graph is removed from the token cache. Once you sign in again, the fresh token obtained will have the necessary permissions to make calls to MS Graph. When you grant the permission, the application will then be able to query for users at any point. You can verify this by clicking the Sync Users button on the users page, refreshing the list of users. Try adding or removing a user and re-syncing the list (but note that it only syncs the first page of users!).
The relevant code for this sample is in the following files:
App_Start\Startup.Auth.cs
, Controllers\AccountController.cs
. In particular, the actions on the controller have an Authorize attribute, which forces the user to sign in. The application uses the authorization code flow to sign in the user.Controllers\SyncController.cs
Controllers\UserController.cs
Controllers\AccountController.cs
Visual C#
ASP.NET Web Application (.NET Framework)
project. In the next screen, choose the MVC
project template. Also add folder and core references for Web API
as you would be adding a Web API controller later. Leave the project's chosen authentication mode as the default, that is, No Authentication
".True
. Note the SSL URL. You will need it when configuring this application's registration in the Azure portal.Microsoft.Owin.Security.ActiveDirectory
, Microsoft.Owin.Security.Cookies
and Microsoft.Owin.Host.SystemWeb
, Microsoft.IdentityModel.Protocol.Extensions
, Microsoft.Owin.Security.OpenIdConnect
and Microsoft.Identity.Client
.App_Start
folder, create a class Startup.Auth.cs
.You will need to remove .App_Start
from the namespace name. Replace the code for the Startup
class with the code from the same file of the sample app. Be sure to take the whole class definition! The definition changes from public class Startup
to public partial class Startup
Startup.Auth.cs
resolve missing references by adding using
statements as suggested by Visual Studio IntelliSense.Startup.cs
.Startup.cs
, replace the code for the Startup
class with the code from the same file of the sample app. Again, note the definition changes from public class Startup
to public partial class Startup
.MsGraphUser.cs
. Replace the implementation with the contents of the file of the same name from the sample.AccountController
. Replace the implementation with the contents of the file of the same name from the sample.UserController
. Replace the implementation with the contents of the file of the same name from the sample.SyncController
. Replace the implementation with the contents of the file of the same name from the sample.Views\Account
folder, add three Empty (without model) Views named GrantPermissions
, Index
and UserMismatch
and one named Index
in the Views\User
folder. Replace the implementation with the contents of the file of the same name from the sample.Shared\_Layout.cshtml
and Home\Index.cshtml
to correctly link the various views together.This project has one WebApp / Web API projects. To deploy them to Azure Web Sites, you'll need, for each one, to:
dotnet-web-daemon-v2
to an Azure Web Site
Create a resource
in the top left-hand corner, select Web --> Web App, and give your web site a name, for example, dotnet-web-daemon-v2-contoso.azurewebsites.net
.Subscription
, Resource Group
, App service plan and Location
. OS
will be Windows and Publish
will be Code.Create
and wait for the App Service to be created.Deployment succeeded
notification, then click on Go to resource
to navigate to the newly created App service.Connection tab
, update the Destination URL so that it is a https
in the home page URL, for example https://dotnet-web-daemon-v2-contoso.azurewebsites.net. Click Next.Enable Organizational Authentication
is NOT selected. Click Save. Click on Publish on the main screen.dotnet-web-daemon-v2
dotnet-web-daemon-v2
application.If you get an HTTP 403, even after admin consent, you might have chosen Delegated permissions for User.Read.All instead of Application permissions.
Community Help and SupportUse Stack Overflow to get support from the community. Ask your questions on Stack Overflow first and browse existing issues to see if someone has asked your question before. Make sure that your questions or comments are tagged with [adal
msal
dotnet
].
If you find and bug in the sample, please raise the issue on GitHub Issues.
If you find a bug in msal.Net, please raise the issue on MSAL.NET GitHub Issues.
To provide a recommendation, visit the following User Voice page.
If you'd like to contribute to this sample, see CONTRIBUTING.MD.
This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
For more information, see MSAL.NET's conceptual documentation:
For more information about the underlying protocol:
For a simpler multi-tenant console daemon application, see active-directory-dotnetcore-daemon-v2
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4