hello-shop/samples/MultiTenancySample/MultiTenancySample.ServiceDefaults/TenantIdProvider.cs
2024-07-05 19:25:58 +08:00

45 lines
1.4 KiB
C#

// Copyright (c) HelloShop Corporation. All rights reserved.
// See the license file in the project root for more information.
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.User.FindAll(tenantKey).Any())
{
return httpContext.User.FindFirst(tenantKey)?.Value;
}
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);
}
}
}