-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
3 changed files
with
85 additions
and
108 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<KestrelServerOptions>(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<Startup>(); | ||
}); | ||
options.AllowSynchronousIO = true; | ||
}); | ||
|
||
services.Configure<CredentialSettings>(configuration.GetSection("CredentialSettings")); | ||
services.AddScoped<MicrosoftGraphDelegatedClient>(); | ||
services.AddScoped<IssuerService>(); | ||
|
||
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<CookieAuthenticationOptions>(CookieAuthenticationDefaults.AuthenticationScheme, | ||
options => options.Events = new RejectSessionCookieWhenAccountNotInCacheEvents(scopes)); | ||
|
||
services.AddAuthorization(options => | ||
{ | ||
options.FallbackPolicy = options.DefaultPolicy; | ||
}); | ||
|
||
services.Configure<CookiePolicyOptions>(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(); |
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 was deleted.
Oops, something went wrong.