Skip to content

Commit

Permalink
Pass access token to the Routes component to allow it to populate the…
Browse files Browse the repository at this point in the history
… TokenProvider in the proper scope

-- dotnet/AspNetCore.Docs#31691 (comment)
  • Loading branch information
Choonster committed Mar 9, 2024
1 parent 0b46147 commit f6e034d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
30 changes: 7 additions & 23 deletions CatalogueScanner.ConfigurationUI/Components/App.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
@using Service

@inject ITokenAcquisition TokenAcquisition
@inject TokenProvider TokenProvider
@inject ILogger<App> Logger

<!DOCTYPE html>
Expand All @@ -20,7 +19,11 @@
<HeadOutlet @rendermode="InteractiveServer" />
</head>
<body class="mat">
<Routes @rendermode="InteractiveServer" />
@*
Pass access token to the Routes component to allow it to populate the TokenProvider in the proper scope
https://github.com/dotnet/AspNetCore.Docs/issues/31691#issuecomment-1937358269
*@
<Routes AccessToken="@AccessToken" @rendermode="InteractiveServer" />

<script src="BlazorInterop.js"></script>
<script src="_framework/blazor.web.js"></script>
Expand All @@ -32,16 +35,10 @@
</html>

@code {
[CascadingParameter]
public HttpContext? HttpContext { get; set; }
private string? AccessToken { get; set; }

protected override async Task OnInitializedAsync()
{
if (HttpContext is null)
{
throw new InvalidOperationException($"{nameof(HttpContext)} parameter is null");
}

using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));

cts.Token.Register(() => Logger.LogError("Token cancelled"));
Expand All @@ -50,20 +47,7 @@
.GetAccessTokenForUserAsync([".default"], tokenAcquisitionOptions: new() { CancellationToken = cts.Token })
.ConfigureAwait(true);

Logger.LogWarning("Access Token: {Token}", accessToken);

string[] importantHeaders = [
"X-MS-TOKEN-AAD-ID-TOKEN",
"X-MS-CLIENT-PRINCIPAL-IDP",
"X-MS-TOKEN-AAD-ACCESS-TOKEN"
];

foreach (var header in importantHeaders)
{
Logger.LogWarning("Header: {Header} = {Value}", header, HttpContext.Request.Headers[header]);
}

TokenProvider.AccessToken = accessToken;
AccessToken = accessToken;

await base.OnInitializedAsync();
}
Expand Down
19 changes: 18 additions & 1 deletion CatalogueScanner.ConfigurationUI/Components/Routes.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<Router AppAssembly="@typeof(Program).Assembly">
@using CatalogueScanner.ConfigurationUI.Service

@inject TokenProvider TokenProvider
@inject ILogger<Routes> Logger

<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(Layout.MainLayout)" />
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
Expand All @@ -10,3 +15,15 @@
</LayoutView>
</NotFound>
</Router>

@code {
[Parameter]
public string? AccessToken { get; set; }

protected override void OnInitialized()
{
TokenProvider.AccessToken = AccessToken;

base.OnInitialized();
}
}

0 comments on commit f6e034d

Please sign in to comment.