Skip to content

Commit

Permalink
feat: add access token sample
Browse files Browse the repository at this point in the history
  • Loading branch information
gao-sun committed Feb 3, 2024
1 parent 0334bc3 commit dca651f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
35 changes: 34 additions & 1 deletion sample-blazor/Components/Pages/Home.razor
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
@page "/"
@using Microsoft.AspNetCore.Components.Authorization
@using System.Security.Claims
@using Logto.AspNetCore.Authentication
@using Microsoft.AspNetCore.Authentication
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject NavigationManager NavigationManager
@inject IHttpContextAccessor HttpContextAccessor
@rendermode InteractiveServer

<PageTitle>Logto Blazor Server sample</PageTitle>
Expand All @@ -23,10 +26,19 @@
List<System.Security.Claims.Claim>()))
{
<li>
@claim.Type: @claim.Value
<b>@claim.Type:</b> @claim.Value
</li>
}
</ul>
<h2 class="text-xl font-bold">Tokens</h2>
<ul>
<li>
<b>Resource:</b> @(Resource ?? "(null)")
</li>
<li>
<b>Access token:</b> @(AccessToken ?? "(null)")
</li>
</ul>
<button class="bg-violet-700 hover:bg-violet-800 text-white px-4 py-2 rounded text-sm"
@onclick="SignOut">
Sign out
Expand All @@ -48,11 +60,18 @@

@code {
private ClaimsPrincipal? User { get; set; }
private string? AccessToken { get; set; }
private string? Resource { get; set; }

protected override async Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
User = authState.User;

if (User?.Identity?.IsAuthenticated ?? false)
{
await LoadTokens();
}
}

private void SignIn()
Expand All @@ -64,4 +83,18 @@
{
NavigationManager.NavigateTo("/SignOut", forceLoad: true);
}

private async Task LoadTokens()
{
var httpContext = HttpContextAccessor.HttpContext;
if (httpContext == null)
{
return;
}

var logtoOptions = httpContext.GetLogtoOptions();
Resource = logtoOptions?.Resource;
// Replace with other token types if needed
AccessToken = await httpContext.GetTokenAsync(LogtoParameters.Tokens.AccessTokenForResource);
}
}
1 change: 1 addition & 0 deletions sample-blazor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddHttpContextAccessor();
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
Expand Down
48 changes: 48 additions & 0 deletions sample-blazor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Logto ASP.NET Core sample project for MVC

This sample project shows how to use the [Logto ASP.NET Core authentication middleware](../src/Logto.AspNetCore.Authentication/) to authenticate users with Logto in a Blazor Server application.

## Prerequisites

- .NET 8.0 or higher (This sample is created with `dotnet new blazor` in .NET 8.0)
- A [Logto Cloud](https://logto.io/) account or a self-hosted Logto
- A Logto traditional web 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
{
// ...
"Logto": {
"Endpoint": "https://<your-logto-endpoint>/",
"AppId": "<your-logto-app-id>",
"AppSecret": "<your-logto-app-secret>"
}
}
```

If you need to test API resource, add the `Resource` key:

```jsonc
{
// ...
"Logto": {
// ...
"Resource": "https://<your-api-resource-indicator>"
}
}
```

## Run the sample

```bash
dotnet run # or `dotnet watch` to run in watch mode
```

0 comments on commit dca651f

Please sign in to comment.