Comments (6)
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.
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.
@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.
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.
@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.
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)
- Exception after ASP.NET Core app migration from .net7 -> .net8 HOT 11
- adjustindex api surface interaction with model class inheritance HOT 2
- NullReferenceException while trying to register a user using the default Identity API HOT 2
- .net8.0 - NullReferenceException while using the UserManager.Users HOT 4
- request for enhancement to IMultiTenantStore<TTenantInfo> Task<IQueryable<TTenantInfo>> GetAllAsync()
- Amateur question related to migration HOT 2
- Can't add migration after updating to dotnet 8 HOT 2
- Mocking StoreInfo? HOT 2
- why MultiTenantDbContext requires ITenantInfo not just current TenantId ? HOT 10
- ArgumentException: Options.ClientId must be provided (Parameter 'ClientId') HOT 7
- TenantInfo being Null in integration test under WebApplicationFactory HOT 7
- [Question] MultiTenancy without stores? HOT 5
- I can't find the reason HOT 1
- How to use TenantInfo in a background job? HOT 4
- Mongo EF Core Provider The given key '__ef_filter__Id_0' was not present in the dictionary HOT 6
- Identity Sample app, from Samples is not working HOT 2
- The documentation still refers to the ConnectionString property HOT 2
- MultiTenantDbContext now throws when saving non multitenant entities HOT 1
- [Question] MultiTenantContext now readonly HOT 7
- Separate Databases ConnectionString with v7.0 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from finbuckle.multitenant.