From fd0f010f5c95d4e8cfcc6b013b86186ff8dffb28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Tue, 6 Mar 2018 15:41:04 +0300 Subject: [PATCH] Created ClaimsHttpTenantResolveContributer. --- .../MicroservicesDemoWebModule.cs | 4 ++-- .../AbpAspNetCoreMultiTenancyModule.cs | 9 +++---- .../ClaimsHttpTenantResolveContributer.cs | 24 +++++++++++++++++++ .../HttpTenantResolveContributerBase.cs | 2 +- .../MultiTenancy/MultiTenancyMiddleware.cs | 19 ++++++++------- 5 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/ClaimsHttpTenantResolveContributer.cs diff --git a/src/MicroserviceDemo/MicroserviceDemo.Web/MicroservicesDemoWebModule.cs b/src/MicroserviceDemo/MicroserviceDemo.Web/MicroservicesDemoWebModule.cs index 0fa634c277..412332d08a 100644 --- a/src/MicroserviceDemo/MicroserviceDemo.Web/MicroservicesDemoWebModule.cs +++ b/src/MicroserviceDemo/MicroserviceDemo.Web/MicroservicesDemoWebModule.cs @@ -127,8 +127,6 @@ namespace MicroserviceDemo.Web app.UseDeveloperExceptionPage(); } - app.UseMultiTenancy(); - app.UseStaticFiles(); app.UseVirtualFiles(); @@ -140,6 +138,8 @@ namespace MicroserviceDemo.Web app.UseAuthentication(); + app.UseMultiTenancy(); + app.UseMvc(routes => { routes.MapRoute( diff --git a/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetCoreMultiTenancyModule.cs b/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetCoreMultiTenancyModule.cs index 05e955f803..14f196f27f 100644 --- a/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetCoreMultiTenancyModule.cs +++ b/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetCoreMultiTenancyModule.cs @@ -14,10 +14,11 @@ namespace Volo.Abp.AspNetCore.MultiTenancy { services.Configure(options => { - options.TenantResolvers.Insert(0, new QueryStringTenantResolveContributer()); - options.TenantResolvers.Insert(1, new RouteTenantResolveContributer()); - options.TenantResolvers.Insert(2, new HeaderTenantResolveContributer()); - options.TenantResolvers.Insert(3, new CookieTenantResolveContributer()); + options.TenantResolvers.Insert(0, new ClaimsHttpTenantResolveContributer()); + options.TenantResolvers.Insert(1, new QueryStringTenantResolveContributer()); + options.TenantResolvers.Insert(2, new RouteTenantResolveContributer()); + options.TenantResolvers.Insert(3, new HeaderTenantResolveContributer()); + options.TenantResolvers.Insert(4, new CookieTenantResolveContributer()); }); services.AddAssemblyOf(); diff --git a/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/ClaimsHttpTenantResolveContributer.cs b/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/ClaimsHttpTenantResolveContributer.cs new file mode 100644 index 0000000000..a320a1fbee --- /dev/null +++ b/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/ClaimsHttpTenantResolveContributer.cs @@ -0,0 +1,24 @@ +using System.Linq; +using Microsoft.AspNetCore.Http; +using Volo.Abp.MultiTenancy; + +namespace Volo.Abp.AspNetCore.MultiTenancy +{ + public class ClaimsHttpTenantResolveContributer : HttpTenantResolveContributerBase + { + protected override void ResolveFromHttpContext(ITenantResolveContext context, HttpContext httpContext) + { + if (httpContext.User?.Identity?.IsAuthenticated == true) + { + base.ResolveFromHttpContext(context, httpContext); + context.Handled = true; + } + } + + protected override string GetTenantIdOrNameFromHttpContextOrNull(ITenantResolveContext context, HttpContext httpContext) + { + var tenantKey = context.GetAspNetCoreMultiTenancyOptions().TenantKey; + return httpContext.User.Claims.FirstOrDefault(c => c.Type == tenantKey)?.Value; + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/HttpTenantResolveContributerBase.cs b/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/HttpTenantResolveContributerBase.cs index 7059eef9a8..51bdc57323 100644 --- a/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/HttpTenantResolveContributerBase.cs +++ b/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/HttpTenantResolveContributerBase.cs @@ -29,7 +29,7 @@ namespace Volo.Abp.AspNetCore.MultiTenancy } } - private void ResolveFromHttpContext(ITenantResolveContext context, HttpContext httpContext) + protected virtual void ResolveFromHttpContext(ITenantResolveContext context, HttpContext httpContext) { var tenantIdOrName = GetTenantIdOrNameFromHttpContextOrNull(context, httpContext); if (!tenantIdOrName.IsNullOrEmpty()) diff --git a/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs b/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs index 6cbc541003..bf27cc92e6 100644 --- a/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs +++ b/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using JetBrains.Annotations; using Microsoft.AspNetCore.Http; using Volo.Abp.MultiTenancy; @@ -27,13 +28,18 @@ namespace Volo.Abp.AspNetCore.MultiTenancy public async Task Invoke(HttpContext httpContext) { - //TODO: Try-catch and return "unknown tenant" if found tenant is not in the store..? + using (SetCurrentTenant(await ResolveCurrentTenantAsync())) + { + await _next(httpContext); + } + } + private async Task ResolveCurrentTenantAsync() + { var tenantIdOrName = _tenantResolver.ResolveTenantIdOrName(); if (tenantIdOrName == null) { - await _next(httpContext); - return; + return null; } var tenant = await FindTenantAsync(tenantIdOrName); @@ -42,10 +48,7 @@ namespace Volo.Abp.AspNetCore.MultiTenancy throw new AbpException("There is no tenant with given tenant id or name: " + tenantIdOrName); } - using (SetCurrent(tenant)) - { - await _next(httpContext); - } + return tenant; } private async Task FindTenantAsync(string tenantIdOrName) @@ -60,7 +63,7 @@ namespace Volo.Abp.AspNetCore.MultiTenancy } } - private IDisposable SetCurrent(TenantInfo tenant) + private IDisposable SetCurrentTenant([CanBeNull] TenantInfo tenant) { var parentScope = _currentTenantIdAccessor.Current; _currentTenantIdAccessor.Current = new TenantIdWrapper(tenant?.Id);