All Projects → Azure-Samples → Active Directory B2c Javascript Nodejs Webapi

Azure-Samples / Active Directory B2c Javascript Nodejs Webapi

Licence: mit
A small Node.js Web API for Azure AD B2C that shows how to protect your web api and accept B2C access tokens using Passport.js.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Active Directory B2c Javascript Nodejs Webapi

Active Directory B2c Dotnet Webapp And Webapi
A combined sample for a .NET web application that calls a .NET Web API, both secured using Azure AD B2C
Stars: ✭ 166 (+95.29%)
Mutual labels:  webapi, microsoft, identity
Active Directory B2c Advanced Policies
Sample for use with Azure AD B2C with Custom Policies.
Stars: ✭ 169 (+98.82%)
Mutual labels:  microsoft, identity
Active Directory B2c Javascript Msal Singlepageapp
A single page application (SPA) calling a Web API. Authentication is done with Azure AD B2C by leveraging MSAL.js
Stars: ✭ 164 (+92.94%)
Mutual labels:  microsoft, identity
ms-identity-mobile-apple-swift-objc
An iOS sample in Swift that authenticates Microsoft Account and Azure AD users and calls the Graph API using OAuth 2.0
Stars: ✭ 61 (-28.24%)
Mutual labels:  microsoft, identity
Microsoft Authentication Library For Objc
Microsoft Authentication Library (MSAL) for iOS and macOS
Stars: ✭ 132 (+55.29%)
Mutual labels:  microsoft, identity
Active Directory B2c Custom Policy Starterpack
Azure AD B2C now allows uploading of a Custom Policy which allows full control and customization of the Identity Experience Framework
Stars: ✭ 136 (+60%)
Mutual labels:  microsoft, identity
Aspnetcore Ddd
Full ASP.NET Core 3.1 LTS application with DDD, CQRS and Event Sourcing
Stars: ✭ 88 (+3.53%)
Mutual labels:  webapi, identity
Aspnetcore Webapi Course
Professional REST API design with ASP.NET Core 3.1 WebAPI
Stars: ✭ 323 (+280%)
Mutual labels:  webapi, identity
Aspnetcoreapistarter
An ASP.NET Core (v2.1) Web API project to quickly bootstrap new projects. Includes Identity, JWT authentication w/ refresh tokens.
Stars: ✭ 304 (+257.65%)
Mutual labels:  webapi, identity
active-directory-b2c-javascript-hellojs-singlepageapp
A single page app, implemented with an ASP.NET Web API backend, that signs up & signs in users using Azure AD B2C and calls the web API using OAuth 2.0 access tokens.
Stars: ✭ 63 (-25.88%)
Mutual labels:  microsoft, identity
Microsoft Authentication Library For Android
Microsoft Authentication Library (MSAL) for Android
Stars: ✭ 103 (+21.18%)
Mutual labels:  microsoft, identity
Epilink
Authenticate people on your Discord servers and give them roles automatically. All-in-one server back-end, Discord bot and front-end. Works with Google, Microsoft and any OpenID Connect provider.
Stars: ✭ 36 (-57.65%)
Mutual labels:  microsoft, identity
Active Directory B2c Xamarin Native
This is a simple Xamarin Forms app showcasing how to use MSAL to authenticate users via Azure Active Directory B2C, and access a Web API with the resulting tokens.
Stars: ✭ 91 (+7.06%)
Mutual labels:  microsoft, identity
Active Directory B2c Dotnetcore Webapp
An ASP.NET Core web application that can sign in a user using Azure AD B2C, get an access token using MSAL.NET and call an API.
Stars: ✭ 160 (+88.24%)
Mutual labels:  microsoft, identity
active-directory-android
An android app that uses Azure AD and the ADAL library for authenticating the user and calling a web API using OAuth 2.0 access tokens.
Stars: ✭ 33 (-61.18%)
Mutual labels:  microsoft, identity
Microsoft Authentication Library For Dotnet
Microsoft Authentication Library (MSAL) for .NET
Stars: ✭ 746 (+777.65%)
Mutual labels:  microsoft, identity
Active Directory B2c Dotnet Desktop
Sample showing how a Windows Desktop .NET (WPF) application can sign in a user using Azure AD B2C, get an access token using MSAL.NET and call an API.
Stars: ✭ 39 (-54.12%)
Mutual labels:  microsoft, identity
Aspnetcore.identity.documentdb
A Cosmos DB / DocumentDB Storage Provider for ASP.NET Core Identity
Stars: ✭ 64 (-24.71%)
Mutual labels:  identity
Chakracore
ChakraCore is an open source Javascript engine with a C API.
Stars: ✭ 8,600 (+10017.65%)
Mutual labels:  microsoft
Aspnetcoreid4external
external OpenID Connect Login to IdentityServer4 with AAD
Stars: ✭ 63 (-25.88%)
Mutual labels:  identity

