Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions IdentityServer/v7/SessionManagement/Api/Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Apire.ServiceDefaults\Aspire.ServiceDefaults.csproj" />
<ProjectReference Include="..\SessionManagement.ServiceDefaults\SessionManagement.ServiceDefaults.csproj" />
<ProjectReference Include="..\..\Aspire.ServiceDefaults\Aspire.ServiceDefaults.csproj" />
</ItemGroup>

</Project>
3 changes: 1 addition & 2 deletions IdentityServer/v7/SessionManagement/Client/Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@


<ItemGroup>
<ProjectReference Include="..\..\Apire.ServiceDefaults\Aspire.ServiceDefaults.csproj" />
<ProjectReference Include="..\SessionManagement.ServiceDefaults\SessionManagement.ServiceDefaults.csproj" />
<ProjectReference Include="..\..\Aspire.ServiceDefaults\Aspire.ServiceDefaults.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Apire.ServiceDefaults\Aspire.ServiceDefaults.csproj" />
<ProjectReference Include="..\SessionManagement.ServiceDefaults\SessionManagement.ServiceDefaults.csproj" />
<ProjectReference Include="..\..\Aspire.ServiceDefaults\Aspire.ServiceDefaults.csproj" />
</ItemGroup>

</Project>
10 changes: 5 additions & 5 deletions IdentityServer/v7/SessionManagement/SessionManagement.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IdentityServerHost", "Ident
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SessionManagement.AppHost", "SessionManagement.AppHost\SessionManagement.AppHost.csproj", "{BD383672-7EF5-4737-91A9-92918CF2BBA7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.ServiceDefaults", "..\Apire.ServiceDefaults\Aspire.ServiceDefaults.csproj", "{A614B4B7-AB24-421D-808F-C35D4139F8E6}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.ServiceDefaults", "..\Aspire.ServiceDefaults\Aspire.ServiceDefaults.csproj", "{BE3015AC-8269-4D94-AB27-E835CBFC3AFC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -35,10 +35,10 @@ Global
{BD383672-7EF5-4737-91A9-92918CF2BBA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BD383672-7EF5-4737-91A9-92918CF2BBA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BD383672-7EF5-4737-91A9-92918CF2BBA7}.Release|Any CPU.Build.0 = Release|Any CPU
{A614B4B7-AB24-421D-808F-C35D4139F8E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A614B4B7-AB24-421D-808F-C35D4139F8E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A614B4B7-AB24-421D-808F-C35D4139F8E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A614B4B7-AB24-421D-808F-C35D4139F8E6}.Release|Any CPU.Build.0 = Release|Any CPU
{BE3015AC-8269-4D94-AB27-E835CBFC3AFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BE3015AC-8269-4D94-AB27-E835CBFC3AFC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BE3015AC-8269-4D94-AB27-E835CBFC3AFC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BE3015AC-8269-4D94-AB27-E835CBFC3AFC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Folder name is "Apire" (no s), can you fix that?


<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireSharedProject>true</IsAspireSharedProject>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App"/>

<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="10.1.0"/>
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="10.1.0"/>
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.14.0"/>
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.14.0"/>
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.14.0"/>
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.14.0"/>
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.14.0"/>
</ItemGroup>

</Project>
129 changes: 129 additions & 0 deletions IdentityServer/v8/Aspire.ServiceDefaults/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.ServiceDiscovery;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;

namespace Microsoft.Extensions.Hosting;

// Adds common Aspire services: service discovery, resilience, health checks, and OpenTelemetry.
// This project should be referenced by each service project in your solution.
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
public static class Extensions
{
private const string HealthEndpointPath = "/health";
private const string AlivenessEndpointPath = "/alive";

public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
builder.ConfigureOpenTelemetry();

builder.AddDefaultHealthChecks();

builder.Services.AddServiceDiscovery();

builder.Services.ConfigureHttpClientDefaults(http =>
{
// Turn on resilience by default
http.AddStandardResilienceHandler();

// Turn on service discovery by default
http.AddServiceDiscovery();
});

// Uncomment the following to restrict the allowed schemes for service discovery.
// builder.Services.Configure<ServiceDiscoveryOptions>(options =>
// {
// options.AllowedSchemes = ["https"];
// });

return builder;
}

public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
builder.Logging.AddOpenTelemetry(logging =>
{
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
});

builder.Services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
metrics.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddRuntimeInstrumentation()
.AddMeter("Duende.IdentityServer", "Duende.IdentityServer.Expirimental", "IdentityServer");
})
.WithTracing(tracing =>
{
tracing.AddSource(builder.Environment.ApplicationName)
.AddAspNetCoreInstrumentation(tracing =>
// Exclude health check requests from tracing
tracing.Filter = context =>
!context.Request.Path.StartsWithSegments(HealthEndpointPath)
&& !context.Request.Path.StartsWithSegments(AlivenessEndpointPath)
)
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
//.AddGrpcClientInstrumentation()
.AddHttpClientInstrumentation()
.AddSource("Duende.IdentityServer");
});

builder.AddOpenTelemetryExporters();

return builder;
}

private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);

if (useOtlpExporter)
{
builder.Services.AddOpenTelemetry().UseOtlpExporter();
}

// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
//if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
//{
// builder.Services.AddOpenTelemetry()
// .UseAzureMonitor();
//}

return builder;
}

public static TBuilder AddDefaultHealthChecks<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
builder.Services.AddHealthChecks()
// Add a default liveness check to ensure app is responsive
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);

return builder;
}

public static WebApplication MapDefaultEndpoints(this WebApplication app)
{
// Adding health checks endpoints to applications in non-development environments has security implications.
// See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments.
if (app.Environment.IsDevelopment())
{
// All health checks must pass for app to be considered ready to accept traffic after starting
app.MapHealthChecks(HealthEndpointPath);

// Only health checks tagged with the "live" tag must pass for app to be considered alive
app.MapHealthChecks(AlivenessEndpointPath, new HealthCheckOptions
{
Predicate = r => r.Tags.Contains("live")
});
}

return app;
}
}
4 changes: 4 additions & 0 deletions IdentityServer/v8/SessionManagement/Api/Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Aspire.ServiceDefaults\Aspire.ServiceDefaults.csproj" />
</ItemGroup>

</Project>
4 changes: 4 additions & 0 deletions IdentityServer/v8/SessionManagement/Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
.CreateLogger();

var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();
builder.Services.AddSerilog();

builder.Services.AddControllers();
Expand All @@ -30,6 +32,8 @@

var app = builder.Build();

app.MapDefaultEndpoints();

app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
Expand Down
5 changes: 5 additions & 0 deletions IdentityServer/v8/SessionManagement/Client/Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="10.0.5" />
</ItemGroup>


<ItemGroup>
<ProjectReference Include="..\..\Aspire.ServiceDefaults\Aspire.ServiceDefaults.csproj" />
</ItemGroup>

</Project>
4 changes: 4 additions & 0 deletions IdentityServer/v8/SessionManagement/Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();

builder.Services.AddControllersWithViews();
builder.Services.AddHttpClient();

Expand Down Expand Up @@ -62,6 +64,8 @@

var app = builder.Build();

app.MapDefaultEndpoints();

app.UseDeveloperExceptionPage();
app.UseStaticFiles();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

<ItemGroup>
<PackageReference Include="Duende.IdentityServer" Version="8.0.0-alpha.1" />
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Aspire.ServiceDefaults\Aspire.ServiceDefaults.csproj" />
</ItemGroup>

</Project>
18 changes: 4 additions & 14 deletions IdentityServer/v8/SessionManagement/IdentityServerHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,17 @@
using Duende.IdentityServer;
using IdentityServerHost;
using Microsoft.AspNetCore.DataProtection;
using Serilog;
using Serilog.Sinks.SystemConsole.Themes;

