Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TokensProvider (plural) -> TokenProvider (singular) #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions BlazorServerAuthWithAzureActiveDirectory/App.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@using BlazorServerAuthWithAzureActiveDirectory.Data
@inject TokenProvider TokensProvider
@inject TokenProvider TokenProvider

<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly">
Expand All @@ -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();
}
}
}
65 changes: 33 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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
Expand All @@ -30,64 +30,66 @@ namespace BlazorServerAuthWithAzureActiveDirectory
}
}
```
On startup

On startup:

```csharp
services.AddScoped<TokenProvider>();
```

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")
};
}

<app>
<component type="typeof(App)" param-InitialState="tokens" render-mode="ServerPrerendered" />
</app>
<app>
<component type="typeof(App)" param-InitialState="tokens" render-mode="ServerPrerendered" />
</app>
```

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();
}
}
```

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<WeatherForecast[]> GetForecastAsync(DateTime startDate)
{
var token = _store.AccessToken;
Expand All @@ -96,7 +98,6 @@ public class WeatherForecastService
var response = await Client.SendAsync(request);
response.EnsureSuccessStatusCode();


return await response.Content.ReadAsAsync<WeatherForecast[]>();
}
}
Expand Down