|
| 1 | +--- |
| 2 | +title: ASP.NET Core Blazor Server additional security scenarios |
| 3 | +author: guardrex |
| 4 | +description: Learn how to configure Blazor Server for additional security scenarios. |
| 5 | +monikerRange: '>= aspnetcore-3.1' |
| 6 | +ms.author: riande |
| 7 | +ms.custom: mvc |
| 8 | +ms.date: 04/27/2020 |
| 9 | +no-loc: [Blazor, SignalR] |
| 10 | +uid: security/blazor/server/additional-scenarios |
| 11 | +--- |
| 12 | +# ASP.NET Core Blazor Server additional security scenarios |
| 13 | + |
| 14 | +By [Javier Calvarro Nelson](https://github.com/javiercn) |
| 15 | + |
| 16 | +## Pass tokens to a Blazor Server app |
| 17 | + |
| 18 | +Tokens available outside of the Razor components in a Blazor Server app can be passed to components with the approach described in this section. For sample code, including a complete `Startup.ConfigureServices` example, see the [Passing tokens to a server-side Blazor application](https://github.com/javiercn/blazor-server-aad-sample). |
| 19 | + |
| 20 | +Authenticate the Blazor Server app as you would with a regular Razor Pages or MVC app. Provision and save the tokens to the authentication cookie. For example: |
| 21 | + |
| 22 | +```csharp |
| 23 | +using Microsoft.AspNetCore.Authentication.OpenIdConnect; |
| 24 | + |
| 25 | +... |
| 26 | + |
| 27 | +services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options => |
| 28 | +{ |
| 29 | + options.ResponseType = "code"; |
| 30 | + options.SaveTokens = true; |
| 31 | + |
| 32 | + options.Scope.Add("offline_access"); |
| 33 | + options.Scope.Add("{SCOPE}"); |
| 34 | + options.Resource = "{RESOURCE}"; |
| 35 | +}); |
| 36 | +``` |
| 37 | + |
| 38 | +Define a class to pass in the initial app state with the access and refresh tokens: |
| 39 | + |
| 40 | +```csharp |
| 41 | +public class InitialApplicationState |
| 42 | +{ |
| 43 | + public string AccessToken { get; set; } |
| 44 | + public string RefreshToken { get; set; } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +Define a **scoped** token provider service that can be used within the Blazor app to resolve the tokens from [dependency injection (DI)](xref:blazor/dependency-injection): |
| 49 | + |
| 50 | +```csharp |
| 51 | +public class TokenProvider |
| 52 | +{ |
| 53 | + public string AccessToken { get; set; } |
| 54 | + public string RefreshToken { get; set; } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +In `Startup.ConfigureServices`, add services for: |
| 59 | + |
| 60 | +* `IHttpClientFactory` |
| 61 | +* `TokenProvider` |
| 62 | + |
| 63 | +```csharp |
| 64 | +services.AddHttpClient(); |
| 65 | +services.AddScoped<TokenProvider>(); |
| 66 | +``` |
| 67 | + |
| 68 | +In the *_Host.cshtml* file, create and instance of `InitialApplicationState` and pass it as a parameter to the app: |
| 69 | + |
| 70 | +```cshtml |
| 71 | +@using Microsoft.AspNetCore.Authentication |
| 72 | +
|
| 73 | +... |
| 74 | +
|
| 75 | +@{ |
| 76 | + var tokens = new InitialApplicationState |
| 77 | + { |
| 78 | + AccessToken = await HttpContext.GetTokenAsync("access_token"), |
| 79 | + RefreshToken = await HttpContext.GetTokenAsync("refresh_token") |
| 80 | + }; |
| 81 | +} |
| 82 | +
|
| 83 | +<app> |
| 84 | + <component type="typeof(App)" param-InitialState="tokens" |
| 85 | + render-mode="ServerPrerendered" /> |
| 86 | +</app> |
| 87 | +``` |
| 88 | + |
| 89 | +In the `App` component (*App.razor*), resolve the service and initialize it with the data from the parameter: |
| 90 | + |
| 91 | +```razor |
| 92 | +@inject TokenProvider TokenProvider |
| 93 | +
|
| 94 | +... |
| 95 | +
|
| 96 | +@code { |
| 97 | + [Parameter] |
| 98 | + public InitialApplicationState InitialState { get; set; } |
| 99 | +
|
| 100 | + protected override Task OnInitializedAsync() |
| 101 | + { |
| 102 | + TokenProvider.AccessToken = InitialState.AccessToken; |
| 103 | + TokenProvider.RefreshToken = InitialState.RefreshToken; |
| 104 | +
|
| 105 | + return base.OnInitializedAsync(); |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +In the service that makes a secure API request, inject the token provider and retrieve the token to call the API: |
| 111 | + |
| 112 | +```csharp |
| 113 | +public class WeatherForecastService |
| 114 | +{ |
| 115 | + private readonly TokenProvider _store; |
| 116 | + |
| 117 | + public WeatherForecastService(IHttpClientFactory clientFactory, |
| 118 | + TokenProvider tokenProvider) |
| 119 | + { |
| 120 | + Client = clientFactory.CreateClient(); |
| 121 | + _store = tokenProvider; |
| 122 | + } |
| 123 | + |
| 124 | + public HttpClient Client { get; } |
| 125 | + |
| 126 | + public async Task<WeatherForecast[]> GetForecastAsync(DateTime startDate) |
| 127 | + { |
| 128 | + var token = _store.AccessToken; |
| 129 | + var request = new HttpRequestMessage(HttpMethod.Get, |
| 130 | + "https://localhost:5003/WeatherForecast"); |
| 131 | + request.Headers.Add("Authorization", $"Bearer {token}"); |
| 132 | + var response = await Client.SendAsync(request); |
| 133 | + response.EnsureSuccessStatusCode(); |
| 134 | + |
| 135 | + return await response.Content.ReadAsAsync<WeatherForecast[]>(); |
| 136 | + } |
| 137 | +} |
| 138 | +``` |
0 commit comments