-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6dded81
commit 76490fd
Showing
14 changed files
with
596 additions
and
2 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,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> |
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,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); | ||
} | ||
} | ||
} |
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,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>(); | ||
}); | ||
} | ||
} | ||
} |
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,13 @@ | ||
{ | ||
"profiles": { | ||
"Host8.EntityFramework": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": "true", | ||
"launchBrowser": true, | ||
"applicationUrl": "https://localhost:5002", | ||
"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,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); | ||
}); | ||
} | ||
} | ||
} |
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,6 @@ | ||
{ | ||
"ConnectionStrings": { | ||
//"db": "server=(localdb)\\mssqllocaldb;database=Duende.BFF;trusted_connection=yes;" | ||
"db": "Data Source=Duende.BFF.db;" | ||
} | ||
} |
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,3 @@ | ||
pre:empty { | ||
display: none; | ||
} |
Oops, something went wrong.