public async Task <string> GetIdentifierAsync(object context) { if (!(context is HttpContext)) { throw new MultiTenantException(null, new ArgumentException($"\"{nameof(context)}\" type must be of type HttpContext", nameof(context))); } var httpContext = context as HttpContext; try { var tenantId = httpContext.Request?.Cookies[_tenantKey]; if (!string.IsNullOrWhiteSpace(tenantId)) { tenantId = AesOperation.DecryptString(AesOperation.Key, tenantId); var store = httpContext.RequestServices.GetRequiredService <IMultiTenantStore>(); var tenantInfo = await store.TryGetAsync(tenantId); return(tenantInfo?.Identifier); } } catch (Exception) { // do nothing } return(null); }
public static void SetTenantCookie(HttpResponse response, ITenantContext tenantContext) { var tenantKey = tenantContext.TenantConfigurations.MultiTenantCookieKey(); var tenantId = tenantContext.Tenant?.Id; if (string.IsNullOrWhiteSpace(tenantId)) { response.Cookies.Delete(tenantKey); } else { var encrypted = AesOperation.EncryptString(AesOperation.Key, tenantId); //TODO: encrypt value response.Cookies.Append( tenantKey, encrypted, new CookieOptions { Path = "/", HttpOnly = false } ); } }