|
| 1 | +using Microsoft.AspNetCore.Builder; |
| 2 | +using Microsoft.AspNetCore.Hosting; |
| 3 | +using Microsoft.AspNetCore.Http; |
| 4 | +using Microsoft.Extensions.Configuration; |
| 5 | +using Microsoft.Extensions.DependencyInjection; |
| 6 | +using Microsoft.Extensions.Hosting; |
| 7 | +using System.IdentityModel.Tokens.Jwt; |
| 8 | + |
| 9 | +namespace BffJs |
| 10 | +{ |
| 11 | + public class Startup |
| 12 | + { |
| 13 | + private readonly IWebHostEnvironment _environment; |
| 14 | + |
| 15 | + public Startup(IConfiguration configuration, IWebHostEnvironment environment) |
| 16 | + { |
| 17 | + _environment = environment; |
| 18 | + |
| 19 | + JwtSecurityTokenHandler.DefaultMapInboundClaims = false; |
| 20 | + } |
| 21 | + |
| 22 | + public void ConfigureServices(IServiceCollection services) |
| 23 | + { |
| 24 | + services.AddControllers(); |
| 25 | + |
| 26 | + // add BFF services and server-side session management |
| 27 | + services.AddBff() |
| 28 | + .AddServerSideSessions(); |
| 29 | + |
| 30 | + services.AddAuthentication(options => |
| 31 | + { |
| 32 | + options.DefaultScheme = "cookie"; |
| 33 | + options.DefaultChallengeScheme = "oidc"; |
| 34 | + options.DefaultSignOutScheme = "oidc"; |
| 35 | + }) |
| 36 | + .AddCookie("cookie", options => |
| 37 | + { |
| 38 | + options.Cookie.Name = "__Host-bff"; |
| 39 | + options.Cookie.SameSite = SameSiteMode.Strict; |
| 40 | + }) |
| 41 | + .AddOpenIdConnect("oidc", options => |
| 42 | + { |
| 43 | + options.Authority = "https://demo.duendesoftware.com"; |
| 44 | + options.ClientId = "interactive.confidential"; |
| 45 | + options.ClientSecret = "secret"; |
| 46 | + options.ResponseType = "code"; |
| 47 | + options.ResponseMode = "query"; |
| 48 | + |
| 49 | + options.GetClaimsFromUserInfoEndpoint = true; |
| 50 | + options.SaveTokens = true; |
| 51 | + |
| 52 | + options.Scope.Clear(); |
| 53 | + options.Scope.Add("openid"); |
| 54 | + options.Scope.Add("profile"); |
| 55 | + options.Scope.Add("api"); |
| 56 | + options.Scope.Add("offline_access"); |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + public void Configure(IApplicationBuilder app) |
| 61 | + { |
| 62 | + if (_environment.IsDevelopment()) |
| 63 | + { |
| 64 | + app.UseDeveloperExceptionPage(); |
| 65 | + } |
| 66 | + |
| 67 | + app.UseDefaultFiles(); |
| 68 | + app.UseStaticFiles(); |
| 69 | + |
| 70 | + app.UseAuthentication(); |
| 71 | + app.UseRouting(); |
| 72 | + |
| 73 | + // add CSRF protection and status code handling for API endpoints |
| 74 | + app.UseBff(); |
| 75 | + app.UseAuthorization(); |
| 76 | + |
| 77 | + app.UseEndpoints(endpoints => |
| 78 | + { |
| 79 | + endpoints.MapControllers() |
| 80 | + .RequireAuthorization() |
| 81 | + .AsLocalBffApiEndpoint(); |
| 82 | + |
| 83 | + endpoints.MapBffManagementEndpoints("/bff"); |
| 84 | + |
| 85 | + // enable proxying to remote API |
| 86 | + //endpoints.MapRemoteBffApiEndpoint("/api", "https://api.mycompany.com") |
| 87 | + // .RequireAccessToken(); |
| 88 | + }); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments