Skip to content

Commit

Permalink
Add .NET 8 w EF sample
Browse files Browse the repository at this point in the history
  • Loading branch information
josephdecock committed Nov 30, 2023
1 parent 6dded81 commit 76490fd
Show file tree
Hide file tree
Showing 14 changed files with 596 additions and 2 deletions.
25 changes: 23 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
}
},
{
"name": "JS6.DPoP",
"name": "JS6 DPoP",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-js6dpop",
Expand Down Expand Up @@ -240,7 +240,7 @@
}
},
{
"name": "JS8.DPoP",
"name": "JS8 DPoP",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-js8dpop",
Expand All @@ -260,5 +260,26 @@
"order": 10
}
},
{
"name": "JS8 EF",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-js8ef",
"program": "${workspaceFolder}/samples/JS8.EF/bin/Debug/net8.0/JS8.EF.dll",
"args": [],
"cwd": "${workspaceFolder}/samples/JS8.EF",
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
},
"console": "integratedTerminal",
"presentation": {
"hidden": false,
"order": 10
}
},
]
}
12 changes: 12 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@
],
"problemMatcher": "$msCompile"
},
{
"label": "build-js8ef",
"type": "process",
"command": "dotnet",
"args": [
"build",
"${workspaceFolder}/samples/JS8.EF/JS8.EF.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
]

}
19 changes: 19 additions & 0 deletions samples/JS8.EF/JS8.EF.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Duende.Bff.EntityFramework\Duende.Bff.EntityFramework.csproj" />
<ProjectReference Include="..\..\src\Duende.Bff.Yarp\Duende.Bff.Yarp.csproj" />
</ItemGroup>

</Project>
22 changes: 22 additions & 0 deletions samples/JS8.EF/LocalApiController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.

using Microsoft.AspNetCore.Mvc;

namespace Host8.EntityFramework
{
[Route("local")]
public class LocalApiController : ControllerBase
{
public IActionResult Get()
{
var data = new
{
Message = "Hello from local API",
User = User.FindFirst("name")?.Value ?? User.FindFirst("sub").Value
};

return Ok(data);
}
}
}
52 changes: 52 additions & 0 deletions samples/JS8.EF/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes;
using System;

namespace Host8.EntityFramework
{
public class Program
{
public static int Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
.MinimumLevel.Override("IdentityModel", LogEventLevel.Debug)
.MinimumLevel.Override("Duende.Bff", LogEventLevel.Debug)
.Enrich.FromLogContext()
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}", theme: AnsiConsoleTheme.Code)
.CreateLogger();

try
{
Log.Information("Starting host...");
CreateHostBuilder(args).Build().Run();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly.");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}

public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
}
13 changes: 13 additions & 0 deletions samples/JS8.EF/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"profiles": {
"Host8.EntityFramework": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"applicationUrl": "https://localhost:5002",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
120 changes: 120 additions & 0 deletions samples/JS8.EF/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using Duende.Bff;
using Duende.Bff.Yarp;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using Microsoft.AspNetCore.DataProtection;

namespace Host8.EntityFramework
{
public class Startup
{
private readonly IConfiguration _configuration;
private readonly IWebHostEnvironment _environment;

public Startup(IConfiguration configuration, IWebHostEnvironment environment)
{
_configuration = configuration;
_environment = environment;
}

public void ConfigureServices(IServiceCollection services)
{
services.AddDataProtection()
.SetApplicationName("JS-EF-Sample");

// Add BFF services to DI - also add server-side session management
var cn = _configuration.GetConnectionString("db");
services.AddBff(options =>
{
options.BackchannelLogoutAllUserSessions = true;
options.EnableSessionCleanup = true;
})
.AddRemoteApis()
.AddEntityFrameworkServerSideSessions(options=> {
//options.UseSqlServer(cn);
options.UseSqlite(cn);
});

// local APIs
services.AddControllers();

// cookie options
services.AddAuthentication(options =>
{
options.DefaultScheme = "cookie";
options.DefaultChallengeScheme = "oidc";
options.DefaultSignOutScheme = "oidc";
})
.AddCookie("cookie", options =>
{
// host prefixed cookie name
options.Cookie.Name = "__Host-spa-ef";
// strict SameSite handling
options.Cookie.SameSite = SameSiteMode.Strict;
})
.AddOpenIdConnect("oidc", options =>
{
options.Authority = "https://localhost:5001";
// confidential client using code flow + PKCE
options.ClientId = "spa";
options.ClientSecret = "secret";
options.ResponseType = "code";
options.ResponseMode = "query";
options.MapInboundClaims = false;
options.GetClaimsFromUserInfoEndpoint = true;
options.SaveTokens = true;
// request scopes + refresh tokens
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("api");
options.Scope.Add("offline_access");
});
}

public void Configure(IApplicationBuilder app)
{
app.UseSerilogRequestLogging();
app.UseDeveloperExceptionPage();

app.UseDefaultFiles();
app.UseStaticFiles();

app.UseAuthentication();
app.UseRouting();

// adds antiforgery protection for local APIs
app.UseBff();

// adds authorization for local and remote API endpoints
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
// local APIs
endpoints.MapControllers()
.RequireAuthorization()
.AsBffApiEndpoint();
// login, logout, user, backchannel logout...
endpoints.MapBffManagementEndpoints();
// proxy endpoint for cross-site APIs
// all calls to /api/* will be forwarded to the remote API
// user or client access token will be attached in API call
// user access token will be managed automatically using the refresh token
endpoints.MapRemoteBffApiEndpoint("/api", "https://localhost:5010")
.RequireAccessToken(TokenType.UserOrClient);
});
}
}
}
6 changes: 6 additions & 0 deletions samples/JS8.EF/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ConnectionStrings": {
//"db": "server=(localdb)\\mssqllocaldb;database=Duende.BFF;trusted_connection=yes;"
"db": "Data Source=Duende.BFF.db;"
}
}
3 changes: 3 additions & 0 deletions samples/JS8.EF/wwwroot/StyleSheet.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pre:empty {
display: none;
}
Loading

0 comments on commit 76490fd

Please sign in to comment.