diff --git a/src/CoreDocker.Api/Logging.cs b/src/CoreDocker.Api/Logging.cs new file mode 100644 index 0000000..0a1ad84 --- /dev/null +++ b/src/CoreDocker.Api/Logging.cs @@ -0,0 +1,23 @@ +using CoreDocker.Core.Framework.Logging; +using Serilog.Events; +using Path = System.IO.Path; + +namespace CoreDocker.Api; + +public static class Logging +{ + public static void AddSerilog(this WebApplicationBuilder builder) + { + Log.Logger = LoggingHelper.SetupOnce(() => new LoggerConfiguration() + .MinimumLevel.Information() + .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) + .Enrich.FromLogContext() + .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}") + .WriteTo.File(Path.Combine(Path.GetTempPath(), "logs", "CoreDocker.Api.log"), + fileSizeLimitBytes: 10 * LoggingHelper.MB, + rollOnFileSizeLimit: true) + .CreateLogger()); + builder.Logging.ClearProviders(); + builder.Logging.AddSerilog(Log.Logger); + } +} \ No newline at end of file diff --git a/src/CoreDocker.Api/Program.cs b/src/CoreDocker.Api/Program.cs index 1061050..6459cc8 100644 --- a/src/CoreDocker.Api/Program.cs +++ b/src/CoreDocker.Api/Program.cs @@ -1,4 +1,4 @@ -using System.Runtime.CompilerServices; +using CoreDocker.Api; using CoreDocker.Api.AppStartup; using CoreDocker.Api.GraphQl; using CoreDocker.Api.Security; @@ -11,7 +11,7 @@ var builder = WebApplication.CreateBuilder(args); builder.Configuration.AddJsonFilesAndEnvironment(); - +builder.AddSerilog(); builder.Services.AddMediatR(cfg => cfg .RegisterServicesFromAssemblyContaining() @@ -62,7 +62,6 @@ app.Run(); - namespace CoreDocker.Api { public partial class Program diff --git a/src/CoreDocker.Api/Security/OpenIdSettings.cs b/src/CoreDocker.Api/Security/OpenIdSettings.cs index 6792446..bccf60b 100644 --- a/src/CoreDocker.Api/Security/OpenIdSettings.cs +++ b/src/CoreDocker.Api/Security/OpenIdSettings.cs @@ -29,7 +29,7 @@ public OpenIdSettings(IConfiguration configuration) : base(configuration, "OpenI public bool UseReferenceTokens => ReadConfigValue("UseReferenceTokens", true); - public bool IsDebugEnabled => ReadConfigValue("IsDebugEnabled", true); + public bool IsDebugEnabled => ReadConfigValue("IsDebugEnabled", false); public string[] GetOriginList() { diff --git a/src/CoreDocker.Api/Security/SecuritySetupClient.cs b/src/CoreDocker.Api/Security/SecuritySetupClient.cs index b6dd8fb..14bdabd 100644 --- a/src/CoreDocker.Api/Security/SecuritySetupClient.cs +++ b/src/CoreDocker.Api/Security/SecuritySetupClient.cs @@ -13,6 +13,7 @@ public static void AddAuthenticationClient(this IServiceCollection services, Ope services.AddDistributedMemoryCache(); services.AddAuthorization(AddFromActivities); services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme) + .AddCookie(SecuritySetupServer.CookieAuthenticationScheme) .AddIdentityServerAuthentication(options => { options.Authority = idSettings.HostUrl; diff --git a/src/CoreDocker.Api/Security/SecuritySetupServer.cs b/src/CoreDocker.Api/Security/SecuritySetupServer.cs index 4180e01..bcf8af4 100644 --- a/src/CoreDocker.Api/Security/SecuritySetupServer.cs +++ b/src/CoreDocker.Api/Security/SecuritySetupServer.cs @@ -10,14 +10,18 @@ namespace CoreDocker.Api.Security { public static class SecuritySetupServer { - private static readonly ILogger _log = Log.ForContext(MethodBase.GetCurrentMethod()?.DeclaringType); - + public const string CookieAuthenticationScheme = "CoreDockerCookie"; + private static readonly ILogger _log = Log.ForContext(MethodBase.GetCurrentMethod()!.DeclaringType!); public static void AddIdentityService(this IServiceCollection services, IConfiguration configuration) { var openIdSettings = new OpenIdSettings(configuration); _log.Debug($"SecuritySetupServer:UseIdentityService Setting the host url {openIdSettings.HostUrl}"); - services.AddIdentityServer() + services.AddIdentityServer(options=> + { + options.IssuerUri = openIdSettings.HostUrl; + options.Authentication.CookieAuthenticationScheme = CookieAuthenticationScheme; + }) .AddSigningCredential(Certificate(openIdSettings.CertPfx, openIdSettings.CertPassword, openIdSettings.CertStoreThumbprint)) .AddInMemoryIdentityResources(OpenIdConfig.GetIdentityResources()) diff --git a/src/CoreDocker.Core/Framework/Logging/LoggingHelper.cs b/src/CoreDocker.Core/Framework/Logging/LoggingHelper.cs index a892d22..73ee2af 100644 --- a/src/CoreDocker.Core/Framework/Logging/LoggingHelper.cs +++ b/src/CoreDocker.Core/Framework/Logging/LoggingHelper.cs @@ -25,5 +25,6 @@ public static ILogger SetupOnce(Func func) return Log.Logger; } + } } \ No newline at end of file diff --git a/test/CoreDocker.Api.Tests/IntegrationTestsBase.cs b/test/CoreDocker.Api.Tests/IntegrationTestsBase.cs index 271a1f4..c87efcc 100644 --- a/test/CoreDocker.Api.Tests/IntegrationTestsBase.cs +++ b/test/CoreDocker.Api.Tests/IntegrationTestsBase.cs @@ -63,7 +63,6 @@ private void SetEnvironmentVariable(string variable, string value) public HttpClient CreateClient(string name) { - Console.Out.WriteLine("name:"+name); return CreateClient(); } }