diff --git a/IssueVerifiableEmployee/Program.cs b/IssueVerifiableEmployee/Program.cs index 2dcbe9e..ba515e9 100644 --- a/IssueVerifiableEmployee/Program.cs +++ b/IssueVerifiableEmployee/Program.cs @@ -1,18 +1,87 @@ -namespace IssuerVerifiableEmployee; +using BffMicrosoftEntraID.Server; +using IssuerVerifiableEmployee; +using IssuerVerifiableEmployee.Services.GraphServices; +using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Authentication.OpenIdConnect; +using Microsoft.AspNetCore.Server.Kestrel.Core; +using Microsoft.Identity.Web; +using Microsoft.Identity.Web.UI; -public class Program +var builder = WebApplication.CreateBuilder(args); + +builder.WebHost.ConfigureKestrel(serverOptions => +{ + serverOptions.AddServerHeader = false; +}); + +var services = builder.Services; +var configuration = builder.Configuration; +var env = builder.Environment; + +services.Configure(options => { - public static void Main(string[] args) - { - CreateHostBuilder(args).Build().Run(); - } - - public static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder - .ConfigureKestrel(options => options.AddServerHeader = false) - .UseStartup(); - }); + options.AllowSynchronousIO = true; +}); + +services.Configure(configuration.GetSection("CredentialSettings")); +services.AddScoped(); +services.AddScoped(); + +services.AddDistributedMemoryCache(); + +var scopes = new string[] { "user.read" }; +services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) + .AddMicrosoftIdentityWebApp(configuration.GetSection("AzureAd")) + .EnableTokenAcquisitionToCallDownstreamApi(scopes) + .AddMicrosoftGraph() + .AddDistributedTokenCaches(); + +// If using downstream APIs and in memory cache, you need to reset the cookie session if the cache is missing +// If you use persistent cache, you do not require this. +// You can also return the 403 with the required scopes, this needs special handling for ajax calls +// The check is only for single scopes +services.Configure(CookieAuthenticationDefaults.AuthenticationScheme, + options => options.Events = new RejectSessionCookieWhenAccountNotInCacheEvents(scopes)); + +services.AddAuthorization(options => +{ + options.FallbackPolicy = options.DefaultPolicy; +}); + +services.Configure(options => +{ + options.CheckConsentNeeded = context => false; + options.MinimumSameSitePolicy = SameSiteMode.None; +}); + +services.AddRazorPages() + .AddMvcOptions(options => { }) + .AddMicrosoftIdentityUI(); + +var app = builder.Build(); + +app.UseSecurityHeaders(SecurityHeadersDefinitions + .GetHeaderPolicyCollection(env.IsDevelopment())); + + +if (env.IsDevelopment()) +{ + app.UseDeveloperExceptionPage(); +} +else +{ + app.UseExceptionHandler("/Error"); } + +app.UseHttpsRedirection(); +app.UseStaticFiles(); + +app.UseRouting(); + +app.UseAuthentication(); +app.UseAuthorization(); + +app.MapRazorPages(); +app.MapControllers(); + +app.Run(); diff --git a/IssueVerifiableEmployee/Properties/launchSettings.json b/IssueVerifiableEmployee/Properties/launchSettings.json index b46863f..c5e83f2 100644 --- a/IssueVerifiableEmployee/Properties/launchSettings.json +++ b/IssueVerifiableEmployee/Properties/launchSettings.json @@ -12,7 +12,7 @@ "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, - "applicationUrl": "https://localhost:5001;http://localhost:5000", + "applicationUrl": "https://localhost:5001", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/IssueVerifiableEmployee/Startup.cs b/IssueVerifiableEmployee/Startup.cs deleted file mode 100644 index adf2c6e..0000000 --- a/IssueVerifiableEmployee/Startup.cs +++ /dev/null @@ -1,92 +0,0 @@ -using BffMicrosoftEntraID.Server; -using IssuerVerifiableEmployee.Services.GraphServices; -using Microsoft.AspNetCore.Authentication.Cookies; -using Microsoft.AspNetCore.Authentication.OpenIdConnect; -using Microsoft.AspNetCore.Server.Kestrel.Core; -using Microsoft.Identity.Web; -using Microsoft.Identity.Web.UI; - -namespace IssuerVerifiableEmployee; - -public class Startup -{ - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } - - public IConfiguration Configuration { get; } - - public void ConfigureServices(IServiceCollection services) - { - services.Configure(options => - { - options.AllowSynchronousIO = true; - }); - - services.Configure(Configuration.GetSection("CredentialSettings")); - services.AddScoped(); - services.AddScoped(); - - services.AddDistributedMemoryCache(); - - var scopes = new string[] { "user.read" }; - services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) - .AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd")) - .EnableTokenAcquisitionToCallDownstreamApi(scopes) - .AddMicrosoftGraph() - .AddDistributedTokenCaches(); - - // If using downstream APIs and in memory cache, you need to reset the cookie session if the cache is missing - // If you use persistent cache, you do not require this. - // You can also return the 403 with the required scopes, this needs special handling for ajax calls - // The check is only for single scopes - services.Configure(CookieAuthenticationDefaults.AuthenticationScheme, - options => options.Events = new RejectSessionCookieWhenAccountNotInCacheEvents(scopes)); - - services.AddAuthorization(options => - { - options.FallbackPolicy = options.DefaultPolicy; - }); - - services.Configure(options => - { - options.CheckConsentNeeded = context => false; - options.MinimumSameSitePolicy = SameSiteMode.None; - }); - - services.AddRazorPages() - .AddMvcOptions(options => { }) - .AddMicrosoftIdentityUI(); - } - - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) - { - app.UseSecurityHeaders(SecurityHeadersDefinitions - .GetHeaderPolicyCollection(env.IsDevelopment())); - - - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - else - { - app.UseExceptionHandler("/Error"); - } - - app.UseHttpsRedirection(); - app.UseStaticFiles(); - - app.UseRouting(); - - app.UseAuthentication(); - app.UseAuthorization(); - - app.UseEndpoints(endpoints => - { - endpoints.MapRazorPages(); - endpoints.MapControllers(); - }); - } -}