零框架多租户设计方案
This commit is contained in:
1 parent
b7caaa2d0c
commit
b4a704526d
42 files changed
+1002
-1
No files matched your search
@@ -0,0 +1,36 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace MultiTenancySample.ServiceDefaults
|
||||
{
|
||||
public class TenantIdProvider(IHttpContextAccessor httpContextAccessor) : ITenantIdProvider
|
||||
{
|
||||
public async Task<string?> GetTenantIdAsync()
|
||||
{
|
||||
HttpContext httpContext = httpContextAccessor.HttpContext ?? new DefaultHttpContext();
|
||||
|
||||
const string tenantKey = "tenant";
|
||||
|
||||
if (httpContext.Request.Headers.TryGetValue(tenantKey, out var headerValues))
|
||||
{
|
||||
return headerValues.First();
|
||||
}
|
||||
|
||||
if (httpContext.Request.Query.TryGetValue(tenantKey, out var queryValues))
|
||||
{
|
||||
return queryValues.First();
|
||||
}
|
||||
|
||||
if (httpContext.Request.Cookies.TryGetValue(tenantKey, out var cookieValue))
|
||||
{
|
||||
return cookieValue;
|
||||
}
|
||||
|
||||
if (httpContext.Request.RouteValues.TryGetValue(tenantKey, out var routeValue))
|
||||
{
|
||||
return routeValue?.ToString();
|
||||
}
|
||||
|
||||
return await Task.FromResult<string?>(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user