Console.Title = "IdentityServer";

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.Enrich.FromLogContext()
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}", theme: AnsiConsoleTheme.Code)
.CreateLogger();

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSerilog();

builder.AddServiceDefaults();

builder.Services.AddRazorPages();

var idsvrBuilder = builder.Services.AddIdentityServer(options =>
{
options.Events.RaiseErrorEvents = true;
options.Events.RaiseInformationEvents = true;
options.Events.RaiseFailureEvents = true;
options.Events.RaiseSuccessEvents = true;

// see https://docs.duendesoftware.com/identityserver/fundamentals/resources/
options.EmitStaticAudienceClaim = true;

Expand Down Expand Up @@ -68,6 +56,8 @@

var app = builder.Build();

app.MapDefaultEndpoints();

if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
Expand Down
23 changes: 11 additions & 12 deletions IdentityServer/v8/SessionManagement/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# Session Management Sample

This sample requires all three projects to be run at once.

Things of note:
* In the *IdentityServer* project in *Startup.cs*, server-side sessions are enabled with a call to *AddServerSideSessions*. This only uses in-memory server-side sessions by default, so restarting the host will lose session data.
* Also in *Startup.cs* with the call to *AddIdentityServer* various settings are configured on the *ServerSideSessions* options object to control the behavior.
* The client application configured in *Clients.cs* has *CoordinateLifetimeWithUserSession* enabled, which causes its refresh token to slide the server-side session for the user.
* When launching the *IdentityServer* project, you should visit the *~/serversidesessions* page to see the active sessions. Note that there is no authorization on this page (so consider adding it based on your requirements).
* Once you login, you should see a user's session in the list.
* As the client app refreshes its access token, you should see the user's session expiration being extended.
* When you revoke the user's session, the user should be logged out of the client app.
**Session Management Sample**
This sample requires all three projects to be run at once. That can easily be done by running the included Aspire AppHost. The Aspire dashboard will show the status of all running applications and show you the links to the running applications. Aspire will also collect the Open Telemetry data (logs, metrics, traces) and make it available on the dashboard.
 Please note that Aspire’s service discovery isn’t used here so that projects can still run without Aspire, but it can easily be [added](https://aspire.dev/fundamentals/service-discovery/ "https://aspire.dev/fundamentals/service-discovery/") if needed.
Things of note:
- In the *IdentityServer* project in *Startup.cs*, server-side sessions are enabled with a call to *AddServerSideSessions*. This only uses in-memory server-side sessions by default, so restarting the host will lose session data.
- Also in *Startup.cs* with the call to *AddIdentityServer* various settings are configured on the *ServerSideSessions* options object to control the behavior.
- The client application configured in *Clients.cs* has *CoordinateLifetimeWithUserSession* enabled, which causes its refresh token to slide the server-side session for the user.
- When launching the *IdentityServer* project, you should visit the *~/serversidesessions* page to see the active sessions. Note that there is no authorization on this page (so consider adding it based on your requirements).
- Once you login, you should see a user's session in the list.
- As the client app refreshes its access token, you should see the user's session expiration being extended.
- When you revoke the user's session, the user should be logged out of the client app.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var builder = DistributedApplication.CreateBuilder(args);

builder.AddProject<Projects.Api>("api");

builder.AddProject<Projects.Client>("client");

builder.AddProject<Projects.IdentityServerHost>("identityserverhost");

builder.Build().Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:17003;http://localhost:15062",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21168",
"ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "https://localhost:23095",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22227"
}
},
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:15062",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19169",
"ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "http://localhost:18285",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20101"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Aspire.AppHost.Sdk/13.2.1">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UserSecretsId>1a0b33b6-a03f-480a-8b74-ac0b85c0cd37</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Api\Api.csproj" />
<ProjectReference Include="..\Client\Client.csproj" />
<ProjectReference Include="..\IdentityServerHost\IdentityServerHost.csproj" />
</ItemGroup>

</Project>
Loading
Loading