-
Notifications
You must be signed in to change notification settings - Fork 722
Expand file tree
/
Copy pathTenantService.cs
More file actions
40 lines (32 loc) · 1.31 KB
/
TenantService.cs
File metadata and controls
40 lines (32 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using System;
namespace TodoApi.Services
{
public interface ITenantService
{
string GetConnectionString();
}
public class TenantService : ITenantService
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IConfiguration _configuration;
public TenantService(IHttpContextAccessor httpContextAccessor, IConfiguration configuration)
{
_httpContextAccessor = httpContextAccessor;
_configuration = configuration;
}
public string GetConnectionString()
{
// Extract client ID from header
var clientId = _httpContextAccessor.HttpContext?.Request.Headers["X-Client-ID"].ToString();
if (string.IsNullOrEmpty(clientId))
throw new UnauthorizedAccessException("Client ID is missing.");
// Retrieve the connection string from configuration
var connectionString = _configuration.GetSection($"ConnectionStrings:ClientDatabases:{clientId}").Value;
if (string.IsNullOrEmpty(connectionString))
throw new UnauthorizedAccessException("Invalid client ID or connection string not found.");
return connectionString;
}
}
}