Skip to content

Commit 335c912

Browse files
author
Tiago Brenck
committed
- Updated README and Webconfig, mentioning the known issue about TenantId not included by default on B2C tokens
1 parent 9bcea81 commit 335c912

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

README.md

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ author: dstrockis
66

77
# Azure AD B2C: Call an ASP.NET Web API from an ASP.NET Web App
88

9-
This sample contains a solution file that contains two projects: `TaskWebApp` and `TaskService`.
9+
This sample contains a solution file that contains two projects: `TaskWebApp` and `TaskService`.
1010

1111
- `TaskWebApp` is a "To-do" ASP.NET MVC web application where the users enters or updates their to-do items. These CRUD operations are performed by a backend web API. The web app displays the information returned from the ASP.NET Web API.
12-
- `TaskService` is the backend ASP.NET API that manages and stores each user's to-do list.
12+
- `TaskService` is the backend ASP.NET API that manages and stores each user's to-do list.
1313

14-
The sample covers the following:
14+
The sample covers the following:
1515

16-
* Calling an OpenID Connect identity provider (Azure AD B2C)
17-
* Acquiring a token from Azure AD B2C using MSAL
16+
- Calling an OpenID Connect identity provider (Azure AD B2C)
17+
- Acquiring a token from Azure AD B2C using MSAL
1818

1919
## How To Run This Sample
2020

@@ -25,25 +25,23 @@ There are two ways to run this sample:
2525

2626
## Using the demo environment
2727

28-
This sample demonstrates how you can sign in or sign up for an account at "Wingtip Toys" (the demo environment for this sample) using a ASP.NET MVC Web Application.
28+
This sample demonstrates how you can sign in or sign up for an account at "Wingtip Toys" (the demo environment for this sample) using a ASP.NET MVC Web Application.
2929

30-
Once singed in, you can create and edit your todo items.
30+
Once singed in, you can create and edit your todo items.
3131

3232
### Step 1: Clone or download this repository
3333

3434
From your shell or command line:
3535

36-
```
37-
git clone https://github.com/Azure-Samples/active-directory-b2c-dotnet-webapp-and-webapi.git
38-
```
36+
`git clone https://github.com/Azure-Samples/active-directory-b2c-dotnet-webapp-and-webapi.git`
3937

4038
### Step 2: Run the project
4139

4240
Open the `B2C-WebAPI-DotNet.sln` in Visual Studio.
4341

44-
You will need to run both the `TaskWebApp` and `TaskService` projects at the same time.
42+
You will need to run both the `TaskWebApp` and `TaskService` projects at the same time.
4543

46-
1. In Solution Explorer, right-click on the solution and open the **Common Properties - Startup Project** window.
44+
1. In Solution Explorer, right-click on the solution and open the **Common Properties - Startup Project** window.
4745
2. Select **Multiple startup projects**.
4846
3. Change the **Action** for both projects from **None** to **Start** as shown in the image below.
4947

@@ -54,7 +52,7 @@ The sample demonstrates the following functionality once signed-in:
5452
1. Click your **``<Display Name>``** in upper right corner to edit your profile or reset your password.
5553
2. Click **Claims** to view the claims associated with the signed-in user's id token.
5654
3. Click **Todo** to create and view your todo items. These CRUD operations are performed by calling the corresponding Web API running in the solution.
57-
4. Sign out and sign in as a different user. Create tasks for this second user. Notice how the tasks are stored per-user on the API, because the API extracts the user's identity from the access token it receives.
55+
4. Sign out and sign in as a different user. Create tasks for this second user. Notice how the tasks are stored per-user on the API, because the API extracts the user's identity from the access token it receives.
5856

5957
## Using your own Azure AD B2C Tenant
6058

@@ -74,17 +72,17 @@ Make sure that all the three policies return **User's Object ID** and **Display
7472

7573
### Step 3: Register your ASP.NET Web API with Azure AD B2C
7674

