All Projects → Azure-Samples → Active Directory B2c Javascript Msal Singlepageapp

Azure-Samples / Active Directory B2c Javascript Msal Singlepageapp

Licence: mit
A single page application (SPA) calling a Web API. Authentication is done with Azure AD B2C by leveraging MSAL.js

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Active Directory B2c Javascript Msal Singlepageapp

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 (-61.59%)
Mutual labels:  microsoft, identity, single-page-app
ajxnetcore
Its an innovative method to turn an ASP.NET Core Application Into Single Page Application, While enhancing the Software performance both on server and client side.
Stars: ✭ 31 (-81.1%)
Mutual labels:  microsoft, single-page-app
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 (-79.88%)
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 (-2.44%)
Mutual labels:  microsoft, identity
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 (+1.22%)
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 (-62.8%)
Mutual labels:  microsoft, identity
Microsoft Authentication Library For Dotnet
Microsoft Authentication Library (MSAL) for .NET
Stars: ✭ 746 (+354.88%)
Mutual labels:  microsoft, identity
Active Directory B2c Advanced Policies
Sample for use with Azure AD B2C with Custom Policies.
Stars: ✭ 169 (+3.05%)
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 (-76.22%)
Mutual labels:  microsoft, identity
Active Directory B2c Javascript Nodejs Webapi
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.
Stars: ✭ 85 (-48.17%)
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 (-44.51%)
Mutual labels:  microsoft, identity
Microsoft Authentication Library For Objc
Microsoft Authentication Library (MSAL) for iOS and macOS
Stars: ✭ 132 (-19.51%)
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 (-78.05%)
Mutual labels:  microsoft, identity
Microsoft Authentication Library For Android
Microsoft Authentication Library (MSAL) for Android
Stars: ✭ 103 (-37.2%)
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 (-17.07%)
Mutual labels:  microsoft, identity
Ao
Elegant Microsoft To-Do desktop app
Stars: ✭ 1,923 (+1072.56%)
Mutual labels:  microsoft
Azure Sdk For Python
This repository is for active development of the Azure SDK for Python. For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/python/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-python.
Stars: ✭ 2,321 (+1315.24%)
Mutual labels:  microsoft
Kunafa
Easy to use, high level framework in Kotlin for front-end web-development
Stars: ✭ 148 (-9.76%)
Mutual labels:  single-page-app
Magic Js
Magic browser/React Native JavaScript SDK is your entry-point to integrating passwordless authentication inside your application.
Stars: ✭ 143 (-12.8%)
Mutual labels:  identity
Coding Interview Chinese
Contains top companies interview experience from major websites in recent years
Stars: ✭ 161 (-1.83%)
Mutual labels:  microsoft

page_type: sample description: "A sample demonstrating how to use the Microsoft Authentication Library for JavaScript to get an access token and call an API secured by Azure AD B2C." languages:

  • javascript products:
  • azure-active-directory-b2c urlFragment: "active-directory-b2c-javascript-msal-singlepageapp"

Single-Page Application built on MSAL.js with Azure AD B2C

This simple sample demonstrates how to use the Microsoft Authentication Library for JavaScript (msal.js) to get an access token and call an API secured by Azure AD B2C.

⚠️ Silent renewing of access tokens is not supported by all social identity providers.

A quickstart guide covering this sample can be found here. A more detailed tutorial covering this sample can be found here.

ℹ️ See here a new B2C single-page application sample using MSAL.js 2.x authorization code flow with PKCE.

Contents

File/folder Description
JavaScriptSPA Contains sample source files.
authPopup.js Main authentication logic resides here (using Popup flow).
authRedirect.js Use this instead of authPopup.js for authentication with redirect flow.
authConfig.js Contains configuration parameters for the sample.
api.js Provides a helper function for calling the web API.
apiConfig.js Contains API endpoint and scope.
ui.js Contains UI logic.
policies.js Contains policies and authority strings.
index.html Contains the UI of the sample.
.gitignore Defines what to ignore at commit time.
CHANGELOG.md List of changes to the sample.
CODE_OF_CONDUCT.md Code of Conduct information.
CONTRIBUTING.md Guidelines for contributing to the sample.
LICENSE The license for the sample.
package.json Package manifest for npm.
README.md This README file.
SECURITY.md Security disclosures.
server.js Implements a simple Node server to serve index.html.

Prerequisites

  • Node must be installed to run this sample.
  • A modern web browser. This sample uses ES6 conventions and will not run on Internet Explorer.

How to run this sample

There are two ways to run this sample:

  1. Using the demo environment - The sample is already configured to use a demo environment and can be run simply by downloading this repository and running the app on your machine. See steps below for Running with demo environment.
  2. Using your own Azure AD B2C tenant - If you would like to use your own Azure AD B2C configuration, follow the steps listed below for using your own Azure AD B2C tenant.

Using the demo environment

This sample demonstrates how to sign in or sign up for an account at "Fabrikam B2C" - the demo environment for this sample. Once signed-in, clicking on the Call Web API button shows the display name you used when you created your account.

Step 1: Clone or download this repository

From your shell or command line:

git clone https://github.com/Azure-Samples/active-directory-b2c-javascript-msal-singlepageapp.git

Step 2: Run the application

Make sure you've installed Node.

From your shell or command line:

cd active-directory-b2c-javascript-msal-singlepageapp
npm install && npm update
npm start

The console window shows the port number for the web application

Listening on port 6420...

