Home

Awesome

Azure Active Directory OIDC Web Sample

LibraryDocsSupportProtocol

This sample demonstrates how to set up OpenId Connect authentication in a web application built using Node.js with Express. The sample is designed to run on any platform.

Prerequisites

To run this sample you will need the following:

Register the sample in your Azure AD tenant

  1. Sign in to the Azure portal.

  2. On the top bar, click on your account, and then on Switch Directory. Once the Directory + subscription pane opens, choose the Active Directory tenant where you wish to register your application.

  3. Click on All services in the left-hand nav, and choose Azure Active Directory.

  4. Click on App registrations and choose New application registration.

  5. Enter a friendly name for the application, for example 'Webapp-Openidconnect' and select 'Web app / API' as the Application Type.

  6. For the sign-on URL, enter the base URL for this sample which is http://localhost:3000/.

  7. Click Create to create the application.

  8. In the succeeding page, Find the Application ID value and record it for later. You'll need it to configure the client ID in the application.

  9. Under Settings, choose Properties and update the App ID URI which is a unique identifier for your app. It is of the format 'https://<your_tenant_name>/<app_name>' replacing <your_tenant_name> with the name of your Azure AD tenant. For example: https://contoso.onmicrosoft.com/Webapp-Openidconnect

  10. Under Settings, click on Reply URLs and set it to http://localhost:3000/auth/openid/return which this sample uses by default.

  11. From the Settings menu, choose Keys and add a new entry in the Password section:

    • Type a key description (for instance 'app secret'),
    • Select a key duration of either In 1 year, In 2 years, or Never Expires.
    • When you save this page, the key value will be displayed. Copy, and save the value in a safe location.
    • You'll need this key later to configure the client secret in the app. This key value will not be displayed again, nor retrievable by any other means, so record it as soon as it is visible from the Azure portal.

Download the sample application and modules

Next, clone the sample repo and install the NPM modules.

From your shell or command line run:

or

From the project root directory, run the command:

Configure the application

Provide the parameters in exports.creds in config.js as instructed.

Optional configuration for production apps:

Build and run the application

Is the server output hard to understand?: We use bunyan for logging in this sample. The console won't make much sense to you unless you also install bunyan and run the server like above but pipe it through the bunyan binary:

$ npm install -g bunyan

$ node app.js | bunyan

You're done!

You will have a server successfully running on http://localhost:3000.

Community Help and Support

We use Stack Overflow with the community to provide support. We highly recommend you 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 [azure-active-directory].

If you find a bug or issue with this sample, please raise the issue on GitHub Issues.

For issues with the passport-azure-ad library, please raise the issue on the library GitHub repo.

Contributing

If you'd like to contribute to this sample, please follow the GitHub Fork and Pull request model.

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.

Security Library

This library controls how users sign-in and access services. We recommend you always take the latest version of our library in your app when possible.

Security Reporting

If you find a security issue with our libraries or services please report it to secure@microsoft.com with as much detail as possible. Your submission may be eligible for a bounty through the Microsoft Bounty program. Please do not post security issues to GitHub Issues or any other public site. We will contact you shortly upon receiving the information. We encourage you to get notifications of when security incidents occur by visiting this page and subscribing to Security Advisory Alerts.

Acknowledgements

We would like to acknowledge the folks who own/contribute to the following projects for their support of Azure Active Directory and their libraries that were used to build this sample. In places where we forked these libraries to add additional functionality, we ensured that the chain of forking remains intact so you can navigate back to the original package. Working with such great partners in the open source community clearly illustrates what open collaboration can accomplish. Thank you!

About The Code

Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License (the "License");