77-
Follow the instructions at [register a Web API with Azure AD B2C](https://docs.microsoft.com/azure/active-directory-b2c/active-directory-b2c-app-registration#register-a-web-api) to register the ASP.NET Web API sample with your tenant. Registering your Web API allows you to define the scopes that your ASP.NET Web Application will request access tokens for.
75+
Follow the instructions at [register a Web API with Azure AD B2C](https://docs.microsoft.com/azure/active-directory-b2c/active-directory-b2c-app-registration#register-a-web-api) to register the ASP.NET Web API sample with your tenant. Registering your Web API allows you to define the scopes that your ASP.NET Web Application will request access tokens for.
7876

79-
Provide the following values for the ASP.NET Web API registration:
77+
Provide the following values for the ASP.NET Web API registration:
8078

8179
- Provide a descriptive Name for the ASP.NET Web API, for example, `My Test ASP.NET Web API`. You will identify this application by its Name whenever working in the Azure portal.
8280
- Mark **Yes** for the **Web App/Web API** setting for your application.
8381
- Set the **Reply URL** to `https://localhost:44332/`. This is the port number that this ASP.NET Web API sample is configured to run on.
84-
- Set the **AppID URI** to `demoapi`. This AppID URI is a unique identifier representing this particular ASP.NET Web API. The AppID URI is used to construct the scopes that are configured in your ASP.NET Web Application. For example, in this ASP.NET Web API sample, the scope will have the value `https://<your-tenant-name>.onmicrosoft.com/demoapi/read`
85-
- Create the application.
82+
- Set the **AppID URI** to `demoapi`. This AppID URI is a unique identifier representing this particular ASP.NET Web API. The AppID URI is used to construct the scopes that are configured in your ASP.NET Web Application. For example, in this ASP.NET Web API sample, the scope will have the value `https://<your-tenant-name>.onmicrosoft.com/demoapi/read`
83+
- Create the application.
8684
- Once the application is created, open your `My Test ASP.NET Web API` application and then open the **Published Scopes** window (in the left nav menu). Add the following 2 scopes:
87-
- **Scope** named `read` followed by a description `demoing a read scenario`.
85+
- **Scope** named `read` followed by a description `demoing a read scenario`.
8886
- **Scope** named `write` followed by a description `demoing a write scenario`.
8987
- Click **Save**.
9088

@@ -116,18 +114,21 @@ In this section, you will change the code in both projects to use your tenant.
116114
1. Find the key `ida:ClientSecret` and replace the value with the Client secret from your web application in in the Azure portal.
117115
1. Find the keys representing the policies, e.g. `ida:SignUpSignInPolicyId` and replace the values with the corresponding policy names you created, e.g. `b2c_1_SiUpIn`
118116
1. Comment out the aadb2cplayground site and uncomment the `locahost:44332` for the TaskServiceUrl – this is the localhost port that the Web API will run on. Your code should look like the following below.
119-
```
117+
118+
```csharp
120119
<!--<add key="api:TaskServiceUrl" value="https://aadb2cplayground.azurewebsites.net/" />-->
121120

122-
<add key="api:TaskServiceUrl" value="https://localhost:44332/"/>
121+
<add key="api:TaskServiceUrl" value="https://localhost:44332/"/>
123122
```
124123

125-
1. Change the `api:ApiIdentifier` key value to the App ID URI of the API you specified in the Web API registration. This App ID URI tells B2C which API your Web Application wants permissions to.
126-
```
124+
1. Change the `api:ApiIdentifier` key value to the App ID URI of the API you specified in the Web API registration. This App ID URI tells B2C which API your Web Application wants permissions to.
125+
126+
```csharp
127127
<!--<add key="api:ApiIdentifier" value="https://fabrikamb2c.onmicrosoft.com/api/" />>
128128

129129
<add key="api:ApiIdentifier" value="https://<your-tenant-name>.onmicrosoft.com/demoapi/" />
130130
```
131+
131132
:memo: Make sure to include the trailing '/' at the end of your `ApiIdentifier` value.
132133

133134
1. Find the keys representing the scopes, e.g. `api:ReadScope` and replace the values with the corresponding scope names you created, e.g. `read`
@@ -147,6 +148,11 @@ You need to run both projects at the same time. If you did not complete the demo
147148

148149
You can now perform all the previous steps as seen in the demo tenant environment.
149150

151+
## Known Issues
152+
153+
- MSAL cache needs a TenantId along with the user's ObjectId to function. It retrieves these two from the claims returned in the id_token. As TenantId is not guranteed to be present in id_tokens issued by B2C unless the steps [listed in this document](https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/AAD-B2C-specifics#caching-with-b2c-in-msalnet),
154+
if you are following the workarounds listed in the doc and tenantId claim (tid) is available in the user's token, then please change the code in [ClaimsPrincipalsExtension.cs](https://github.com/Azure-Samples/active-directory-b2c-dotnet-webapp-and-webapi/blob/nvalluri-b2c/TaskWebApp/Utils/ClaimsPrincipalExtension.cs) GetB2CMsalAccountId() to let MSAL pick this from the claims instead.
155+
150156
## Next Steps
151157

152158
Customize your user experience further by supporting more identity providers. Checkout the docs belows to learn how to add additional providers:
@@ -172,6 +178,4 @@ Additional information regarding this sample can be found in our documentation:
172178
173179
## Questions & Issues
174180

175-
Please file any questions or problems with the sample as a github issue. You can also post on [StackOverflow](https://stackoverflow.com/questions/tagged/azure-ad-b2c) with the tag `azure-ad-b2c`.
176-
177-
181+
Please file any questions or problems with the sample as a github issue. You can also post on [StackOverflow](https://stackoverflow.com/questions/tagged/azure-ad-b2c) with the tag `azure-ad-b2c`.

TaskWebApp/Web.config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
<add key="ClientValidationEnabled" value="true" />
1111
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
1212
<add key="ida:Tenant" value="fabrikamb2c.onmicrosoft.com" />
13+
<!--MSAL cache needs a tenantId along with the user's objectId to function. It retrieves these two from the claims returned in the id_token.
14+
As tenantId is not guranteed to be present in id_tokens issued by B2C unless the steps listed in this
15+
document (https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/AAD-B2C-specifics#caching-with-b2c-in-msalnet).
16+
If you are following the workarounds listed in the doc and tenantId claim (tid) is available in the user's token, then please change the
17+
code in <ClaimsPrincipalsExtension.cs GetB2CMsalAccountId()> to let MSAL pick this from the claims instead -->
1318
<add key="ida:TenantId" value="775527ff-9a37-4307-8b3d-cc311f58d925" />
1419
<add key="ida:ClientId" value="fdb91ff5-5ce6-41f3-bdbd-8267c817015d" />
1520
<add key="ida:ClientSecret" value="X330F3#92!z614M4" />

0 commit comments

Comments
 (0)