Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
afedyanin committed Oct 18, 2023
1 parent 4bbc4e9 commit a3e93ed
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 27 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Keycloak for Blazor demo
- [OpenID Connect Client with .NET](https://curity.io/resources/learn/dotnet-openid-connect-website/)
- [C#/NetStandard OpenID Connect Client Library for native Applications](https://github.com/IdentityModel/IdentityModel.OidcClient)
- [Secure ASP.NET Core Blazor WebAssembly](https://learn.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/?view=aspnetcore-7.0)
- [OpenIDConnect Response Type Confusion](https://stackoverflow.com/questions/29275477/openidconnect-response-type-confusion)



## Keycloak setup

Expand Down
6 changes: 3 additions & 3 deletions src/BlazorApp/BlazorApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
</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.AspNetCore.Components.WebAssembly" Version="7.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="7.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.10" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/BlazorApp/Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static async Task Main(string[] args)
options.ProviderOptions.MetadataUrl = "https://localhost:8080/realms/myrealm/.well-known/openid-configuration";
options.ProviderOptions.Authority = "https://localhost:8080/realms/myrealm";
options.ProviderOptions.ClientId = "blazor-client";
options.ProviderOptions.ResponseType = "id_token token";
options.ProviderOptions.ResponseType = "code";
options.UserOptions.NameClaim = "preferred_username";
options.UserOptions.RoleClaim = "roles";
Expand Down
39 changes: 35 additions & 4 deletions src/BlazorApp/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
@page "/"
@page "/"

@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using System.Security.Claims

@inject AuthenticationStateProvider AuthenticationStateProvider

<PageTitle>Index</PageTitle>

<h1>Hello, world!</h1>
<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>();

Welcome to your new app.
protected override Task OnInitializedAsync() => GetClaimsPrincipalData();

<SurveyPrompt Title="How is Blazor working for you?" />
private async Task GetClaimsPrincipalData()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
if (user.Identity?.IsAuthenticated ?? false)
{
claims = user.Claims;
}
}
}
6 changes: 3 additions & 3 deletions src/BlazorApp/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
@inherits LayoutComponentBase
@inherits LayoutComponentBase

<div class="page">
<div class="sidebar">
<NavMenu />
</div>

<main>
<div class="top-row px-4">
<a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
<div class="top-row px-4 auth">
<LoginDisplay />
</div>

<article class="content px-4">
Expand Down
16 changes: 0 additions & 16 deletions src/BlazorApp/Shared/SurveyPrompt.razor

This file was deleted.

0 comments on commit a3e93ed

Please sign in to comment.