Remove Serilog from Identity.API and clean up
This commit is contained in:
parent
3f5f0b94ed
commit
5342c86af0
@ -1,4 +1,4 @@
|
|||||||
global using Azure.Core;
|
global using Azure.Core;
|
||||||
global using Azure.Identity;
|
global using Azure.Identity;
|
||||||
global using HealthChecks.UI.Client;
|
global using HealthChecks.UI.Client;
|
||||||
global using IdentityModel;
|
global using IdentityModel;
|
||||||
@ -41,7 +41,6 @@ global using System.Collections.Generic;
|
|||||||
global using System.ComponentModel.DataAnnotations;
|
global using System.ComponentModel.DataAnnotations;
|
||||||
global using System.Data.SqlClient;
|
global using System.Data.SqlClient;
|
||||||
global using System.IdentityModel.Tokens.Jwt;
|
global using System.IdentityModel.Tokens.Jwt;
|
||||||
global using System.IO;
|
|
||||||
global using System.Linq;
|
global using System.Linq;
|
||||||
global using System.Security.Claims;
|
global using System.Security.Claims;
|
||||||
global using System.Text.RegularExpressions;
|
global using System.Text.RegularExpressions;
|
||||||
|
@ -37,12 +37,6 @@
|
|||||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" />
|
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" />
|
||||||
<PackageReference Include="Microsoft.Web.LibraryManager.Build" />
|
<PackageReference Include="Microsoft.Web.LibraryManager.Build" />
|
||||||
<PackageReference Include="Polly" />
|
<PackageReference Include="Polly" />
|
||||||
<PackageReference Include="Serilog.AspNetCore" />
|
|
||||||
<PackageReference Include="Serilog.Enrichers.Environment" />
|
|
||||||
<PackageReference Include="Serilog.Settings.Configuration" />
|
|
||||||
<PackageReference Include="Serilog.Sinks.Console" />
|
|
||||||
<PackageReference Include="Serilog.Sinks.Http" />
|
|
||||||
<PackageReference Include="Serilog.Sinks.Seq" />
|
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" />
|
<PackageReference Include="Swashbuckle.AspNetCore" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" />
|
||||||
<PackageReference Include="System.Data.SqlClient" />
|
<PackageReference Include="System.Data.SqlClient" />
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
var appName = "Identity.API";
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
var builder = WebApplication.CreateBuilder();
|
|
||||||
|
|
||||||
if (builder.Configuration.GetValue<bool>("UseVault", false))
|
if (builder.Configuration.GetValue<bool>("UseVault", false))
|
||||||
{
|
{
|
||||||
@ -10,21 +9,43 @@ if (builder.Configuration.GetValue<bool>("UseVault", false))
|
|||||||
builder.Configuration.AddAzureKeyVault(new Uri($"https://{builder.Configuration["Vault:Name"]}.vault.azure.net/"), credential);
|
builder.Configuration.AddAzureKeyVault(new Uri($"https://{builder.Configuration["Vault:Name"]}.vault.azure.net/"), credential);
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.AddCustomConfiguration();
|
builder.Services.AddControllersWithViews();
|
||||||
builder.AddCustomSerilog();
|
builder.Services.AddControllers();
|
||||||
builder.AddCustomMvc();
|
builder.Services.AddRazorPages();
|
||||||
builder.AddCustomDatabase();
|
|
||||||
builder.AddCustomIdentity();
|
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("IdentityDb")));
|
||||||
builder.AddCustomIdentityServer();
|
builder.Services.AddIdentity<ApplicationUser, IdentityRole>()
|
||||||
builder.AddCustomAuthentication();
|
.AddEntityFrameworkStores<ApplicationDbContext>()
|
||||||
builder.AddCustomHealthChecks();
|
.AddDefaultTokenProviders();
|
||||||
builder.AddCustomApplicationServices();
|
|
||||||
|
builder.Services.AddIdentityServer(options =>
|
||||||
|
{
|
||||||
|
options.IssuerUri = "null";
|
||||||
|
options.Authentication.CookieLifetime = TimeSpan.FromHours(2);
|
||||||
|
|
||||||
|
options.Events.RaiseErrorEvents = true;
|
||||||
|
options.Events.RaiseInformationEvents = true;
|
||||||
|
options.Events.RaiseFailureEvents = true;
|
||||||
|
options.Events.RaiseSuccessEvents = true;
|
||||||
|
})
|
||||||
|
.AddInMemoryIdentityResources(Config.GetResources())
|
||||||
|
.AddInMemoryApiScopes(Config.GetApiScopes())
|
||||||
|
.AddInMemoryApiResources(Config.GetApis())
|
||||||
|
.AddInMemoryClients(Config.GetClients(builder.Configuration))
|
||||||
|
.AddAspNetIdentity<ApplicationUser>()
|
||||||
|
.AddDeveloperSigningCredential(); // Not recommended for production - you need to store your key material somewhere secure
|
||||||
|
|
||||||
|
builder.Services.AddAuthentication();
|
||||||
|
builder.Services.AddHealthChecks()
|
||||||
|
.AddCheck("self", () => HealthCheckResult.Healthy())
|
||||||
|
.AddSqlServer(builder.Configuration.GetConnectionString("IdentityDb"),
|
||||||
|
name: "IdentityDB-check",
|
||||||
|
tags: new string[] { "IdentityDB" });
|
||||||
|
builder.Services.AddTransient<IProfileService, ProfileService>();
|
||||||
|
builder.Services.AddTransient<ILoginService<ApplicationUser>, EFLoginService>();
|
||||||
|
builder.Services.AddTransient<IRedirectService, RedirectService>();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
if (app.Environment.IsDevelopment())
|
|
||||||
{
|
|
||||||
app.UseDeveloperExceptionPage();
|
|
||||||
}
|
|
||||||
|
|
||||||
var pathBase = builder.Configuration["PATH_BASE"];
|
var pathBase = builder.Configuration["PATH_BASE"];
|
||||||
if (!string.IsNullOrEmpty(pathBase))
|
if (!string.IsNullOrEmpty(pathBase))
|
||||||
@ -35,16 +56,11 @@ app.UseStaticFiles();
|
|||||||
|
|
||||||
// This cookie policy fixes login issues with Chrome 80+ using HHTP
|
// This cookie policy fixes login issues with Chrome 80+ using HHTP
|
||||||
app.UseCookiePolicy(new CookiePolicyOptions { MinimumSameSitePolicy = SameSiteMode.Lax });
|
app.UseCookiePolicy(new CookiePolicyOptions { MinimumSameSitePolicy = SameSiteMode.Lax });
|
||||||
|
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
|
|
||||||
app.UseIdentityServer();
|
app.UseIdentityServer();
|
||||||
|
|
||||||
|
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
app.MapDefaultControllerRoute();
|
app.MapDefaultControllerRoute();
|
||||||
|
|
||||||
app.MapHealthChecks("/hc", new HealthCheckOptions()
|
app.MapHealthChecks("/hc", new HealthCheckOptions()
|
||||||
{
|
{
|
||||||
Predicate = _ => true,
|
Predicate = _ => true,
|
||||||
@ -54,9 +70,6 @@ app.MapHealthChecks("/liveness", new HealthCheckOptions
|
|||||||
{
|
{
|
||||||
Predicate = r => r.Name.Contains("self")
|
Predicate = r => r.Name.Contains("self")
|
||||||
});
|
});
|
||||||
try
|
|
||||||
{
|
|
||||||
app.Logger.LogInformation("Seeding database ({ApplicationName})...", appName);
|
|
||||||
|
|
||||||
// Apply database migration automatically. Note that this approach is not
|
// Apply database migration automatically. Note that this approach is not
|
||||||
// recommended for production scenarios. Consider generating SQL scripts from
|
// recommended for production scenarios. Consider generating SQL scripts from
|
||||||
@ -66,17 +79,4 @@ try
|
|||||||
await SeedData.EnsureSeedData(scope, app.Configuration, app.Logger);
|
await SeedData.EnsureSeedData(scope, app.Configuration, app.Logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
app.Logger.LogInformation("Starting web host ({ApplicationName})...", appName);
|
await app.RunAsync();
|
||||||
app.Run();
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
app.Logger.LogCritical(ex, "Host terminated unexpectedly ({ApplicationName})...", appName);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
Serilog.Log.CloseAndFlush();
|
|
||||||
}
|
|
||||||
|
@ -1,117 +0,0 @@
|
|||||||
using Serilog;
|
|
||||||
|
|
||||||
namespace Microsoft.eShopOnContainers.Services.Identity.API;
|
|
||||||
|
|
||||||
public static class ProgramExtensions
|
|
||||||
{
|
|
||||||
private const string AppName = "Identity API";
|
|
||||||
|
|
||||||
public static void AddCustomConfiguration(this WebApplicationBuilder builder)
|
|
||||||
{
|
|
||||||
builder.Configuration.AddConfiguration(GetConfiguration()).Build();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void AddCustomSerilog(this WebApplicationBuilder builder)
|
|
||||||
{
|
|
||||||
var seqServerUrl = builder.Configuration["SeqServerUrl"];
|
|
||||||
var logstashUrl = builder.Configuration["LogstashgUrl"];
|
|
||||||
|
|
||||||
Log.Logger = new LoggerConfiguration()
|
|
||||||
.MinimumLevel.Verbose()
|
|
||||||
.Enrich.WithProperty("ApplicationContext", AppName)
|
|
||||||
.Enrich.FromLogContext()
|
|
||||||
.WriteTo.Console()
|
|
||||||
.WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl)
|
|
||||||
.WriteTo.Http(string.IsNullOrWhiteSpace(logstashUrl) ? "http://localhost:8080" : logstashUrl, null)
|
|
||||||
.ReadFrom.Configuration(builder.Configuration)
|
|
||||||
.CreateLogger();
|
|
||||||
|
|
||||||
builder.Host.UseSerilog();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void AddCustomMvc(this WebApplicationBuilder builder)
|
|
||||||
{
|
|
||||||
builder.Services.AddControllersWithViews();
|
|
||||||
builder.Services.AddControllers();
|
|
||||||
builder.Services.AddRazorPages();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static void AddCustomDatabase(this WebApplicationBuilder builder) =>
|
|
||||||
builder.Services.AddDbContext<ApplicationDbContext>(
|
|
||||||
options => options.UseSqlServer(builder.Configuration.GetConnectionString("IdentityDb")));
|
|
||||||
|
|
||||||
public static void AddCustomIdentity(this WebApplicationBuilder builder)
|
|
||||||
{
|
|
||||||
builder.Services.AddIdentity<ApplicationUser, IdentityRole>()
|
|
||||||
.AddEntityFrameworkStores<ApplicationDbContext>()
|
|
||||||
.AddDefaultTokenProviders();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static void AddCustomIdentityServer(this WebApplicationBuilder builder)
|
|
||||||
{
|
|
||||||
var identityServerBuilder = builder.Services.AddIdentityServer(options =>
|
|
||||||
{
|
|
||||||
options.IssuerUri = "null";
|
|
||||||
options.Authentication.CookieLifetime = TimeSpan.FromHours(2);
|
|
||||||
|
|
||||||
options.Events.RaiseErrorEvents = true;
|
|
||||||
options.Events.RaiseInformationEvents = true;
|
|
||||||
options.Events.RaiseFailureEvents = true;
|
|
||||||
options.Events.RaiseSuccessEvents = true;
|
|
||||||
})
|
|
||||||
.AddInMemoryIdentityResources(Config.GetResources())
|
|
||||||
.AddInMemoryApiScopes(Config.GetApiScopes())
|
|
||||||
.AddInMemoryApiResources(Config.GetApis())
|
|
||||||
.AddInMemoryClients(Config.GetClients(builder.Configuration))
|
|
||||||
.AddAspNetIdentity<ApplicationUser>();
|
|
||||||
|
|
||||||
// not recommended for production - you need to store your key material somewhere secure
|
|
||||||
identityServerBuilder.AddDeveloperSigningCredential();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void AddCustomAuthentication(this WebApplicationBuilder builder)
|
|
||||||
{
|
|
||||||
builder.Services.AddAuthentication();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void AddCustomHealthChecks(this WebApplicationBuilder builder)
|
|
||||||
{
|
|
||||||
builder.Services.AddHealthChecks()
|
|
||||||
.AddCheck("self", () => HealthCheckResult.Healthy())
|
|
||||||
.AddSqlServer(builder.Configuration.GetConnectionString("IdentityDb"),
|
|
||||||
name: "IdentityDB-check",
|
|
||||||
tags: new string[] { "IdentityDB" });
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void AddCustomApplicationServices(this WebApplicationBuilder builder)
|
|
||||||
{
|
|
||||||
builder.Services.AddTransient<IProfileService, ProfileService>();
|
|
||||||
builder.Services.AddTransient<ILoginService<ApplicationUser>, EFLoginService>();
|
|
||||||
builder.Services.AddTransient<IRedirectService, RedirectService>();
|
|
||||||
}
|
|
||||||
|
|
||||||
static IConfiguration GetConfiguration()
|
|
||||||
{
|
|
||||||
var builder = new ConfigurationBuilder()
|
|
||||||
.SetBasePath(Directory.GetCurrentDirectory())
|
|
||||||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
||||||
.AddEnvironmentVariables();
|
|
||||||
|
|
||||||
var config = builder.Build();
|
|
||||||
|
|
||||||
if (config.GetValue<bool>("UseVault", false))
|
|
||||||
{
|
|
||||||
TokenCredential credential = new ClientSecretCredential(
|
|
||||||
config["Vault:TenantId"],
|
|
||||||
config["Vault:ClientId"],
|
|
||||||
config["Vault:ClientSecret"]);
|
|
||||||
builder.AddAzureKeyVault(new Uri($"https://{config["Vault:Name"]}.vault.azure.net/"), credential);
|
|
||||||
}
|
|
||||||
|
|
||||||
return builder.Build();
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user