Skip to content

Commit

Permalink
feat: dinamic windows provider
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Jun 11, 2022
1 parent 94fc001 commit 2aab545
Show file tree
Hide file tree
Showing 36 changed files with 722 additions and 256 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Project: Aguafrommars/TheIdServer
// Copyright (c) 2022 @Olivier Lefebvre

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace Microsoft.AspNetCore.Builder
{
public static class ApplicationBuilderExtensions
{
public static IApplicationBuilder UseApiSample(this IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseHttpsRedirection()
.UseCors(configurePolicy =>
{
configurePolicy.WithOrigins("http://localhost:5002")
.AllowAnyMethod()
.AllowAnyHeader();
});

app.UseRouting()
.UseAuthentication()
.UseAuthorization()
.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});

return app;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Project: Aguafrommars/TheIdServer
// Copyright (c) 2022 @Olivier Lefebvre

using IdentityServer4.AccessTokenValidation;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace Microsoft.AspNetCore.Builder
{
public static class WebApplicationBuilderExtensions
{
public static WebApplicationBuilder AddApiSample(this WebApplicationBuilder webApplicationBuilder)
{
var services = webApplicationBuilder.Services;
services.AddCors()
.AddAuthorization()
.AddControllers();

services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication(options =>
{
options.Authority = "https://localhost:5443";
options.RequireHttpsMetadata = false;
options.SupportedTokens = SupportedTokens.Both;
options.ApiName = "api1";
options.EnableCaching = true;
options.CacheDuration = TimeSpan.FromMinutes(10);
options.LegacyAudienceValidation = true;
});

return webApplicationBuilder;
}
}
}
41 changes: 11 additions & 30 deletions sample/Aguacongas.TheIdServer.ApiSample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,17 @@
// Project: Aguafrommars/TheIdServer
// Copyright (c) 2022 @Olivier Lefebvre
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Builder;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes;

var builder = WebApplication.CreateBuilder(args);

namespace Aguacongas.TheIdServer.ApiSample
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
builder.Host.UseSerilog((hostingContext, configuration) =>
configuration.ReadFrom.Configuration(hostingContext.Configuration));

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>()
.UseSerilog((context, configuration) =>
{
configuration
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.MinimumLevel.Override("System", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
.WriteTo.Debug(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}")
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}", theme: AnsiConsoleTheme.Literate);
});
});
}
}
builder.AddApiSample();

var app = builder.Build();

app.UseApiSample(builder.Environment);

await app.RunAsync().ConfigureAwait(false);
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Project: Aguafrommars/TheIdServer
// Copyright (c) 2022 @Olivier Lefebvre

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace Microsoft.AspNetCore.Builder
{
public static class ApplicationBuilderExtensions
{
public static IApplicationBuilder UseMvcSample(this IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
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()
.UseAuthentication()
.UseStaticFiles()
.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});

return app;
}
}
}

Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Project: Aguafrommars/TheIdServer
// Project: Aguafrommars/TheIdServer
// Copyright (c) 2022 @Olivier Lefebvre

using IdentityModel.Client;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using System;
Expand All @@ -16,20 +14,13 @@
using System.Linq;
using System.Net.Http;

namespace Aguacongas.TheIdServer.MvcClient
namespace Microsoft.AspNetCore.Builder
{
public class Startup
public static class WebApplicationBuilderExtensions
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
public static WebApplicationBuilder AddMvcSample(this WebApplicationBuilder webApplicationBuilder)
{
var services = webApplicationBuilder.Services;
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

var clientId = "mvc";
Expand All @@ -46,12 +37,13 @@ public void ConfigureServices(IServiceCollection services)
{
var pendingRefreshTokenRequests = new ConcurrentDictionary<string, bool>();
var events = options.Events;
var events = options.Events;
events.OnValidatePrincipal = async context =>
{
var tokens = context.Properties.GetTokens();
var services = context.HttpContext.RequestServices;
var logger = services.GetRequiredService<ILogger<Startup>>();
var loggerFactory = services.GetRequiredService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger(nameof(WebApplicationBuilderExtensions));
if (tokens == null || !tokens.Any())
{
logger.LogDebug("No tokens found.");
Expand Down Expand Up @@ -115,7 +107,7 @@ public void ConfigureServices(IServiceCollection services)
await context.HttpContext.SignInAsync(context.Principal, context.Properties);
logger.LogInformation("Automatic refresh token succeed. Next expire date {ExpireAt}", expires);
}
catch(Exception e)
catch (Exception e)
{
logger.LogError(e.Message, e);
}
Expand All @@ -140,36 +132,10 @@ public void ConfigureServices(IServiceCollection services)
options.GetClaimsFromUserInfoEndpoint = true;
options.SaveTokens = true;
});

services.AddControllersWithViews();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
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()
.UseAuthentication()
.UseStaticFiles()
.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
return webApplicationBuilder;
}
}
}
42 changes: 12 additions & 30 deletions sample/Aguacongas.TheIdServer.MvcClient/Program.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,17 @@
// Project: Aguafrommars/TheIdServer
// Copyright (c) 2022 @Olivier Lefebvre
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Builder;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes;

namespace Aguacongas.TheIdServer.MvcClient
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
var builder = WebApplication.CreateBuilder(args);

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>()
.UseSerilog((context, configuration) =>
{
configuration
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.MinimumLevel.Override("System", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
.WriteTo.Debug(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}")
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}", theme: AnsiConsoleTheme.Literate);
});
});
}
}
builder.Host.UseSerilog((hostingContext, configuration) =>
configuration.ReadFrom.Configuration(hostingContext.Configuration));

builder.AddMvcSample();

var app = builder.Build();

app.UseMvcSample(builder.Environment);

await app.RunAsync().ConfigureAwait(false);
51 changes: 0 additions & 51 deletions sample/Aguacongas.TheIdServer.WsFederationSample/Startup.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
"ConnectionStrings": {
"DefaultConnection": "server=localhost;database=theidserver-duende-debug;user=root;password=mysql"
},
"IdentityServer": {
"Key": {
"KeyRotationOptions": {
Expand All @@ -10,7 +7,6 @@
}
}
},
"DbType": "InMemory",
"Serilog": {
"WriteTo": [
{
Expand Down
Loading

0 comments on commit 2aab545

Please sign in to comment.