page_type: sample languages:

  • javascript products:
  • nodejs
  • ms-graph
  • azure-active-directory
  • azure-active-directory-b2c urlFragment: active-directory-b2c-javascript-nodejs-webapi description: "This sample demonstrates a JavaScript SPA application calling a Node.js Web Api that is secured using Azure AD B2C"

A sample demonstrating how to protect a Node.js Web API with Azure AD B2C using the Passport.js library

  1. Overview
  2. Scenario
  3. Contents
  4. Prerequisites
  5. Setup
  6. Registration
  7. Running the sample
  8. Explore the sample
  9. About the code
  10. More information
  11. Community Help and Support
  12. Contributing

Overview

This sample demonstrates how to protect a Node.js Web API with Microsoft identity platform and Azure AD B2C using the passport-azure-ad library.

You will need a client application for calling the Web API. Choose:

Scenario

  1. The client application uses the Microsoft Authentication Library for JavaScript (MSAL.js) to sign-in a user and obtain a JWT Access Token from Azure AD B2C.
  2. The Access Token is used as a bearer token to authenticate the user when calling this web API.
  3. The web API responds with the name of the user obtained from the token claims.

Overview

Contents

File/folder Description
config.js Contains configuration parameters for the sample.
index.js Main application logic resides here.
process.json Contains configuration parameters for logging via Morgan.

Prerequisites

  • Node.js must be installed to run this sample.
  • A modern web browser. This sample uses ES6 conventions and will not run on Internet Explorer.
  • Visual Studio Code is recommended for running and editing this sample.
  • VS Code Azure Tools extension is recommended for interacting with Azure through VS Code Interface.
  • An Azure AD B2C tenant. For more information see: How to get an Azure AD B2C tenant
  • A user account in your Azure AD B2C. This sample will not work with a personal Microsoft account. Therefore, if you signed in to the Azure portal with a personal account and have never created a user account in your directory before, you need to do that now.

Setup

Step 1: Clone or download this repository

From your shell or command line:

    git clone https://github.com/Azure-Samples/active-directory-b2c-javascript-nodejs-webapi.git

or download and extract the repository .zip file.

⚠️ Given that the name of the sample is quite long, and so are the names of the referenced packages, you might want to clone it in a folder close to the root of your hard drive, to avoid maximum file path length limitations on Windows.

Step 2: Install project dependencies

    cd active-directory-b2c-javascript-nodejs-webapi
    npm install

Register the sample application(s) with your Azure Active Directory tenant

⚠️ This sample comes with a pre-registered application for testing purposes. If you would like to use your own Azure AD B2C tenant and application, follow the steps below to register and configure the application in the Azure Portal. Otherwise, continue with the steps for Running the sample.

Choose the Azure AD tenant where you want to create your applications

As a first step you'll need to:

  1. Sign in to the Azure portal.
  2. If your account is present in more than one Azure AD B2C tenant, select your profile at the top right corner in the menu on top of the page, and then switch directory to change your portal session to the desired Azure AD B2C tenant.

