-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: init wasm demo * refactor: fix auth issue * chore: add README.md * chore: fix typos
- Loading branch information
Showing
15 changed files
with
455 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
@using sample_wasm.Pages | ||
|
||
<CascadingAuthenticationState> | ||
<Router AppAssembly="@typeof(App).Assembly"> | ||
<Found Context="routeData"> | ||
<Home /> | ||
<FocusOnNavigate RouteData="@routeData" Selector="h1" /> | ||
</Found> | ||
<NotFound> | ||
<PageTitle>Not found</PageTitle> | ||
<p role="alert">Sorry, there's nothing at this address.</p> | ||
</NotFound> | ||
</Router> | ||
</CascadingAuthenticationState> |
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,34 @@ | ||
@page "/" | ||
|
||
<section class="p-4 flex flex-col gap-2"> | ||
<h1 class="text-2xl font-bold">Logto Blazor WASM sample</h1> | ||
<p>This is the sample application for Logto integration with Blazor WASM.</p> | ||
<section class="space-y-2"> | ||
<AuthorizeView> | ||
<Authorized> | ||
<p class="text-emerald-700 text-s font-bold"> | ||
You are signed in as @(@User?.Profile?.Name ?? "(unknown name)"). | ||
</p> | ||
<h2 class="text-xl font-bold">Profile</h2> | ||
<ul class="list-disc list-inside"> | ||
<li>Email: @(@User?.Profile?.Email ?? "(null)")</li> | ||
<li>Email verified: @(@User?.Profile?.EmailVerified ?? false)</li> | ||
</ul> | ||
<p>Access token: @(@User?.AccessToken ?? "(null)")</p> | ||
<button class="bg-violet-700 hover:bg-violet-800 text-white px-4 py-2 rounded text-sm" | ||
@onclick="OnLogoutButtonClickAsync"> | ||
Sign out | ||
</button> | ||
</Authorized> | ||
<NotAuthorized> | ||
<p class="text-amber-600 text-s font-bold"> | ||
You are not signed in. | ||
</p> | ||
<button class="bg-violet-700 hover:bg-violet-800 text-white px-4 py-2 rounded text-sm" | ||
@onclick="OnLoginButtonClickAsync"> | ||
Sign in | ||
</button> | ||
</NotAuthorized> | ||
</AuthorizeView> | ||
</section> | ||
</section> |
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,56 @@ | ||
namespace sample_wasm.Pages; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.AspNetCore.Components.Web; | ||
using Blorc.OpenIdConnect; | ||
using Microsoft.AspNetCore.Components.Authorization; | ||
|
||
[Authorize] | ||
public partial class Home : ComponentBase | ||
{ | ||
[Inject] | ||
public required IUserManager UserManager { get; set; } | ||
public TimeSpan? SignOutTimeSpan { get; set; } | ||
|
||
public User<Profile>? User { get; set; } | ||
|
||
[CascadingParameter] | ||
protected Task<AuthenticationState>? AuthenticationStateTask { get; set; } | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
User = await UserManager.GetUserAsync<User<Profile>>(AuthenticationStateTask!); | ||
|
||
UserManager.UserActivity += OnUserManagerUserActivity; | ||
UserManager.UserInactivity += OnUserManagerUserInactivity; | ||
} | ||
|
||
private void OnUserManagerUserInactivity(object? sender, UserInactivityEventArgs args) | ||
{ | ||
SignOutTimeSpan = args.SignOutTimeSpan; | ||
StateHasChanged(); | ||
} | ||
|
||
private void OnUserManagerUserActivity(object? sender, UserActivityEventArgs args) | ||
{ | ||
SignOutTimeSpan = null; | ||
StateHasChanged(); | ||
} | ||
|
||
private async Task OnLoginButtonClickAsync(MouseEventArgs obj) | ||
{ | ||
await UserManager.SignInRedirectAsync(); | ||
} | ||
|
||
private async Task OnLogoutButtonClickAsync(MouseEventArgs obj) | ||
{ | ||
await UserManager.SignOutRedirectAsync(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
UserManager.UserActivity -= OnUserManagerUserActivity; | ||
UserManager.UserInactivity -= OnUserManagerUserInactivity; | ||
} | ||
} |
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,30 @@ | ||
using Microsoft.AspNetCore.Components.Web; | ||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting; | ||
using Blorc.OpenIdConnect; | ||
using Blorc.Services; | ||
using sample_wasm; | ||
|
||
var builder = WebAssemblyHostBuilder.CreateDefault(args); | ||
builder.RootComponents.Add<App>("#app"); | ||
builder.RootComponents.Add<HeadOutlet>("head::after"); | ||
|
||
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); | ||
|
||
builder.Services.AddBlorcCore(); | ||
builder.Services.AddAuthorizationCore(); | ||
builder.Services.AddBlorcOpenIdConnect( | ||
options => | ||
{ | ||
builder.Configuration.Bind("IdentityServer", options); | ||
}); | ||
|
||
var webAssemblyHost = builder.Build(); | ||
|
||
await webAssemblyHost | ||
.ConfigureDocumentAsync(async documentService => | ||
{ | ||
await documentService.InjectBlorcCoreJsAsync(); | ||
await documentService.InjectOpenIdConnectAsync(); | ||
}); | ||
|
||
await webAssemblyHost.RunAsync(); |
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,31 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:40255", | ||
"sslPort": 44360 | ||
} | ||
}, | ||
"profiles": { | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", | ||
"applicationUrl": "https://localhost:7119;http://localhost:5025", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
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,75 @@ | ||
# Logto ASP.NET Blazor WebAssembly sample project | ||
|
||
This sample project shows how to use the [Blorc.OpenIdConnect](https://github.com/WildGums/Blorc.OpenIdConnect) to authenticate users with Logto in a Blazor WebAssembly application. | ||
|
||
## Prerequisites | ||
|
||
- .NET 6.0 or higher | ||
- A [Logto Cloud](https://logto.io/) account or a self-hosted Logto | ||
- A Logto single-page application created | ||
|
||
### Optional | ||
|
||
- Set up an API resource in Logto | ||
|
||
If you don't have the Logto application created, please follow the [⚡ Get started](https://docs.logto.io/docs/tutorials/get-started/) guide to create one. | ||
|
||
## Configuration | ||
|
||
Create an `appsettings.Development.json` (or `appsettings.json`) with the following structure: | ||
|
||
```jsonc | ||
{ | ||
// ... | ||
"IdentityServer": { | ||
"Authority": "https://<your-logto-endpoint>/oidc", | ||
"ClientId": "<your-logto-app-id>", | ||
"PostLogoutRedirectUri": "<your-app-url>", // Remember to configure this in Logto | ||
"RedirectUri": "<your-app-url>", // Remember to configure this in Logto | ||
"ResponseType": "code", | ||
"Scope": "openid profile" // Add more scopes if needed | ||
} | ||
} | ||
``` | ||
|
||
### Fetch user info | ||
|
||
For some special claims, such as `custom_data`, calling the `/userinfo` endpoint is required. To enable this feature, add the following configuration: | ||
|
||
```jsonc | ||
{ | ||
// ... | ||
"IdentityServer": { | ||
// ... | ||
"LoadUserInfo": true | ||
} | ||
} | ||
``` | ||
|
||
> [!Caution] | ||
> Since WebAssembly is a client-side application, the token request will only be sent to the server-side once. Due to this nature, `LoadUserInfo` is conflict with fetching access token for API resources. | ||
### JWT access token | ||
|
||
If you need to fetch an access token in JWT format for an API resource, add the following configuration: | ||
|
||
```jsonc | ||
{ | ||
// ... | ||
"IdentityServer": { | ||
// ... | ||
"Resource": "https://<your-api-resource-indicator>", | ||
"ExtraTokenParams": { | ||
"resource": "https://<your-api-resource-indicator>" // Ensure the key is lowercase | ||
} | ||
} | ||
} | ||
``` | ||
|
||
The value of `Resource` and `ExtraTokenParams.resource` should be the same. | ||
|
||
## Run the sample | ||
|
||
```bash | ||
dotnet run # or `dotnet watch` to run in watch mode | ||
``` |
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 @@ | ||
@using System.Net.Http | ||
@using System.Net.Http.Json | ||
@using Microsoft.AspNetCore.Components.Authorization | ||
@using Microsoft.AspNetCore.Components.Forms | ||
@using Microsoft.AspNetCore.Components.Routing | ||
@using Microsoft.AspNetCore.Components.Web | ||
@using Microsoft.AspNetCore.Components.Web.Virtualization | ||
@using Microsoft.AspNetCore.Components.WebAssembly.Http | ||
@using Microsoft.JSInterop | ||
@using sample_wasm |
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,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Blorc.OpenIdConnect" Version="1.9.0-beta0001" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.1" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.1" PrivateAssets="all" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.