diff --git a/Sample.Authentication/Program.cs b/Sample.Authentication/Program.cs index 35ab590..e8d6afb 100644 --- a/Sample.Authentication/Program.cs +++ b/Sample.Authentication/Program.cs @@ -1,12 +1,5 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore; +using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Logging; using Serilog; using Serilog.Events; @@ -14,6 +7,7 @@ namespace Sample.Authentication { public class Program { + public static void Main(string[] args) { Log.Logger = new LoggerConfiguration() @@ -31,8 +25,11 @@ public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup() + .ConfigureLogging((b)=> { + b.AddSerilog(Log.Logger); // this does! + }) .UseUrls("http://*:5000", "http://*:5001", "http://*:5002", "http://*:5003", "http://*:5004") - .UseSerilog() + // .UseSerilog(providers: Providers) // this doesn't work with dotnettency! .Build(); } } diff --git a/Sample.Authentication/Sample.Authentication.csproj b/Sample.Authentication/Sample.Authentication.csproj index f522c4f..cadd8a0 100644 --- a/Sample.Authentication/Sample.Authentication.csproj +++ b/Sample.Authentication/Sample.Authentication.csproj @@ -1,4 +1,4 @@ - + netcoreapp2.0 @@ -9,11 +9,12 @@ - - - + + + + - + diff --git a/Sample.Authentication/Startup.cs b/Sample.Authentication/Startup.cs index f5a318f..b6ca930 100644 --- a/Sample.Authentication/Startup.cs +++ b/Sample.Authentication/Startup.cs @@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Security.Claims; @@ -15,90 +16,105 @@ public class Startup { public IServiceProvider ConfigureServices(IServiceCollection services) { - IServiceProvider serviceProvider = services.AddAspNetCoreMultiTenancy((options) => + var defaultServices = services.Clone(); + + try { - options - .InitialiseTenant() // factory class to load tenant when it needs to be initialised for the first time. Can use overload to provide a delegate instead. - .ConfigureTenantContainers((containerBuilder) => - { - containerBuilder.WithAutofac((tenant, tenantServices) => - { - if (tenant.Name == "Moogle") - { - tenantServices.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) - .AddCookie((c) => - { - c.Cookie.Name = tenant.Name; - }); - } - }) - .AddPerRequestContainerMiddlewareServices() // services needed for per tenant container middleware. - .AddPerTenantMiddlewarePipelineServices(); // services needed for per tenant middleware pipeline. - }) - .ConfigureTenantMiddleware((a) => - { - a.OnInitialiseTenantPipeline((b, c) => - { - c.UseDeveloperExceptionPage(); - c.UseStaticFiles(); - - // var log = c.ApplicationServices.GetRequiredService>(); - if (b.Tenant.Name == "Moogle") - { - c.UseAuthentication(); - - // Browse to /Protected endpoint, will issue a challenge if not authenticated. - // This challenge automatically redirects to the default login path = /Account/Login - c.Map("/Protected", (d) => - { - d.Run(async (h) => - { - if (!h.User.Identity?.IsAuthenticated ?? false) - { - await h.ChallengeAsync(); - } - else - { - await h.Response.WriteAsync("Authenticated as: " + h.User.FindFirstValue(ClaimTypes.Name)); - } - }); - }); - - // Browse to /Account/Login will automatically create a sign in cookie then redirect to /Protected - c.Map("/Account/Login", (d) => - { - d.Run(async (h) => - { - List claims = new List{ + var sp = services.AddMultiTenancy((builder) => + { + builder.IdentifyTenantsWithRequestAuthorityUri() + .InitialiseTenant() + .AddAspNetCore() + .ConfigureTenantContainers((containerOptions) => + { + containerOptions + .SetDefaultServices(defaultServices) + .Autofac((tenant, tenantServices) => + { + if (tenant.Name == "Moogle") + { + tenantServices.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) + .AddCookie((c) => + { + c.Cookie.Name = tenant.Name; + }); + } + }); + }) + .ConfigureTenantMiddleware((tenantOptions) => + { + tenantOptions.AspNetCorePipeline((context, tenantAppBuilder) => + { + + tenantAppBuilder.UseDeveloperExceptionPage(); + tenantAppBuilder.UseStaticFiles(); + + // var log = c.ApplicationServices.GetRequiredService>(); + if (context.Tenant.Name == "Moogle") + { + tenantAppBuilder.UseAuthentication(); + + // Browse to /Protected endpoint, will issue a challenge if not authenticated. + // This challenge automatically redirects to the default login path = /Account/Login + tenantAppBuilder.Map("/Protected", (d) => + { + d.Run(async (h) => + { + if (!h.User.Identity?.IsAuthenticated ?? false) + { + await h.ChallengeAsync(); + } + else + { + await h.Response.WriteAsync("Authenticated as: " + h.User.FindFirstValue(ClaimTypes.Name)); + } + }); + }); + + // Browse to /Account/Login will automatically create a sign in cookie then redirect to /Protected + tenantAppBuilder.Map("/Account/Login", (d) => + { + d.Run(async (h) => + { + List claims = new List{ new Claim(ClaimTypes.Name, "testuser"), new Claim("FullName", "test user"), new Claim(ClaimTypes.Role, "Administrator"), - }; + }; - ClaimsIdentity claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); + ClaimsIdentity claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); - AuthenticationProperties authProperties = new AuthenticationProperties - { - RedirectUri = "/Protected" - }; + AuthenticationProperties authProperties = new AuthenticationProperties + { + RedirectUri = "/Protected" + }; - await h.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, - new ClaimsPrincipal(claimsIdentity), authProperties); - }); - }); + await h.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, + new ClaimsPrincipal(claimsIdentity), authProperties); + }); + }); - } + } - // All tenants have welcome page middleware enabled. - c.UseWelcomePage(); + // All tenants have welcome page middleware enabled. + tenantAppBuilder.UseWelcomePage(); - }); - }); + }); + }); - }); + }); + return sp; + + } + catch (Exception ex) + { + + throw ex; + } // When using tenant containers, must return IServiceProvider. - return serviceProvider; + // Note: in asp.netcore 3.0.0 we don't need to, but something additional must be registered in program createhostbuilder. + } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -111,8 +127,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) app = app.UseMultitenancy((options) => { - options.UsePerTenantContainers(); - options.UsePerTenantMiddlewarePipeline(); + options.UseTenantContainers(); + options.UsePerTenantMiddlewarePipeline(app); }); } } diff --git a/Sample.Authentication/TenantShellFactory.cs b/Sample.Authentication/TenantShellFactory.cs index f3840e8..ba69ad4 100644 --- a/Sample.Authentication/TenantShellFactory.cs +++ b/Sample.Authentication/TenantShellFactory.cs @@ -6,7 +6,7 @@ namespace Sample.Authentication { public class TenantShellFactory : ITenantShellFactory { - public Task> Get(TenantDistinguisher distinguisher) + public Task> Get(TenantIdentifier distinguisher) { if (distinguisher.Uri.Port == 5000 || distinguisher.Uri.Port == 5001) {