-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
515 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace BlazorApp.Client.BFF; | ||
|
||
public class AntiforgeryHandler : DelegatingHandler | ||
{ | ||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
request.Headers.Add("X-CSRF", "1"); | ||
return base.SendAsync(request, cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using System.Net; | ||
using System.Net.Http.Json; | ||
using System.Security.Claims; | ||
using Microsoft.AspNetCore.Components.Authorization; | ||
|
||
namespace BlazorApp.Client.BFF; | ||
|
||
public class BffAuthenticationStateProvider : AuthenticationStateProvider | ||
{ | ||
private static readonly TimeSpan UserCacheRefreshInterval = TimeSpan.FromSeconds(60); | ||
|
||
private readonly HttpClient _client; | ||
private readonly ILogger<BffAuthenticationStateProvider> _logger; | ||
|
||
private DateTimeOffset _userLastCheck = DateTimeOffset.FromUnixTimeSeconds(0); | ||
private ClaimsPrincipal _cachedUser = new(new ClaimsIdentity()); | ||
|
||
public BffAuthenticationStateProvider( | ||
HttpClient client, | ||
ILogger<BffAuthenticationStateProvider> logger) | ||
{ | ||
_client = client; | ||
_logger = logger; | ||
} | ||
|
||
public override async Task<AuthenticationState> GetAuthenticationStateAsync() | ||
{ | ||
var user = await GetUser(); | ||
var state = new AuthenticationState(user); | ||
|
||
// checks periodically for a session state change and fires event | ||
// this causes a round trip to the server | ||
// adjust the period accordingly if that feature is needed | ||
if (user.Identity.IsAuthenticated) | ||
{ | ||
_logger.LogInformation("starting background check.."); | ||
Timer? timer = null; | ||
|
||
timer = new Timer(async _ => | ||
{ | ||
var currentUser = await GetUser(false); | ||
if (currentUser.Identity.IsAuthenticated == false) | ||
{ | ||
_logger.LogInformation("user logged out"); | ||
NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(currentUser))); | ||
await timer.DisposeAsync(); | ||
} | ||
}, null, 1000, 5000); | ||
} | ||
|
||
return state; | ||
} | ||
|
||
private async ValueTask<ClaimsPrincipal> GetUser(bool useCache = true) | ||
{ | ||
var now = DateTimeOffset.Now; | ||
if (useCache && now < _userLastCheck + UserCacheRefreshInterval) | ||
{ | ||
_logger.LogDebug("Taking user from cache"); | ||
return _cachedUser; | ||
} | ||
|
||
_logger.LogDebug("Fetching user"); | ||
_cachedUser = await FetchUser(); | ||
_userLastCheck = now; | ||
|
||
return _cachedUser; | ||
} | ||
|
||
public record ClaimRecord(string Type, object Value); | ||
|
||
private async Task<ClaimsPrincipal> FetchUser() | ||
{ | ||
try | ||
{ | ||
_logger.LogInformation("Fetching user information."); | ||
var response = await _client.GetAsync("bff/user?slide=false"); | ||
|
||
if (response.StatusCode == HttpStatusCode.OK) | ||
{ | ||
var claims = await response.Content.ReadFromJsonAsync<List<ClaimRecord>>(); | ||
|
||
var identity = new ClaimsIdentity( | ||
nameof(BffAuthenticationStateProvider), | ||
"name", | ||
"role"); | ||
|
||
foreach (var claim in claims!) | ||
{ | ||
identity.AddClaim(new Claim(claim.Type, claim.Value.ToString())); | ||
} | ||
|
||
return new ClaimsPrincipal(identity); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogWarning(ex, "Fetching user failed."); | ||
} | ||
|
||
return new ClaimsPrincipal(new ClaimsIdentity()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.12" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="7.0.12" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.12" PrivateAssets="all" /> | ||
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.12" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.12" PrivateAssets="all" /> | ||
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="7.0.12" /> | ||
|
||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\BlazorShared\BlazorShared.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,5 @@ | ||
@page "/" | ||
|
||
@using Microsoft.AspNetCore.Components.Authorization | ||
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication | ||
@using System.Security.Claims | ||
|
||
@inject AuthenticationStateProvider AuthenticationStateProvider | ||
@page "/" | ||
|
||
<PageTitle>Index</PageTitle> | ||
|
||
<h1>BlazorWASM Keycloak Authentication</h1> | ||
|
||
This application demonstrates how to integrate with Keycloak from BlazorWASM application. | ||
|
||
@if (claims.Count() > 0) | ||
{ | ||
<h2>User Claims:</h2> | ||
<ul> | ||
@foreach (var claim in claims) | ||
{ | ||
<li>@claim.Type: @claim.Value</li> | ||
} | ||
</ul> | ||
} | ||
|
||
@code { | ||
private IEnumerable<Claim> claims = Enumerable.Empty<Claim>(); | ||
|
||
protected override Task OnInitializedAsync() => GetClaimsPrincipalData(); | ||
|
||
private async Task GetClaimsPrincipalData() | ||
{ | ||
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); | ||
var user = authState.User; | ||
if (user.Identity?.IsAuthenticated ?? false) | ||
{ | ||
claims = user.Claims; | ||
} | ||
} | ||
} | ||
<CurrentSession /> |
Oops, something went wrong.