Skip to content

Commit ea547fa

Browse files
committed
sync API tutorial with API code sample
1 parent 2c463ac commit ea547fa

File tree

2 files changed

+37
-34
lines changed

2 files changed

+37
-34
lines changed

articles/active-directory-b2c/tutorial-single-page-app-webapi.md

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -56,47 +56,49 @@ In the prerequisite tutorial, you created a web application named *webapp1*. In
5656

5757
[!INCLUDE [active-directory-b2c-permissions-api](../../includes/active-directory-b2c-permissions-api.md)]
5858

59-
Your single-page web application is registered to call the protected web API. A user authenticates with Azure AD B2C to use the single-page application. The single-page app obtains an authorization grant from Azure AD B2C to access the protected web API.
59+
Your single-page web application has now been granted permissions to the protected web API for the scopes specified. A user authenticates with Azure AD B2C to use the single-page application. The single-page app uses the authorization grant flow to access the protected web API with an access token returned by Azure AD B2C.
6060

6161
## Configure the sample
6262

63-
Now that the web API is registered and you have scopes defined, you configure the web API code to use your Azure AD B2C tenant. In this tutorial, you configure a sample Node.js web API you download from GitHub.
63+
Now that the web API is registered and you've defined scopes, configure the web API code to work with your Azure AD B2C tenant. In this tutorial, you configure a sample Node.js web API you download from GitHub.
6464

65-
[Download a \*.zip archive](https://github.com/Azure-Samples/active-directory-b2c-javascript-nodejs-webapi/archive/master.zip) or clone the sample web API project from GitHub.
65+
[Download a \*.zip archive](https://github.com/Azure-Samples/active-directory-b2c-javascript-nodejs-webapi/archive/master.zip) or clone the sample web API project from GitHub. You can also browse directly to the [Azure-Samples/active-directory-b2c-javascript-nodejs-webapi](https://github.com/Azure-Samples/active-directory-b2c-javascript-nodejs-webapi) project on GitHub.
6666

6767
```console
6868
git clone https://github.com/Azure-Samples/active-directory-b2c-javascript-nodejs-webapi.git
6969
```
7070

7171
### Configure the web API
7272

73-
1. Open the **index.js** file in Visual Studio Code.
74-
1. Modify the file to reflect your tenant name, the application ID of the web API application, the name of your sign-up/sign-in policy, and the scopes you defined earlier. The block should look similar to the following example (with appropriate `tenantIdGuid` and `clientId` values):
73+
1. Open the *config.js* file in your code editor.
74+
1. Modify the variable values to reflect those of the application registration you created earlier. Also update the `policyName` with the user flow you created as part of the prerequisites. For example, *B2C_1_signupsignin1*.
7575

7676
```javascript
77-
var clientID = "<your-webapi-application-ID>";
78-
var b2cDomainHost = "fabrikamb2c.b2clogin.com";
79-
var tenantIdGuid = "<your-webapi-tenant-ID>";
77+
var clientID = "<your-webapi-application-ID>"; // Application (client) ID
78+
var b2cDomainHost = "<your-tenant-name>.b2clogin.com";
79+
var tenantId = "<your-tenant-ID>.onmicrosoft.com";
8080
var policyName = "B2C_1_signupsignin1";
8181
```
8282

8383
#### Enable CORS
8484

85-
To allow your single-page application to call the Node.js web API, you need to enable [CORS](https://expressjs.com/en/resources/middleware/cors.html) in the web API. In a real application you should be careful about which domain is making the request, but for this example we will allow requests coming from any domain. To do so, use the following middleware:
85+
To allow your single-page application to call the Node.js web API, you need to enable [CORS](https://expressjs.com/en/resources/middleware/cors.html) in the web API. In a production application you should be careful about which domain is making the request, but for this tutorial, allow requests from any domain.
8686

87-
```javascript
88-
app.use(function (req, res, next) {
89-
res.header("Access-Control-Allow-Origin", "*");
90-
res.header("Access-Control-Allow-Headers", "Authorization, Origin, X-Requested-With, Content-Type, Accept");
91-
next();
92-
});
93-
```
87+
To enable CORS, use the following middleware. In the Node.js web API code sample used in this tutorial, you can find this code in the *index.js* file.
88+
89+
```javascript
90+
app.use((req, res, next) => {
91+
res.header("Access-Control-Allow-Origin", "*");
92+
res.header("Access-Control-Allow-Headers", "Authorization, Origin, X-Requested-With, Content-Type, Accept");
93+
next();
94+
});
95+
```
9496

9597
### Configure the single-page application
9698

9799
The single-page application (SPA) from the [previous tutorial](tutorial-single-page-app.md) in the series uses Azure AD B2C for user sign-up and sign-in, and calls the Node.js web API protected by the *frabrikamb2c* demo tenant.
98100

99-
In this section, you update the single-page application to call the Node.js web API protected by *your* Azure AD B2C tenant and which you run on your local machine.
101+
In this section, you update the single-page application to call the Node.js web API protected by *your* Azure AD B2C tenant (and which you run on your local machine).
100102

101103
To change the settings in the SPA:
102104

@@ -105,11 +107,11 @@ To change the settings in the SPA:
105107
1. In the `apiConfig` definition, replace the `b2cScopes` value with the full URI for the scope (the **SCOPE** value you recorded earlier).
106108
1. Change the `webApi` value to the redirect URI you added when you registered the web API application in an earlier step.
107109

108-
The `apiConfig` definition should look similar to the following code block (with your tenant name in the place of `<your-tenant-name>`):
110+
The `apiConfig` definition should look similar to the following code block, but with your B2C tenant's name in the place of `<your-tenant-name>`:
109111
110112
```javascript
111113
const apiConfig = {
112-
b2cScopes: ["https://fabrikamb2c.onmicrosoft.com/helloapi/demo.read"],
114+
b2cScopes: ["https://<your-tenant-name>.onmicrosoft.com/api/demo.read"],
113115
webApi: "http://localhost:5000/"
114116
};
115117
@@ -141,13 +143,14 @@ Although both applications run locally in this tutorial, they use Azure AD B2C f
141143
```console
142144
Listening on port 5000...
143145
```
144-
```
145146
146-
### Run the single page app
147+
### Run the single-page app
147148
148149
1. Open a console window and change to the directory containing the JavaScript SPA sample. For example:
149150
150-
`cd active-directory-b2c-javascript-msal-singlepageapp`
151+
```console
152+
cd active-directory-b2c-javascript-msal-singlepageapp
153+
```
151154
152155
1. Run the following commands:
153156
@@ -164,22 +167,22 @@ Although both applications run locally in this tutorial, they use Azure AD B2C f
164167
165168
1. Navigate to `http://localhost:6420` in your browser to view the application.
166169
1. Sign in using the email address and password you used in the [previous tutorial](tutorial-single-page-app.md). Upon successful login, you should see the `User 'Your Username' logged-in` message.
167-
1. Select the **Call Web API** button. The SPA obtains an authorization grant from Azure AD B2C, then accesses the protected web API to display the contents of its index page:
170+
1. Select the **Call API** button. The SPA obtains an authorization grant from Azure AD B2C, then accesses the protected web API to display the name of the logged-in user:
168171
169172
```Output
170-
Web APi returned:
171-
"<html>\r\n<head>\r\n <title>Azure AD B2C API Sample</title>\r\n ...
173+
Web API returned:
174+
{"name":"Lisa Andrews"}
172175
```
173176
174177
## Next steps
175178
176-
In this tutorial, you learned how to:
179+
In this tutorial, you:
177180
178181
> [!div class="checklist"]
179-
> * Add a web API application
180-
> * Configure scopes for a web API
181-
> * Grant permissions to the web API
182-
> * Configure the sample to use the application
182+
> * Created a web API application registration in your Azure AD B2C tenant
183+
> * Configured scopes for the web API
184+
> * Granted permissions to the web API
185+
> * Modified a web API code sample to work with your tenant
183186
184187
Now that you've seen an SPA request a resource from a protected web API, gain a deeper understanding of how these application types interact with each other and with Azure AD B2C.
185188

includes/active-directory-b2c-permissions-api.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ author: msmimart
33
ms.service: active-directory-b2c
44
ms.subservice: B2C
55
ms.topic: include
6-
ms.date: 10/16/2019
6+
ms.date: 03/23/2019
77
ms.author: mimart
88
# Used by the web app/web API tutorials for granting a web application access to
99
# a registered web API application
@@ -24,8 +24,8 @@ ms.author: mimart
2424
1. Select the **My APIs** tab.
2525
1. Select the API to which the web application should be granted access. For example, *webapi1*.
2626
1. Under **Permission**, expand **demo**, and then select the scopes that you defined earlier. For example, *demo.read* and *demo.write*.
27-
1. Select **Add permissions**. As directed, wait a few minutes before proceeding to the next step.
27+
1. Select **Add permissions**.
2828
1. Select **Grant admin consent for (your tenant name)**.
29-
1. Select your currently signed-in administrator account, or sign in with an account in your Azure AD B2C tenant that's been assigned at least the *Cloud application administrator* role.
29+
1. If you're prompted to select an account, select your currently signed-in administrator account, or sign in with an account in your Azure AD B2C tenant that's been assigned at least the *Cloud application administrator* role.
3030
1. Select **Accept**.
31-
1. Select **Refresh**, and then verify that "Granted for ..." appears under **Status** for both scopes. It might take a few minutes for the permissions to propagate.
31+
1. Select **Refresh**, and then verify that "Granted for ..." appears under **Status** for both scopes.

0 commit comments

Comments
 (0)