-
Notifications
You must be signed in to change notification settings - Fork 8
/
TenantMiddleware.cs
61 lines (56 loc) · 2.01 KB
/
TenantMiddleware.cs
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Data.SqlClient;
using Dapper;
using Microsoft.Extensions.Configuration;
namespace ASPNETCoreDapperRLS
{
public class TenantMiddleware
{
private readonly RequestDelegate next;
public TenantMiddleware(RequestDelegate next)
{
this.next = next;
}
public async Task Invoke(HttpContext context, IConfiguration configuration)
{
context.Items["TenantConnection"] = null;
context.Items["Tenant"] = null;
var apiKey = context.Request.Headers["X-API-Key"].FirstOrDefault();
if (string.IsNullOrEmpty(apiKey))
{
return;
}
Guid apiKeyGuid;
if (!Guid.TryParse(apiKey, out apiKeyGuid))
{
return;
}
using (var connection = new SqlConnection(configuration["ConnectionStrings:DefaultConnection"]))
{
await connection.OpenAsync();
var tenant = await SetTenant(connection, apiKeyGuid);
context.Items["TenantConnection"] = connection;
context.Items["Tenant"] = tenant;
await next.Invoke(context);
}
}
private async Task<Tenant> SetTenant(SqlConnection connection, Guid apiKey)
{
var tenant = await connection.QueryFirstOrDefaultAsync<Tenant>("SELECT * FROM Tenant WHERE APIKey = @APIKey", new { APIKey = apiKey });
await connection.ExecuteAsync(@"EXEC dbo.sp_set_session_context @key = N'TenantId', @value = @value", new { value = tenant.TenantId });
return tenant;
}
}
public static class TenantMiddlewareExtension
{
public static IApplicationBuilder UseTenant(this IApplicationBuilder app)
{
app.UseMiddleware<TenantMiddleware>();
return app;
}
}
}