Skip to content

Commit f5e86ac

Browse files
authored
Merge pull request #107913 from derisen/patch-1
'updates due to sample update'
2 parents 96af962 + b377235 commit f5e86ac

File tree

1 file changed

+81
-61
lines changed

1 file changed

+81
-61
lines changed
Lines changed: 81 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Use MSAL with Azure Active Directory B2CLearn | Azure
33
titleSuffix: Microsoft identity platform
4-
description: Microsoft Authentication Library (MSAL) enables applications to interoperate with Azure AD B2C and acquire tokens to call secured Web APIs. These web APIs can be Microsoft Graph, other Microsoft APIs, web APIs from others, or your own web API.
4+
description: Microsoft Authentication Library for JavaScript (MSAL.js) enables applications to work with Azure AD B2C and acquire tokens to call secured Web APIs. These web APIs can be Microsoft Graph, other Microsoft APIs, web APIs from others, or your own web API.
55
services: active-directory
66
author: negoe
77
manager: CelesteDG
@@ -17,105 +17,125 @@ ms.custom: aaddev
1717
#Customer intent: As an application developer, I want to learn about Microsoft Authentication Library so that I can decide if this platform meets my application development needs and requirements.
1818
---
1919

20-
# Use Microsoft Authentication Library to interoperate with Azure Active Directory B2C
20+
# Use Microsoft Authentication Library for JavaScript to work with Azure Active Directory B2C
2121