You can visit http://localhost:6420 and perform the following actions:

  1. Click the Login button to start the Azure AD B2C sign in, sign up or password reset user-flows.
  2. Once signed in, you can click on the Call Web API button to have your display name returned from the Web API call as a JSON object.
  3. Once signed in, you can click the Edit Profile button to edit your profile information.
  4. Click Logout to logout from the application.

Using your own Azure AD B2C Tenant

In the previous section, you learned how to run the sample application using the demo environment. In this section, you'll learn how to configure this single page application sample and the related Node.js Web API with Azure AD B2C sample to work with your own Azure AD B2C Tenant. This will be covered in two parts.

PART I: Configure the API

How to setup and register the Node.js Web API sample

Follow the instructions on the Node.js Web API with Azure AD B2C sample. Once you are done, you should have a Node.js web API running on the port 5000. While it runs, continue with Part II below.

PART II: Configure the Client

Step 1: Register your own Web Application with Azure AD B2C

Next, you need to register your single page application in your B2C tenant.

Provide the following values for the Single Page Application registration:

  • Provide a descriptive Name for the single page application, for example, My Test SPA. You will identify this application by its Name whenever working in the Azure portal.
  • Mark Yes for the Web App/Web API setting for your application.
  • Set the Reply URL for your app to http://localhost:6420. This sample provided in this repository is configured to run on port 6420.
  • Create the application.
  • Once the application is created, click on the API Permissions blade (in the left nav menu). Click Add a Permission and select the name of the Node.js Web API you registered previously (under My APIs tab). Select the scope(s) you defined previously, for example, demo.read and hit Save.

Step 2: Configure the sample code to use your Azure AD B2C tenant

Now in the sample code, you can replace the single page application's demo environment configuration with your own tenant.

  1. Open the authConfig.js file.
  2. Find the assignment for clientId and replace the value with the Application ID for the single page application you registered earlier, for example the Application ID found in My Test SPA application in the Azure portal.
  3. Open the policies.js file.
  4. Find the entries for names and authorities and replacing, as appropriate, with the names of the policies you created in Step 2, and fabrikamb2c.onmicrosoft.com by the name of your Azure AD B2C tenant, for example https://<your-tenant-name>.b2clogin.com/<your-tenant-name>.onmicrosoft.com/<your-sign-in-sign-up-policy>
  5. Open the apiConfig.js file.
  6. Find the assignment for the scopes b2cScopes replacing the URL by the scope URL you created for the Web API, e.g. b2cScopes: ["https://<your-tenant-name>.onmicrosoft.com/helloapi/demo.read"]
  7. Find the assignment for API URL webApi replacing the current URL by the URL where you deployed your Web API in Step 4, e.g. webApi: http://localhost:5000/hello

Your resulting code should look as follows:

const msalConfig = {
  auth: {
    clientId: "e760cab2-b9a1-4c0d-86fb-ff7084abd902",
    authority: b2cPolicies.authorities.signUpSignIn.authority,
    validateAuthority: false
  },
  cache: {
    cacheLocation: "localStorage",
    storeAuthStateInCookie: true
  }
};

const loginRequest = {
  scopes: ["openid", "profile"],
};

const tokenRequest = {
  scopes: apiConfig.b2cScopes // i.e. ["https://fabrikamb2c.onmicrosoft.com/helloapi/demo.read"]
};
const b2cPolicies = {
    names: {
        signUpSignIn: "b2c_1_susi",
        forgotPassword: "b2c_1_reset",
        editProfile: "b2c_1_edit_profile"
    },
    authorities: {
        signUpSignIn: {
            authority: "https://fabrikamb2c.b2clogin.com/fabrikamb2c.onmicrosoft.com/b2c_1_susi",
        },
        forgotPassword: {
            authority: "https://fabrikamb2c.b2clogin.com/fabrikamb2c.onmicrosoft.com/b2c_1_reset",
        },
        editProfile: {
            authority: "https://fabrikamb2c.b2clogin.com/fabrikamb2c.onmicrosoft.com/b2c_1_edit_profile"
        }
    },
}
const apiConfig = {
  b2cScopes: ["https://fabrikamb2c.onmicrosoft.com/helloapi/demo.read"],
  webApi: "https://fabrikamb2chello.azurewebsites.net/hello"
};

Step 3: Run the sample

  1. Install the node dependencies if this is your first time running the app (e.g. if you skipped running in the demo environment):

    cd active-directory-b2c-javascript-msal-singlepageapp
    npm install && npm update
    
  2. Run the Web application:

    npm start
    
  3. Go to http://localhost:6420.

  4. Click the login button at the top of the application screen. The sample works exactly in the same way regardless of the account type you choose, apart from some visual differences in the authentication and consent experience. Upon successful sign in, the application screen will show buttons that allow you to call an API and sign out.

  5. Click on the Call Web API and see the textual representation of the JSON object that is returned. Make sure your Node.js Web API sample is still running on port 5000.

  6. Sign out by clicking the Logout button.

💭 Consider taking a moment to share your experience with us.

Optional

More information

For more information on Azure B2C, see:

Community Help and Support

We use StackOverflow with the msal and azure-ad-b2c tags to provide support. We highly recommend you ask your questions on StackOverflow first and browse existing issues to see if someone has asked your question before. Make sure that your questions or comments are tagged with [msal.js].

If you find and bug or have a feature request, please raise the issue on GitHub Issues.

To provide a recommendation, visit our Feedback Forum.

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].