You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/active-directory/develop/tutorial-blazor-webassembly.md
+27-30Lines changed: 27 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,87 +1,83 @@
1
1
---
2
-
title: Tutorial - Sign in users and call a protected API from a Blazor WebAssembly app
2
+
title: Tutorial - Sign in users and call a protected API from a Blazor WebAssembly app
3
3
description: In this tutorial, sign in users and call a protected API using the Microsoft identity platform in a Blazor WebAssembly (WASM) app.
4
4
author: janicericketts
5
5
ms.author: jricketts
6
6
ms.service: active-directory
7
7
ms.subservice: develop
8
8
ms.topic: tutorial
9
-
ms.date: 10/16/2020
9
+
ms.date: 12/14/2022
10
10
#Customer intent: As a developer, I want to add authentication and authorization to a Blazor WebAssembly app and call Microsoft Graph.
11
11
---
12
12
13
13
# Tutorial: Sign in users and call a protected API from a Blazor WebAssembly app
14
14
15
-
In this tutorial, you build a Blazor WebAssembly app that signs in users and gets data from Microsoft Graph by using the Microsoft identity platform and registering your app in Azure Active Directory (Azure AD).
15
+
In this tutorial, you build a Blazor WebAssembly app that signs in users and gets data from Microsoft Graph by using the Microsoft identity platform and registering your app in Azure Active Directory (Azure AD).
16
16
17
17
In this tutorial:
18
18
19
19
> [!div class="checklist"]
20
20
>
21
-
> * Create a new Blazor WebAssembly app configured to use Azure Active Directory (Azure AD) for [authentication and authorization](authentication-vs-authorization.md) using the Microsoft identity platform
22
-
> * Retrieve data from a protected web API, in this case [Microsoft Graph](/graph/overview)
21
+
> - Create a new Blazor WebAssembly app configured to use Azure AD for [authentication and authorization](authentication-vs-authorization.md)
22
+
> - Retrieve data from a protected web API, in this case [Microsoft Graph](/graph/overview)
23
23
24
-
This tutorial uses .NET Core 3.1. The .NET docs contain instructions on [how to secure a Blazor WebAssembly app](/aspnet/core/blazor/security/webassembly/graph-api) using ASP.NET Core 5.0.
24
+
This tutorial uses .NET Core 7.0.
25
25
26
-
We also have a [tutorial for Blazor Server](tutorial-blazor-server.md).
26
+
We also have a [tutorial for Blazor Server](tutorial-blazor-server.md).
* An Azure AD tenant where you can register an app. If you don't have access to an Azure AD tenant, you can get one by registering with the [Microsoft 365 Developer Program](https://developer.microsoft.com/microsoft-365/dev-program) or by creating an [Azure free account](https://azure.microsoft.com/free).
- An Azure AD tenant where you can register an app. If you don't have access to an Azure AD tenant, you can get one by registering with the [Microsoft 365 Developer Program](https://developer.microsoft.com/microsoft-365/dev-program) or by creating an [Azure free account](https://azure.microsoft.com/free).
32
32
33
33
## Register the app in the Azure portal
34
34
35
-
Every app that uses Azure Active Directory (Azure AD) for authentication must be registered with Azure AD. Follow the instructions in [Register an application](quickstart-register-app.md) with these specifications:
35
+
Every app that uses Azure AD for authentication must be registered with Azure AD. Follow the instructions in [Register an application](quickstart-register-app.md) with these specifications:
36
36
37
37
- For **Supported account types**, select **Accounts in this organizational directory only**.
38
38
- Set the **Redirect URI** drop down to **Single-page application (SPA)** and enter `https://localhost:5001/authentication/login-callback`. The default port for an app running on Kestrel is 5001. If the app is available on a different port, specify that port number instead of `5001`.
39
39
40
-
Once registered, under **Manage**, select **Authentication** > **Implicit grant and hybrid flows**. Select **Access tokens** and **ID tokens**, and then select **Save**.
41
-
42
-
> Note: if you're using .NET 6 or later then you don't need to use Implicit grant. The latest template uses MSAL Browser 2.0 and supports Auth Code Flow with PKCE
43
-
44
40
## Create the app using the .NET Core CLI
45
41
46
-
To create the app you need the latest Blazor templates. You can install them for the .NET Core CLI with the following command:
42
+
To create the app, you need the latest Blazor templates. You can install them for the .NET Core CLI with the following command:
47
43
48
44
```dotnetcli
49
-
dotnet new -i Microsoft.Identity.Web.ProjectTemplates::1.9.1
45
+
dotnet new install Microsoft.Identity.Web.ProjectTemplates
50
46
```
51
47
52
48
Then run the following command to create the application. Replace the placeholders in the command with the proper information from your app's overview page and execute the command in a command shell. The output location specified with the `-o|--output` option creates a project folder if it doesn't exist and becomes part of the app's name.
|`{CLIENT ID}`| Application (client) ID |`41451fa7-0000-0000-0000-69eff5a761fd`|
62
58
|`{TENANT ID}`| Directory (tenant) ID |`e86c78e2-0000-0000-0000-918e0565a45e`|
63
59
64
60
## Test the app
65
61
66
-
You can now build and run the app. When you run this template app, you must specify the framework to run using --framework. This tutorial uses the .NET Standard 2.1, but the template supports other frameworks as well.
62
+
You can now build and run the app. In your terminal, run the following command:
67
63
68
64
```dotnetcli
69
-
dotnet run --framework netstandard2.1
65
+
dotnet run
70
66
```
71
67
72
-
In your browser, navigate to `https://localhost:5001`, and log in using an Azure AD user account to see the app running and logging users in with the Microsoft identity platform.
68
+
In your browser, navigate to `https://localhost:<port number>`, and log in using an Azure AD user account to see the app running and logging users in with the Microsoft identity platform.
73
69
74
-
The components of this template that enable logins with Azure AD using the Microsoft identity platform are explained in the [ASP.NET doc on this topic](/aspnet/core/blazor/security/webassembly/standalone-with-azure-active-directory#authentication-package).
70
+
The components of this template that enable logins with Azure AD using the Microsoft identity platform are explained in the [ASP.NET doc on this article](/aspnet/core/blazor/security/webassembly/standalone-with-azure-active-directory#authentication-package).
75
71
76
72
## Retrieving data from a protected API (Microsoft Graph)
77
73
78
74
[Microsoft Graph](/graph/overview) contains APIs that provide access to Microsoft 365 data for your users, and it supports the tokens issued by the Microsoft identity platform, which makes it a good protected API to use as an example. In this section, you add code to call Microsoft Graph and display the user's emails on the application's "Fetch data" page.
79
75
80
76
This section is written using a common approach to calling a protected API using a named client. The same method can be used for other protected APIs you want to call. However, if you do plan to call Microsoft Graph from your application you can use the Graph SDK to reduce boilerplate. The .NET docs contain instructions on [how to use the Graph SDK](/aspnet/core/blazor/security/webassembly/graph-api?view=aspnetcore-5.0&preserve-view=true).
81
77
82
-
Before you start, log out of your app since you'll be making changes to the required permissions, and your current token won't work. If you haven't already, run your app again and select **Log out** before updating the code below.
78
+
Before you start, log out of your app since you'll be making changes to the required permissions, and your current token won't work. If you haven't already, run your app again and select **Log out** before updating the code in your app.
83
79
84
-
Now you will update your app's registration and code to pull a user's emails and display the messages within the app.
80
+
Now you'll update your app's registration and code to pull a user's emails and display the messages within the app.
85
81
86
82
First, add the `Mail.Read` API permission to the app's registration so that Azure AD is aware that the app will request to access its users' email.
87
83
@@ -91,15 +87,15 @@ First, add the `Mail.Read` API permission to the app's registration so that Azur
91
87
1. Select **Delegated Permissions**, then search for and select the **Mail.Read** permission.
92
88
1. Select **Add permissions**.
93
89
94
-
Next, add the following to your project's *.csproj* file in the netstandard2.1**ItemGroup**. This will allow you to create the custom HttpClient in the next step.
90
+
Next, add the following to your project's _.csproj_ file in the **ItemGroup**. This will allow you to create the custom HttpClient in the next step.
Then modify the code as specified in the next few steps. These changes will add [access tokens](access-tokens.md) to the outgoing requests sent to the Microsoft Graph API. This pattern is discussed in more detail in [ASP.NET Core Blazor WebAssembly additional security scenarios](/aspnet/core/blazor/security/webassembly/additional-scenarios).
101
97
102
-
First, create a new file named *GraphAPIAuthorizationMessageHandler.cs* with the following code. This handler will be user to add an access token for the `User.Read` and `Mail.Read` scopes to outgoing requests to the Microsoft Graph API.
98
+
First, create a new file named _GraphAPIAuthorizationMessageHandler.cs_ with the following code. This handler will be user to add an access token for the `User.Read` and `Mail.Read` scopes to outgoing requests to the Microsoft Graph API.
103
99
104
100
```csharp
105
101
usingMicrosoft.AspNetCore.Components;
@@ -118,11 +114,12 @@ public class GraphAPIAuthorizationMessageHandler : AuthorizationMessageHandler
118
114
}
119
115
```
120
116
121
-
Then, replace the contents of the `Main` method in *Program.cs* with the following code. This code makes use of the new `GraphAPIAuthorizationMessageHandler` and adds `User.Read` and `Mail.Read` as default scopes the app will request when the user first signs in.
117
+
Then, replace the contents from the line that start with `var` to the end of the file in _Program.cs_ with the following code. This code makes use of the new `GraphAPIAuthorizationMessageHandler` and adds `User.Read` and `Mail.Read` as default scopes the app will request when the user first signs in.
Finally,replace the contents of the *FetchData.razor* page with the following code. This code fetches user email data from the Microsoft Graph API and displays them as a list. In `OnInitializedAsync`, the new `HttpClient` that uses the proper access token is created and used to make the request to the Microsoft Graph API.
140
+
Finally,in the _Pages_ folder, replace the contents of the _FetchData.razor_ page with the following code. This code fetches user email data from the Microsoft Graph API and displays them as a list. In `OnInitializedAsync`, the new `HttpClient` that uses the proper access token is created and used to make the request to the Microsoft Graph API.
0 commit comments