Giter Site home page Giter Site logo

Comments (6)

AndrewTriesToCode avatar AndrewTriesToCode commented on May 18, 2024

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.

from finbuckle.multitenant.

Sheko007 avatar Sheko007 commented on May 18, 2024

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

from finbuckle.multitenant.

AndrewTriesToCode avatar AndrewTriesToCode commented on May 18, 2024

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

from finbuckle.multitenant.

AndreSteenbergen avatar AndreSteenbergen commented on May 18, 2024

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

from finbuckle.multitenant.

AndrewTriesToCode avatar AndrewTriesToCode commented on May 18, 2024

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

from finbuckle.multitenant.

AndreSteenbergen avatar AndreSteenbergen commented on May 18, 2024

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.

from finbuckle.multitenant.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.