Create User Flows and Custom Policies

Please refer to: Tutorial: Create user flows in Azure Active Directory B2C

Add External Identity Providers

Please refer to: Tutorial: Add identity providers to your applications in Azure Active Directory B2C

Register the service app (active-directory-b2c-javascript-nodejs-webapi)

  1. Navigate to the Azure portal and select the Azure AD B2C service.
  2. Select the App Registrations blade on the left, then select New registration.
  3. In the Register an application page that appears, enter your application's registration information:
    • In the Name section, enter a meaningful application name that will be displayed to users of the app, for example active-directory-b2c-javascript-nodejs-webapi.
    • Under Supported account types, select Accounts in any organizational directory only.
  4. Select Register to create the application.
  5. In the app's registration screen, find and note the Application (client) ID. You use this value in your app's configuration file(s) later in your code.
  6. Select Save to save your changes.
  7. In the app's registration screen, select the Expose an API blade to the left to open the page where you can declare the parameters to expose this app as an API for which client applications can obtain access tokens for. The first thing that we need to do is to declare the unique resource URI that the clients will be using to obtain access tokens for this API. To declare an resource URI, follow the following steps:
    • Click Set next to the Application ID URI to generate a URI that is unique for this app.
    • For this sample, accept the proposed Application ID URI (api://{clientId}) by selecting Save.
  8. All APIs have to publish a minimum of one scope for the client's to obtain an access token successfully. To publish a scope, follow the following steps:
    • Select Add a scope button open the Add a scope screen and Enter the values as indicated below:
      • For Scope name, use demo.read.
      • For Admin consent display name type Access active-directory-b2c-javascript-nodejs-webapi
      • For Admin consent description type Allows the app to access active-directory-b2c-javascript-nodejs-webapi as the signed-in user.
      • Keep State as Enabled
      • Click on the Add scope button on the bottom to save this scope.
  9. On the right side menu, select the Manifest blade.
    • Set accessTokenAcceptedVersion property to 2.
    • Click on Save.

Configure the service app (active-directory-b2c-javascript-nodejs-webapi) to use your app registration

Open the project in your IDE (like Visual Studio or Visual Studio Code) to configure the code.

In the steps below, "ClientID" is the same as "Application ID" or "AppId".

  1. Open the config.json file.
  2. Find the key tenantName and replace the existing value with your Azure AD B2C tenant's name e.g. fabrikamb2c.
  3. Find the key clientID and replace the existing value with the application ID (clientId) of the active-directory-b2c-javascript-nodejs-webapi application copied from the Azure Portal.
  4. Find the key policyName and replace the existing value with name of the policy you've created, e.g. B2C_1_SUSI.

Running the sample

    cd active-directory-b2c-javascript-nodejs-webapi
    npm start

Explore the sample

Call this web API from your client application. Upon an authorized call, the web API will respond by:

      res.status(200).json({'name': req.authInfo['name']});

ℹ️ Did the sample not work for you as expected? Then please reach out to us using the GitHub Issues page.

We'd love to hear from you

Consider taking a moment to share your experience with us.

About the code

Token Validation

passport-azure-ad validates the token against the issuer, scope and audience claims (defined in BearerStrategy constructor) using the passport.authenticate() API:

    app.get('/hello', passport.authenticate('oauth-bearer', { session: false }),
        (req, res) => {
            console.log('Validated claims: ', req.authInfo);
    );

More information

For more information about how OAuth 2.0 protocols work in this scenario and other scenarios, see Authentication Scenarios for Azure AD.

Community Help and Support

Use 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 [azure-active-directory azure-ad-b2c ms-identity adal msal].

If you find a bug in the sample, please raise the issue on GitHub Issues.

To provide a recommendation, visit the following User Voice page.

Contributing

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 [email protected] with any additional questions or comments.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].