Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to access to Tenant by Code #62

Closed
Sheko007 opened this issue Jul 26, 2018 · 6 comments
Closed

how to access to Tenant by Code #62

Sheko007 opened this issue Jul 26, 2018 · 6 comments
Labels

Comments

@Sheko007
Copy link

how to access to Tenant by Code

@AndrewTriesToCode
Copy link
Sponsor Contributor

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.

@Sheko007
Copy link
Author

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

@AndrewTriesToCode
Copy link
Sponsor Contributor

AndrewTriesToCode commented Jul 28, 2018

@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.

@AndreSteenbergen
Copy link

AndreSteenbergen commented Oct 9, 2018

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();

@AndrewTriesToCode
Copy link
Sponsor Contributor

@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/

@AndreSteenbergen
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants