Registers Logto authentication services in the DI container for connecting to a Logto instance using OpenID Connect or JWT Bearer authentication.
- A Logto instance (can be hosted via
CommunityToolkit.Aspire.Hosting.Logto).
Install the Aspire Logto Client library with NuGet:
dotnet add package CommunityToolkit.Aspire.Logto.Client
In the Program.cs file of your project, call the AddLogtoOIDC extension method to register Logto OIDC authentication for use via the dependency injection container. The method takes an optional connection name parameter.
builder.AddLogtoOIDC("logto");For API projects that need to validate Logto-issued JWTs, use the AddLogtoJwtBearer extension method on the AuthenticationBuilder:
builder.Services
.AddAuthentication()
.AddLogtoJwtBearer("logto", "your-app-identifier");The Aspire Logto Client integration provides multiple options to configure the server connection based on the requirements and conventions of your project.
When using a connection string from the ConnectionStrings configuration section, you can provide the name of the connection string when calling builder.AddLogtoOIDC():
builder.AddLogtoOIDC("logto");And then the connection string will be retrieved from the ConnectionStrings configuration section:
{
"ConnectionStrings": {
"logto": "Endpoint=http://localhost:3001"
}
}The Aspire Logto Client integration supports Microsoft.Extensions.Configuration. It loads the settings from configuration by using the Aspire:Logto:Client key. Example appsettings.json that configures some of the options:
{
"Aspire": {
"Logto": {
"Client": {
"Endpoint": "http://localhost:3001",
"AppId": "your-app-id",
"AppSecret": "your-app-secret"
}
}
}
}You can also pass the Action<LogtoOptions> delegate to set up some or all the options inline, for example to set the AppId from code:
builder.AddLogtoOIDC("logto", logtoOptions: options =>
{
options.AppId = "your-app-id";
options.AppSecret = "your-app-secret";
});In your AppHost project, install the CommunityToolkit.Aspire.Hosting.Logto library with NuGet:
dotnet add package CommunityToolkit.Aspire.Hosting.Logto
Then, in the Program.cs file of AppHost, register a Logto instance and consume the connection using the following methods:
var postgres = builder.AddPostgres("postgres");
var logto = builder.AddLogto("logto", postgres);
var myService = builder.AddProject<Projects.MyService>()
.WithReference(logto);The WithReference method configures a connection in the MyService project named logto. In the Program.cs file of MyService, the Logto connection can be consumed using:
builder.AddLogtoOIDC("logto");