22-
Microsoft Authentication Library (MSAL) enables application developers to authenticate users with social and local identities by using [Azure Active Directory B2C (Azure AD B2C)](https://docs.microsoft.com/azure/active-directory-b2c/). Azure AD B2C is an identity management service. By using it, you can customize and control how customers sign up, sign in, and manage their profiles when they use your applications.
22+
[Microsoft Authentication Library for JavaScript (MSAL.js)](https://github.com/AzureAD/microsoft-authentication-library-for-js) enables JavaScript developers to authenticate users with social and local identities using [Azure Active Directory B2C (Azure AD B2C)](https://docs.microsoft.com/azure/active-directory-b2c/). By using Azure AD B2C as an identity management service, you can customize and control how customers sign up, sign in, and manage their profiles when they use your applications.
2323

24-
Azure AD B2C also enables you to brand and customize the UI of your applications to provide a seamless experience to your customers.
24+
Azure AD B2C also enables you to brand and customize the UI of your applications during the authentication process in order to provide a seamless experience to your customers.
2525

26-
This tutorial demonstrates how to use MSAL to interoperate with Azure AD B2C.
26+
This article demonstrates how to use MSAL.js to work with Azure AD B2C and summarizes key points that you should be aware of. For a complete discussion and tutorial, please consult [Azure AD B2C Documentation](https://docs.microsoft.com/azure/active-directory-b2c/overview).
2727

2828
## Prerequisites
2929

30-
If you haven't already created your own [Azure AD B2C tenant](https://docs.microsoft.com/azure/active-directory-b2c/tutorial-create-tenant), create one now. You also can use an existing Azure AD B2C tenant.
30+
If you haven't already created your own [Azure AD B2C tenant](https://docs.microsoft.com/azure/active-directory-b2c/tutorial-create-tenant), start with creating one now (you also can use an existing Azure AD B2C tenant if you have one already).
3131

32-
## JavaScript
32+
This demonstration contains two parts:
3333

34-
The following steps demonstrate how a single-page application can use Azure AD B2C to sign up, sign in, and call a protected web API.
34+
- how to protect a web API.
35+
- how to register a single-page application to authenticate and call *that* web API.
36+
37+
## Node.js Web API
38+
39+
> [!NOTE]
40+
> At this moment, MSAL.js for Node is still in development (see the [roadmap](https://github.com/AzureAD/microsoft-authentication-library-for-js/wiki#roadmap)). In the meantime, we suggest using [passport-azure-ad](https://github.com/AzureAD/passport-azure-ad), an authentication library for Node.js developed and supported by Microsoft.
41+
42+
The following steps demonstrate how a **web API** can use Azure AD B2C to protect itself and expose selected scopes to a client application.
3543

3644
### Step 1: Register your application
3745

38-
To implement authentication, you first need to register your application. See [Register your application](https://github.com/Azure-Samples/active-directory-b2c-javascript-msal-singlepageapp#step-4-register-your-own-web-application-with-azure-ad-b2c) for detailed steps.
46+
To protect your web API with Azure AD B2C, you first need to register it. See [Register your application](https://docs.microsoft.com/azure/active-directory-b2c/add-web-application?tabs=applications) for detailed steps.
3947

4048
### Step 2: Download the sample application
4149

4250
Download the sample as a zip file, or clone it from GitHub:
4351

44-
```
45-
git clone https://github.com/Azure-Samples/active-directory-b2c-javascript-msal-singlepageapp.git
52+
```console
53+
git clone https://github.com/Azure-Samples/active-directory-b2c-javascript-nodejs-webapi.git
4654
```
4755

4856
### Step 3: Configure authentication
4957

50-
1. Open the **index.html** file in the sample.
58+
1. Open the `config.js` file in the sample.
5159

52-
1. Configure the sample with the client ID and key that you recorded earlier while registering your application. Change the following lines of code by replacing the values with the names of your directory and APIs:
60+
2. Configure the sample with the application credentials that you obtained earlier while registering your application. Change the following lines of code by replacing the values with the names of your clientID, host, tenantId and policy name.
5361

54-
```javascript
55-
// The current application coordinates were pre-registered in a B2C tenant.
62+
```JavaScript
63+
const clientID = "<Application ID for your Node.js Web API - found on Properties page in Azure portal e.g. 93733604-cc77-4a3c-a604-87084dd55348>";
64+
const b2cDomainHost = "<Domain of your B2C host eg. fabrikamb2c.b2clogin.com>";
65+
const tenantId = "<your-tenant-ID>.onmicrosoft.com"; // Alternatively, you can use your Directory (tenant) ID (GUID)
66+
const policyName = "<Name of your sign in / sign up policy, e.g. B2C_1_signupsignin1>";
67+
```
5668

57-
var appConfig = {
58-
b2cScopes: ["https://fabrikamb2c.onmicrosoft.com/helloapi/demo.read"],
59-
webApi: "https://fabrikamb2chello.azurewebsites.net/hello"
60-
};
69+
For more information, check out this [Node.js B2C web API sample](https://github.com/Azure-Samples/active-directory-b2c-javascript-nodejs-webapi).
6170

62-
const msalConfig = {
63-
auth: {
64-
clientId: "e760cab2-b9a1-4c0d-86fb-ff7084abd902" //This is your client/application ID
65-
authority: "https://fabrikamb2c.b2clogin.com/fabrikamb2c.onmicrosoft.com/b2c_1_susi", //This is your tenant info
66-
validateAuthority: false
67-
},
68-
cache: {
69-
cacheLocation: "localStorage",
70-
storeAuthStateInCookie: true
71-
}
72-
};
73-
// create UserAgentApplication instance
74-
const myMSALObj = new Msal.UserAgentApplication(msalConfig);
71+
---
7572

76-
```
73+
## JavaScript SPA
74+
75+
The following steps demonstrate how a **single-page application** can use Azure AD B2C to sign up, sign in, and call a protected web API.
76+
77+
### Step 1: Register your application
78+
79+
To implement authentication, you first need to register your application. See [Register your application](https://docs.microsoft.com/azure/active-directory-b2c/tutorial-register-applications) for detailed steps.
80+
81+
### Step 2: Download the sample application
7782

78-
The name of the [user flow](https://docs.microsoft.com/azure/active-directory-b2c/active-directory-b2c-reference-policies) in this tutorial is **B2C_1_signupsignin1**. If you're using a different user flow name, set the **authority** value to that name.
83+
Download the sample as a zip file, or clone it from GitHub:
7984

80-
### Step 4: Configure your application to use `b2clogin.com`
85+
```console
86+
git clone https://github.com/Azure-Samples/active-directory-b2c-javascript-msal-singlepageapp.git
87+
```
8188

82-
You can use `b2clogin.com` in place of `login.microsoftonline.com` as a redirect URL. You do so in your Azure AD B2C application when you set up an identity provider for sign-up and sign-in.
89+
### Step 3: Configure authentication
8390

84-
Use of `b2clogin.com` in the context of `https://your-tenant-name.b2clogin.com/your-tenant-guid` has the following effects:
91+
There are two points of interest in configuring your application:
8592

86-
- Microsoft services consume less space in the cookie header.
87-
- Your URLs no longer include a reference to Microsoft. For example, your Azure AD B2C application probably refers to `login.microsoftonline.com`.
93+
- Configure API endpoint and exposed scopes
94+
- Configure authentication parameters and token scopes
8895

89-
To use `b2clogin.com`, you need to update the configuration of your application.
96+
1. Open the `apiConfig.js` file in the sample.
9097

91-
- Set the **validateAuthority** property to `false`, so that redirects using `b2clogin.com` can occur.
98+
2. Configure the sample with the parameters that you obtained earlier while registering your web API. Change the following lines of code by replacing the values with the address of your web API and exposed scopes.
9299

93-
The following example shows how you might set the property:
100+
```javascript
101+
// The current application coordinates were pre-registered in a B2C tenant.
102+
const apiConfig = {
103+
b2cScopes: ["https://fabrikamb2c.onmicrosoft.com/helloapi/demo.read"], //API scopes you exposed during api registration
104+
webApi: "https://fabrikamb2chello.azurewebsites.net/hello"
105+
};
106+
```
94107

95-
```javascript
96-
// The current application coordinates were pre-registered in a B2C directory.
108+
3. Open the `authConfig.js` file in the sample.
97109

98-
const msalConfig = {
99-
auth:{
100-
clientId: "Enter_the_Application_Id_here",
101-
authority: "https://contoso.b2clogin.com/tfp/contoso.onmicrosoft.com/B2C_1_signupsignin1",
102-
b2cScopes: ["https://contoso.onmicrosoft.com/demoapi/demo.read"],
103-
webApi: 'https://contosohello.azurewebsites.net/hello',
104-
validateAuthority: false;
110+
4. Configure the sample with the parameters that you obtained earlier while registering your single-page application. Change the following lines of code by replacing the values with your ClientId, authority metadata and token request scopes.
105111

106-
};
107-
// create UserAgentApplication instance
108-
const myMSALObj = new UserAgentApplication(msalConfig);
109-
```
112+
```javascript
113+
// Config object to be passed to Msal on creation.
114+
const msalConfig = {
115+
auth: {
116+
clientId: "e760cab2-b9a1-4c0d-86fb-ff7084abd902",
117+
authority: "https://fabrikamb2c.b2clogin.com/fabrikamb2c.onmicrosoft.com/B2C_1_signupsignin1",
118+
validateAuthority: false
119+
},
120+
cache: {
121+
cacheLocation: "localStorage", // This configures where your cache will be stored
122+
storeAuthStateInCookie: false // Set this to "true" to save cache in cookies
123+
}
124+
};
110125

111-
> [!NOTE]
112-
> Your Azure AD B2C application probably refers to `login.microsoftonline.com` in several places, such as your user flow references and token endpoints. Make sure that your authorization endpoint, token endpoint, and issuer have been updated to use `your-tenant-name.b2clogin.com`.
126+
// Add here scopes for id token to be used at the MS Identity Platform endpoint
127+
const loginRequest = {
128+
scopes: ["openid", "profile"],
129+
};
130+
```
113131

114-
Follow [this MSAL JavaScript sample](https://github.com/Azure-Samples/active-directory-b2c-javascript-msal-singlepageapp#single-page-application-built-on-msaljs-with-azure-ad-b2c) on how to use the MSAL Preview for JavaScript (MSAL.js). The sample gets an access token and calls an API secured by Azure AD B2C.
132+
For more information, check out this [JavaScript B2C single-page application sample](https://github.com/Azure-Samples/active-directory-b2c-javascript-msal-singlepageapp).
133+
134+
---
115135

116136
## Next steps
117137

118138
Learn more about:
119-
120-
- [Custom policies](https://docs.microsoft.com/azure/active-directory-b2c/active-directory-b2c-overview-custom)
121-
- [User interface customization](https://docs.microsoft.com/azure/active-directory-b2c/customize-ui-overview)
139+
- [User flows](https://docs.microsoft.com/azure/active-directory-b2c/tutorial-create-user-flows)
140+
- [Custom policies](https://docs.microsoft.com/azure/active-directory-b2c/custom-policy-get-started)
141+
- [UX customization](https://docs.microsoft.com/azure/active-directory-b2c/custom-policy-configure-user-input)

0 commit comments

Comments
 (0)