Closed Sheko007 closed 5 years ago
Hi @Sheko007
I'm not sure I understand the question. Are you asking how to access the TenantContext
from your controller code? If that is the question then just use the GetTenantContext()
extension method on HttpContext
like this:
using Finbuckle.MultiTenant.AspNetCore; // this will be just Finbuckle.MultiTenant in v2.0 coming soon
...
IActionResult MyController()
{
var tenantContext = HttpContext.GetTenantContext();
return View();
}
You can also inject the TenantContext
into the controller constructor or action method if you prefer.
Hi @achandlerwhite
i using hoststrategy so tenant will be as subdmain
now i have one tenant name : aaa.localhost:9999
tenant = aaa
so if in code now
aaa.localhost:9999
and use
IActionResult MyController() { var tenantContext = HttpContext.GetTenantContext(); return View(); }
return = tenantContext my tenant = aaa
if me now on localhost:9999
so if me use this code return null
to not there Anther way to Access to Tenant from code like
search and find tenant by name or id return TenantContext and access on it direct
@Sheko007 I see what you mean. Using the host strategy on a dev machine is tricky. You will have to edit your systems “hosts” file and point the ‘aaa.localhost’ to IP address 127.0.0.1. The steps vary by operating system but if you google it you will get some good search results.
However, if you for some reason can't do that, you can get the multitenant store from dependency injection and call GetByIdentifierAsync
on it to get the tenant context. Let me know if you need a code sample for how to do that.
I don't know if this is still an issue, I had the same issue. I solved it using a custom IMultiTenantStrategy
public class TenantStrategy : IMultiTenantStrategy
{
public string GetIdentifier(object context)
{
if (!(context is HttpContext))
throw new MultiTenantException(null,
new ArgumentException("\"context\" type must be of type HttpContext", nameof(context)));
var host = (context as HttpContext).Request.Host;
return host.Host;
}
}
and a custom IMultiTenantStore
with the following check: My config.Identifier is a full host RegExp.
public Task<TenantContext> GetByIdentifierAsync(string identifier)
{
if (!hostConfigurations.TryGetValue(identifier, out TenantContext context))
{
foreach (var testConfig in tenantConfigurations)
{
if (testConfig.Identifier == "*")
{
hostConfigurations[identifier] = context = GetContextFromConfig(testConfig);
break;
}
try
{
Regex re = new Regex(testConfig.Identifier, RegexOptions.IgnoreCase);
if (re.IsMatch(identifier))
{
hostConfigurations[identifier] = context = GetContextFromConfig(testConfig);
break;
}
}
catch { }
}
}
return Task.FromResult(context);
}
Configured in startup as:
services.AddSingleton<IMultiTenantStore>((serviceProvider) =>
{
var cfg = serviceProvider.GetService<List<TenantConfiguration>>();
return new TenantResolver(cfg.ToArray());
});
services.AddMultiTenant()
.WithStrategy<TenantStrategy>()
.AddPerTenantCookieAuthentication();
@AndreSteenbergen interesting solution. Testing anything based on subdomains is fundamentally difficult in local development. I found it easier in macOS and Linux (just editing the hosts file) than in Windows (because IIS Express requires more configuration to get it to work).
I just found a site that might help. Haven't tested it myself, but I will be: http://readme.localtest.me/
there is an etc hosts file in windows as well, it's in the c:\windows\system32\drivers\etc\hosts this is twe way I am testing all subdomains locally.
how to access to Tenant by Code