Finbuckle / Finbuckle.MultiTenant

Finbuckle.MultiTenant is an open-source multitenancy middleware library for .NET. It enables tenant resolution, per-tenant app behavior, and per-tenant data isolation.
https://www.finbuckle.com/multitenant
Apache License 2.0
1.32k stars 267 forks source link

Getting Tenant name in Blazor page without IHttpContextAccessor #330

Closed skeww closed 4 years ago

skeww commented 4 years ago

I found this page issue https://github.com/Finbuckle/Finbuckle.MultiTenant/issues/188

However, a document that went up later (May 2020) on Microsoft docs (https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-3.1) says:

"Additionally, again for security reasons, you must not use IHttpContextAccessor within Blazor apps. Blazor apps run outside of the context of the ASP.NET Core pipeline. The HttpContext isn't guaranteed to be available within the IHttpContextAccessor, nor is it guaranteed to be holding the context that started the Blazor app."

I have implemented the IHttpContextAccessor solution, but sometimes I get a null reference and can't get the tenant name.

Has anyone found a workaround for getting the tenant info in a blazor page?

AndrewTriesToCode commented 4 years ago

Are you using 6.x? You should be able to get the tenant info straight out of DI. If you have MyTenantInfo as the type for your tenant class you can inject or resolve from DI directly.

You can also use IMultiTenantContextAccessor<T> where T is your custom info type. This should work well in Blazor, but let me know if you still have the issue.

skeww commented 4 years ago

Yes, I am using 6. I ended up creating a tenant service with a reference to my tenant store. It's working (I can get a tenant list and the current tenant from the host name.)

AndrewTriesToCode commented 4 years ago

@skeww Excellent, sounds like you have a nice solution there!

gocampo commented 9 months ago

Are you using 6.x? You should be able to get the tenant info straight out of DI. If you have MyTenantInfo as the type for your tenant class you can inject or resolve from DI directly.

You can also use IMultiTenantContextAccessor<T> where T is your custom info type. This should work well in Blazor, but let me know if you still have the issue.

Hi @AndrewTriesToCode !! what do you mean by "you can inject or resolve from DI directly"? All I can found in samples and everywhere is through HttpContext.GetMultiTenantContext.