Skip to content

Commit f6df29d

Browse files
committed
Readme Updates
1 parent eb078c3 commit f6df29d

File tree

2 files changed

+36
-46
lines changed

2 files changed

+36
-46
lines changed

4.-Console-app-calls-web-API-with-PoP/README-incremental.md

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ client: .NET Desktop (WPF)
77
service: ASP.NET Core Web API
88
endpoint: Microsoft identity platform
99
---
10-
# Sign-in a user with the Microsoft Identity Platform in a WPF Desktop application and call an ASP.NET Core Web API using Proof of Possession token
10+
# Sign-in a user with the Microsoft Identity Platform in a console application and call an ASP.NET Core Web API using Proof of Possession token
1111

1212
[![Build status](https://identitydivision.visualstudio.com/IDDP/_apis/build/status/AAD%20Samples/.NET%20client%20samples/active-directory-dotnet-native-aspnetcore-v2)](https://identitydivision.visualstudio.com/IDDP/_build/latest?definitionId=516)
1313

@@ -34,7 +34,7 @@ In the fourth chapter, we would enhance our protected Web API using Azure AD [Pr
3434

3535
### Overview
3636

37-
In This sample, the Web API is called by a .NET Desktop WPF application.
37+
In This sample, the Web API is called by a .NET console application.
3838

3939
The .Net application uses the Microsoft Authentication Library [MSAL.NET](https://aka.ms/msal-net) to obtain a JWT [Access Token](https://docs.microsoft.com/azure/active-directory/develop/access-tokens) through the [OAuth 2.0](https://docs.microsoft.com/azure/active-directory/develop/active-directory-protocols-oauth-code) protocol. The access token is sent to the ASP.NET Core Web API, which authorizes the user using the ASP.NET JWT Bearer Authentication middleware.
4040

@@ -44,13 +44,12 @@ The .Net application uses the Microsoft Authentication Library [MSAL.NET](https:
4444

4545
The Web API (TodoListService) maintains an in-memory collection of to-do items for each authenticated user. Several applications signed-in under the same identity will share the same to-do list.
4646

47-
The WPF application (TodoListClient) allows a user to:
47+
The desktop application (TodoListClient) allows a user to:
4848

49-
- Sign-in. The first time a user signs in, a consent screen is presented where the user consents for the application accessing the TodoList Service on their behalf.
50-
- When the user has signed-in, the user is presented with a list of to-do items fetched from the Web API for this signed-in identity.
51-
- The user can add more to-do items by clicking on *Add item* button.
49+
- Enter an item. When the user enters the first item, sign-in screen is displayed. The first time a user signs in, a consent screen is presented where the user consents for the application accessing the TodoList Service on their behalf.
50+
- Each time, the user enters an item, a list of to-do items are fetched from the Web API for this signed-in identity.
5251

53-
Next time a user runs the application, the user is signed-in with the same identity as the WPF application maintains a cache on disk. Users can clear the cache (which will have the effect of them signing out).
52+
Next time a user runs the application, the user is signed-in with the same identity as the console application maintains a cache on disk. Users can clear the cache (which will have the effect of them signing out).
5453

5554
![TodoList Client](./ReadmeFiles/todolist-client.png)
5655

@@ -90,15 +89,15 @@ When you start the Web API from Visual Studio, depending on the browser you use,
9089
- an empty web page (with Microsoft Edge)
9190
- or an error HTTP 401 (with Chrome)
9291

93-
This behavior is expected as the browser is not authenticated. The WPF application will be authenticated, so it will be able to access the Web API.
92+
This behavior is expected as the browser is not authenticated. The console application will be authenticated, so it will be able to access the Web API.
9493

9594
Explore the sample by signing in into the TodoList client, adding items to the To Do list, removing the user account (clearing the cache), and starting again. As explained, if you stop the application without removing the user account, the next time you run the application, you won't be prompted to sign in again. That is because the sample implements a persistent cache for MSAL, and remembers the tokens from the previous run.
9695

9796
NOTE: Remember, the To-Do list is stored in memory in this `TodoListService-v2` sample. Each time you run the TodoListService API, your To-Do list will get emptied.
9897

9998
## How was the code created
10099

101-
### Code for the WPF app
100+
### Code for the console app
102101

103102
The focus of this tutorial is PoP (Proof of Possession).
104103

@@ -116,52 +115,41 @@ In `MainWindow.xaml.cs`, You'll need to:
116115
.Build();
117116
```
118117

119-
- Create an `HttpRequestMessage` by passing the verb (for instance `HttpMethod.Get`) and the URL of the Web API to call.
118+
- Create an `HttpRequestMessage` by passing the verb (for instance `HttpMethod.Post`) and the URL of the Web API to call.
120119
```csharp
121-
HttpRequestMessage readRequest =
122-
new HttpRequestMessage(HttpMethod.Get, new Uri(TodoListApiAddress));
120+
HttpRequestMessage writeRequest =
121+
new HttpRequestMessage(HttpMethod.Post, new Uri(TodoListApiAddress));
123122
```
124123

125-
- Call the `AcquireTokenSilent` and pass `readRequest` parameter to `WithProofOfPossesssion` method as shown below:
124+
- Call the `AcquireTokenSilent` or `AcquireTokenInteractive`, and pass `writeRequest` parameter to `WithProofOfPossesssion` method as shown below:
126125

127126
```csharp
128-
private async Task GetTodoList(bool isAppStarting)
129-
{
130-
var accounts = (await _app.GetAccountsAsync()).ToList();
131-
132-
HttpRequestMessage readRequest = new HttpRequestMessage(HttpMethod.Get, new Uri(TodoListApiAddress));
133-
134-
AuthenticationResult result = null;
135-
try
136-
{
137-
result = await _app.AcquireTokenSilent(Scopes, accounts.FirstOrDefault())
138-
.WithProofOfPosession(readRequest)
139-
.ExecuteAsync()
140-
.ConfigureAwait(false);
141-
142-
Dispatcher.Invoke(
143-
() =>
144-
{
145-
...
146-
});
147-
}
148-
catch (MsalUiRequiredException)
127+
TodoItem todoItem = ReadItemFromConsole();
128+
// Add Pop token to the HttpRequestMessage, attempting from the cache
129+
// and otherwise interactively
130+
try
149131
{
150-
...
132+
var account = (await app.GetAccountsAsync()).FirstOrDefault();
133+
result = await app.AcquireTokenSilent(Scopes, account)
134+
.WithProofOfPosession(writeRequest)
135+
.ExecuteAsync();
151136
}
152-
catch (MsalException ex)
137+
catch (MsalUiRequiredException)
153138
{
154-
...
139+
result = await app.AcquireTokenInteractive(Scopes)
140+
.WithProofOfPosession(writeRequest)
141+
.ExecuteAsync();
155142
}
156143

157-
// Once the token has been returned by MSAL, add it to the http authorization header, before making the call to access the To Do list service.
158-
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("PoP", result.AccessToken);
159-
160-
// Call the To Do list service.
161-
HttpResponseMessage response = await _httpClient.GetAsync(TodoListApiAddress);
162-
...
163-
}
164-
```
144+
// Call the Web API
145+
string json = JsonConvert.SerializeObject(todoItem);
146+
StringContent content = new StringContent(json,
147+
Encoding.UTF8,
148+
"application/json");
149+
writeRequest.Content = content;
150+
await httpClient.SendAsync(writeRequest);
151+
152+
```
165153

166154
### Code for the Web API (TodoListService)
167155

4.-Console-app-calls-web-API-with-PoP/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,9 @@ NOTE: Remember, the To-Do list is stored in memory in this `TodoListService-v2`
222222

223223
The focus of this tutorial is PoP (Proof of Possession).
224224

225-
With PoP, the programming model is a bit different from the way MSAL.NET usually works. A PoP token contains information about the intended URL and the HTTP verb (POST, GET). Therefore, to get a PoP token you will provide to MSAL an `HttpRequestMessage` and MSAL.NET will populate the Authorization header of this message with a PoP token. You'll need to:
225+
With PoP, the programming model is a bit different from the way MSAL.NET usually works. A PoP token contains information about the intended URL and the HTTP verb (POST, GET). Therefore, to get a PoP token you will provide to MSAL an `HttpRequestMessage` and MSAL.NET will populate the Authorization header of this message with a PoP token.
226+
227+
In `Program.cs`, You'll need to:
226228

227229
- Instantiate a `IPublicClientApplication` specifying `WithExperimentalFeatures()`, as PoP is still an experimental feature for MSAL.NET (and implemented for only public client applications on .NET Framework).
228230

0 commit comments

Comments
 (0)