mirror of https://github.com/abpframework/abp.git
9 changed files with 115 additions and 26 deletions
@ -0,0 +1,8 @@ |
|||
namespace Volo.Abp.AspNetCore.MultiTenancy |
|||
{ |
|||
public static class AbpAspNetCoreMultiTenancyConsts |
|||
{ |
|||
//TODO: Get from an option instead of a constant!
|
|||
public const string TenantIdKey = "__tenantId"; |
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
using Microsoft.AspNetCore.Http; |
|||
|
|||
namespace Volo.Abp.AspNetCore.MultiTenancy |
|||
{ |
|||
public class CookieTenantResolver : HttpTenantResolverBase |
|||
{ |
|||
protected override string GetTenantIdFromHttpContextOrNull(HttpContext httpContext) |
|||
{ |
|||
return httpContext.Request.Cookies[AbpAspNetCoreMultiTenancyConsts.TenantIdKey]; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,13 @@ |
|||
using Microsoft.AspNetCore.Http; |
|||
|
|||
namespace Volo.Abp.AspNetCore.MultiTenancy |
|||
{ |
|||
public class HeaderTenantResolver : HttpTenantResolverBase |
|||
{ |
|||
protected override string GetTenantIdFromHttpContextOrNull(HttpContext httpContext) |
|||
{ |
|||
//TODO: Get first one if provided multiple values and write a log
|
|||
return httpContext.Request.Headers[AbpAspNetCoreMultiTenancyConsts.TenantIdKey]; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
using Microsoft.AspNetCore.Http; |
|||
using Volo.Abp.MultiTenancy; |
|||
using Volo.ExtensionMethods; |
|||
|
|||
namespace Volo.Abp.AspNetCore.MultiTenancy |
|||
{ |
|||
public abstract class HttpTenantResolverBase : ITenantResolver |
|||
{ |
|||
public virtual void Resolve(ITenantResolveContext context) |
|||
{ |
|||
var httpContext = context.GetHttpContext(); |
|||
if (httpContext == null) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
var tenantId = GetTenantIdFromHttpContextOrNull(httpContext); |
|||
if (!tenantId.IsNullOrEmpty()) |
|||
{ |
|||
context.Tenant = new TenantInfo(tenantId); |
|||
} |
|||
} |
|||
|
|||
protected abstract string GetTenantIdFromHttpContextOrNull(HttpContext httpContext); |
|||
} |
|||
} |
|||
@ -1,30 +1,17 @@ |
|||
using Volo.Abp.MultiTenancy; |
|||
using Volo.ExtensionMethods.Collections.Generic; |
|||
using Microsoft.AspNetCore.Http; |
|||
|
|||
namespace Volo.Abp.AspNetCore.MultiTenancy |
|||
{ |
|||
public class QueryStringTenantResolver : ITenantResolver |
|||
public class QueryStringTenantResolver : HttpTenantResolverBase |
|||
{ |
|||
public const string TenantIdKey = "__tenantId"; |
|||
|
|||
public void Resolve(ITenantResolveContext context) |
|||
protected override string GetTenantIdFromHttpContextOrNull(HttpContext httpContext) |
|||
{ |
|||
var httpContext = context.GetHttpContext(); |
|||
if (httpContext == null) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
if (!httpContext.Request.QueryString.HasValue) |
|||
{ |
|||
return; |
|||
return null; |
|||
} |
|||
|
|||
var tenantId = httpContext.Request.Query[TenantIdKey]; |
|||
if (!tenantId.IsNullOrEmpty()) |
|||
{ |
|||
context.Tenant = new TenantInfo(tenantId); |
|||
} |
|||
return httpContext.Request.Query[AbpAspNetCoreMultiTenancyConsts.TenantIdKey]; |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,20 @@ |
|||
using System; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.AspNetCore.Routing; |
|||
|
|||
namespace Volo.Abp.AspNetCore.MultiTenancy |
|||
{ |
|||
public class RouteTenantResolver : HttpTenantResolverBase |
|||
{ |
|||
protected override string GetTenantIdFromHttpContextOrNull(HttpContext httpContext) |
|||
{ |
|||
var tenantId = httpContext.GetRouteValue(AbpAspNetCoreMultiTenancyConsts.TenantIdKey); |
|||
if (tenantId == null) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
return Convert.ToString(tenantId); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue