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: aspnetcore/blazor/call-web-api.md
+2-82Lines changed: 2 additions & 82 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -178,90 +178,10 @@ The [Blazor WebAssembly sample app (BlazorWebAssemblySample)](https://github.com
178
178
179
179
To allow other sites to make cross-origin resource sharing (CORS) requests to your app, see <xref:security/cors>.
180
180
181
-
## HttpClient and HttpRequestMessage with Fetch API request options
182
-
183
-
When running on WebAssembly in a Blazor WebAssembly app, use [HttpClient](xref:fundamentals/http-requests) and <xref:System.Net.Http.HttpRequestMessage> to customize requests. For example, you can specify the request URI, HTTP method, and any desired request headers.
184
-
185
-
```razor
186
-
@using System.Net.Http
187
-
@using System.Net.Http.Headers
188
-
@using System.Net.Http.Json
189
-
@inject HttpClient Http
190
-
191
-
@code {
192
-
private async Task PostRequest()
193
-
{
194
-
var requestMessage = new HttpRequestMessage()
195
-
{
196
-
Method = new HttpMethod("POST"),
197
-
RequestUri = new Uri("https://localhost:10000/api/TodoItems"),
198
-
Content =
199
-
JsonContent.Create(new TodoItem
200
-
{
201
-
Name: "A New Todo Item",
202
-
IsComplete: false
203
-
})
204
-
};
205
-
206
-
requestMessage.Headers.Authorization =
207
-
new AuthenticationHeaderValue("Bearer", "{OAUTH TOKEN}");
You can set additional options using the more generic `SetBrowserRequestOption` extension method.
231
-
232
-
The HTTP response is typically buffered in a Blazor WebAssembly app to enable support for sync reads on the response content. To enable support for response streaming, use the `SetBrowserResponseStreamingEnabled` extension method on the request.
233
-
234
-
To include credentials in a cross-origin request, use the `SetBrowserRequestCredentials` extension method:
For more information on Fetch API options, see [MDN web docs: WindowOrWorkerGlobalScope.fetch():Parameters](https://developer.mozilla.org/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters).
241
-
242
-
When sending credentials (authorization cookies/headers) on CORS requests, the `Authorization` header must be allowed by the CORS policy.
*`Content-Type` and `Authorization` headers. To allow a custom header (for example, `x-custom-header`), list the header when calling <xref:Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicyBuilder.WithHeaders*>.
249
-
* Credentials set by client-side JavaScript code (`credentials` property set to `include`).
Copy file name to clipboardExpand all lines: aspnetcore/includes/blazor-security/fetchdata-component.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ The `FetchData` component shows how to:
5
5
6
6
The `@attribute [Authorize]` directive indicates to the Blazor WebAssembly authorization system that the user must be authorized in order to visit this component. The presence of the attribute in the *Client* app doesn't prevent the API on the server from being called without proper credentials. The *Server* app also must use `[Authorize]` on the appropriate endpoints to correctly protect them.
7
7
8
-
`AuthenticationService.RequestAccessToken();` takes care of requesting an access token that can be added to the request to call the API. If the token is cached or the service is able to provision a new access token without user interaction, the token request succeeds. Otherwise, the token request fails.
8
+
`IAccessTokenProvider.RequestAccessToken();` takes care of requesting an access token that can be added to the request to call the API. If the token is cached or the service is able to provision a new access token without user interaction, the token request succeeds. Otherwise, the token request fails with an `AccessTokenNotAvailableException` error, which is caught in a `try-catch` statement.
9
9
10
10
In order to obtain the actual token to include in the request, the app must check that the request succeeded by calling `tokenResult.TryGetToken(out var token)`.
@@ -418,6 +418,7 @@ If the app determines that the underlying authentication state data has changed
418
418
If the app is required to check authorization rules as part of procedural logic, use a cascaded parameter of type `Task<AuthenticationState>` to obtain the user's <xref:System.Security.Claims.ClaimsPrincipal>. `Task<AuthenticationState>` can be combined with other services, such as `IAuthorizationService`, to evaluate policies.
Most apps only require an access token to interact with the protected resources that they use. In some scenarios, an app might require more than one token in order to interact with two or more resources.
23
-
24
-
In the following example, additional Azure Active Directory (AAD) Microsoft Graph API scopes are required by an app to read user data and send mail. After adding the Microsoft Graph API permissions in the Azure AAD portal, the additional scopes are configured in the Client app (`Program.Main`, *Program.cs*):
The `IAccessTokenProvider.RequestToken` method provides an overload that allows an app to provision an access token with a given set of scopes, asseeninthefollowingexample:
The `AuthorizationMessageHandler` service can be used with `HttpClient` to attach access tokens to outgoing requests. Tokens are acquired using the existing `IAccessTokenProvider` service. If a token can't be acquired, an `AccessTokenNotAvailableException` is thrown. `AccessTokenNotAvailableException` has a `Redirect` method that can be used to navigate the user to the identity provider to acquire a new token. The `AuthorizationMessageHandler` can be configured with the authorized URLs, scopes, and return URL using the `ConfigureHandler` method.
Access tokens can be manually obtained by calling `IAccessTokenProvider.RequestAccessToken`.
126
+
127
+
In the following example, additional Azure Active Directory (AAD) Microsoft Graph API scopes are required by an app to read user data and send mail. After adding the Microsoft Graph API permissions in the Azure AAD portal, the additional scopes are configured in the Client app (`Program.Main`, *Program.cs*):
The `IAccessTokenProvider.RequestToken` method provides an overload that allows an app to provision an access token with a given set of scopes, asseeninthefollowingexample:
FormoreinformationonFetchAPIoptions, see [MDNwebdocs: WindowOrWorkerGlobalScope.fetch():Parameters](https://developer.mozilla.org/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters).
253
+
254
+
Whensendingcredentials (authorizationcookies/headers) onCORSrequests, the `Authorization` headermustbeallowedbytheCORSpolicy.
0 commit comments