From 6198629a72a70b9a39b41f178d31b209101b308a Mon Sep 17 00:00:00 2001 From: Luke Latham <1622880+guardrex@users.noreply.github.com> Date: Mon, 27 Apr 2020 08:09:29 -0500 Subject: [PATCH] TokensProvider (plural) -> TokenProvider (singular) --- .../App.razor | 8 +-- README.md | 65 ++++++++++--------- 2 files changed, 37 insertions(+), 36 deletions(-) diff --git a/BlazorServerAuthWithAzureActiveDirectory/App.razor b/BlazorServerAuthWithAzureActiveDirectory/App.razor index 2ec5cc0..646b110 100644 --- a/BlazorServerAuthWithAzureActiveDirectory/App.razor +++ b/BlazorServerAuthWithAzureActiveDirectory/App.razor @@ -1,5 +1,5 @@ @using BlazorServerAuthWithAzureActiveDirectory.Data -@inject TokenProvider TokensProvider +@inject TokenProvider TokenProvider @@ -19,9 +19,9 @@ protected override Task OnInitializedAsync() { - TokensProvider.AccessToken = InitialState.AccessToken; - TokensProvider.RefreshToken = InitialState.RefreshToken; + TokenProvider.AccessToken = InitialState.AccessToken; + TokenProvider.RefreshToken = InitialState.RefreshToken; return base.OnInitializedAsync(); } -} \ No newline at end of file +} diff --git a/README.md b/README.md index edf4edf..f7d0a22 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,11 @@ # Passing tokens to a server-side Blazor application -* Authenticate your application as you would do with a regular mvc/razor pages application. -* Provision and save the tokens to the authentication cookie -* Define a class to pass in the initial settings for the application: +Authenticate your application as you would do with a regular mvc/razor pages application. + +Provision and save the tokens to the authentication cookie. + +Define a class to pass in the initial settings for the application: + ```csharp namespace BlazorServerAuthWithAzureActiveDirectory.Data { @@ -15,12 +18,9 @@ namespace BlazorServerAuthWithAzureActiveDirectory.Data } ``` -Define a **scoped** service that can be used within the Blazor application to resolve the settings from DI -```csharp -using System; -using System.Security.Claims; -using System.Threading.Tasks; +Define a **scoped** service that can be used within the Blazor application to resolve the settings from DI: +```csharp namespace BlazorServerAuthWithAzureActiveDirectory { public class TokenProvider @@ -30,41 +30,45 @@ namespace BlazorServerAuthWithAzureActiveDirectory } } ``` -On startup + +On startup: + ```csharp services.AddScoped(); ``` -On the _Host cshtml, create and instance of InitialApplicationState and pass that as a parameter to the app: -``` - @{ - var tokens = new InitialApplicationState - { - AccessToken = await HttpContext.GetTokenAsync("access_token"), - RefreshToken = await HttpContext.GetTokenAsync("refresh_token") - }; - } +In *_Host cshtml*, create and instance of `InitialApplicationState` and pass that as a parameter to the app: +```cshtml +@{ + var tokens = new InitialApplicationState + { + AccessToken = await HttpContext.GetTokenAsync("access_token"), + RefreshToken = await HttpContext.GetTokenAsync("refresh_token") + }; +} - - - + + + ``` -On the app component resolve the service and initialize it with the data from the parameter +In the app component, resolve the service and initialize it with the data from the parameter: + ```razor @using BlazorServerAuthWithAzureActiveDirectory.Data -@inject TokenProvider TokensProvider +@inject TokenProvider TokenProvider + ... -@code{ - [Parameter] public InitialApplicationState InitialState { get; set; } +@code{ + [Parameter] + public InitialApplicationState InitialState { get; set; } protected override Task OnInitializedAsync() { - TokensProvider.AccessToken = InitialState.AccessToken; - TokensProvider.RefreshToken = InitialState.RefreshToken; - + TokenProvider.AccessToken = InitialState.AccessToken; + TokenProvider.RefreshToken = InitialState.RefreshToken; return base.OnInitializedAsync(); } @@ -72,22 +76,20 @@ On the app component resolve the service and initialize it with the data from th ``` On your service, inject the token provider and retrieve the token to call the API: + ```csharp public class WeatherForecastService { private readonly TokenProvider _store; - public WeatherForecastService(IHttpClientFactory clientFactory, TokenProvider tokenProvider) { Client = clientFactory.CreateClient(); _store = tokenProvider; } - public HttpClient Client { get; } - public async Task GetForecastAsync(DateTime startDate) { var token = _store.AccessToken; @@ -96,7 +98,6 @@ public class WeatherForecastService var response = await Client.SendAsync(request); response.EnsureSuccessStatusCode(); - return await response.Content.ReadAsAsync(); } }