Created
May 6, 2024 17:31
-
-
Save alpersilistre/090b5ac6e58d54e0fd189dd3e4018806 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using Microsoft.AspNetCore.Authorization; | |
using Microsoft.AspNetCore.Mvc.Authorization; | |
using Microsoft.Identity.Web; | |
using Microsoft.Identity.Web.UI; | |
var builder = WebApplication.CreateBuilder(args); | |
IEnumerable<string>? initialScopes = builder.Configuration["DownstreamApi:Scopes"]?.Split(' '); | |
builder.Services.AddMicrosoftIdentityWebAppAuthentication(builder.Configuration, "AzureAd") | |
.EnableTokenAcquisitionToCallDownstreamApi(initialScopes) | |
.AddDownstreamApi("DownstreamApi", builder.Configuration.GetSection("DownstreamApi")) | |
.AddInMemoryTokenCaches(); | |
// Add services to the container. | |
builder.Services.AddRazorPages().AddMvcOptions(options => | |
{ | |
var policy = new AuthorizationPolicyBuilder() | |
.RequireAuthenticatedUser().Build(); | |
options.Filters.Add(new AuthorizeFilter(policy)); | |
}).AddMicrosoftIdentityUI(); | |
var port = Environment.GetEnvironmentVariable("PORT") ?? "8080"; | |
var url = $"https://0.0.0.0:{port}"; | |
var app = builder.Build(); | |
// Configure the HTTP request pipeline. | |
if (!app.Environment.IsDevelopment()) | |
{ | |
app.UseExceptionHandler("/Home/Error"); | |
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. | |
app.UseHsts(); | |
} | |
app.UseHttpsRedirection(); | |
app.UseStaticFiles(); | |
app.UseRouting(); | |
app.UseAuthentication(); | |
app.UseAuthorization(); | |
app.MapRazorPages(); | |
app.MapControllers(); | |
app.MapControllerRoute( | |
name: "default", | |
pattern: "{controller=Home}/{action=Index}/{id?}"); | |
app.Run(url); | |
// app.Run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment