Skip to content

Commit

Permalink
Update applications
Browse files Browse the repository at this point in the history
  • Loading branch information
damienbod committed Aug 27, 2023
1 parent c24f66b commit ac013de
Show file tree
Hide file tree
Showing 13 changed files with 307 additions and 301 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@
/ClientCredentialsFlows/LogsServiceApi.txt
/AngularAzureADMultipleApis/_logs-ApiWithMutlipleApis.txt
/AngularAzureADMultipleApis/_logs-ServiceApi.txt
/ConfidentialClientCredentialsCertificate/_logs-MyServerRenderedPortal.txt
/ConfidentialClientCredentialsCertificate/_logs-ServiceApi.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.UI;
using Microsoft.IdentityModel.Logging;
using Serilog;
using System.IdentityModel.Tokens.Jwt;

namespace MyServerRenderedPortal;

internal static class HostingExtensions
{
public static WebApplication ConfigureServices(this WebApplicationBuilder builder)
{
var services = builder.Services;
var configuration = builder.Configuration;

services.AddTransient<ConfidentialClientApiService>();
services.AddTransient<ClientAssertionsApiService>();
services.AddHttpClient();
services.AddOptions();

services.AddMicrosoftIdentityWebAppAuthentication(configuration);

services.AddRazorPages().AddMvcOptions(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
}).AddMicrosoftIdentityUI();
return builder.Build();
}

public static WebApplication ConfigurePipeline(this WebApplication app)
{
IdentityModelEventSource.ShowPII = true;
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

app.UseSerilogRequestLogging();

if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseSerilogRequestLogging();

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapRazorPages();
app.MapControllers();

return app;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@

<ItemGroup>
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.2.2" />
<PackageReference Include="Azure.Identity" Version="1.8.2" />
<PackageReference Include="Microsoft.Identity.Web" Version="2.9.0" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="2.9.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.6" />
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Azure.Identity" Version="1.10.0" />

<PackageReference Include="Microsoft.Identity.Web" Version="2.13.3" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="2.13.3" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.9" />

<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.AzureApp" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,48 +1,40 @@
using MyServerRenderedPortal;

using ServiceApi;
using Azure.Identity;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using System;

namespace MyServerRenderedPortal;
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.AzureApp()
.CreateBootstrapLogger();

public class Program
try
{
public static int Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateLogger();
Log.Information("Starting WebApi");

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseSerilog((context, loggerConfiguration) => loggerConfiguration
.ReadFrom.Configuration(context.Configuration));

try
{
Log.Information("Starting web host");
CreateHostBuilder(args).Build().Run();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}
var app = builder
.ConfigureServices()
.ConfigurePipeline();

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
.ReadFrom.Configuration(hostingContext.Configuration)
.Enrich.FromLogContext()
.MinimumLevel.Verbose()
.WriteTo.File("../LogsMyServerRenderedPortal.txt")
.WriteTo.Console(theme: AnsiConsoleTheme.Code)
)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
app.Run();
}
catch (Exception ex) when (ex.GetType().Name is not "StopTheHostException"
&& ex.GetType().Name is not "HostAbortedException")
{
Log.Fatal(ex, "Unhandled exception");
}
finally
{
Log.Information("Shut down complete");
Log.CloseAndFlush();
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:60192",
"sslPort": 44377
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"AZURE_CLIENT_ID": "64ecb044-417b-4892-83d4-5c03e8c977b9",
"ASPNETCORE_ENVIRONMENT": "Development",
"AZURE_TENANT_ID": "7ff95b15-dc21-4ba6-bc92-824856578fc1"
}
},
"MyServerRenderedPortal": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001"
"applicationUrl": "https://localhost:44377"
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,36 @@
}
]
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
"Serilog": {
"Using": [ "Serilog.Sinks.Console" ],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Debug",
"Microsoft.EntityFrameworkCore": "Warning",
"System": "Debug"
}
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
"WriteTo": [
{
"Name": "Console",
"Args": {
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} ({SourceContext}){NewLine}{Exception}"
}
},
{
"Name": "File",
"Args": {
"path": "../_logs-MyServerRenderedPortal.txt",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] [{EventId}] {Message}{NewLine}{Exception}",
"rollOnFileSizeLimit": true,
"fileSizeLimitBytes": 4194304,
"retainedFileCountLimit": 5
}
}
]
},
"AllowedHosts": "*"
}
Loading

0 comments on commit ac013de

Please sign in to comment.