0
oguzhanacikgoez created
Hi,
at first i have to say "Great Work!" to Halil. You have built the most genius architecture I have seen before, thank you for that.
In my application I created multiple tenants for my customers. Currently they're log-in with the default form and need to put the tenant name in the input field.
For a better user experience I target to detect the tenant by requested URL.
customer1.myapp.com -> runs the application with the customer1 database
How could i solve this issue, any idea?
1 Answer(s)
-
0
Hi,
First of all thanks :).
You need to create a new session of yours like this.
public class AspNetZeroAbpSession : ClaimsAbpSession { public ITenantIdAccessor TenantIdAccessor { get; set; } public override int? TenantId { get { if (base.TenantId != null) { return base.TenantId; } //Try to find tenant from ITenantIdAccessor, if provided return TenantIdAccessor?.GetCurrentTenantIdOrNull(false); //set false to prevent circular usage. } } public AspNetZeroAbpSession(IMultiTenancyConfig multiTenancy) : base(multiTenancy) { } }
public class TenantIdAccessor : ITenantIdAccessor, ISingletonDependency { private const string HttpContextKey = "AbpZeroCurrentTenantCacheItem"; private readonly ITenantCache _tenantCache; private readonly IIocResolver _iocResolver; private readonly AsyncLocal _currentTenant; private readonly Lazy _abpSession; private readonly Lazy _tenancyNameFinder; private int? CurrentTenantId { get { if (HttpContext.Current != null) { _currentTenant.Value = HttpContext.Current.Items[HttpContextKey] as int?; return _currentTenant.Value; } return _currentTenant.Value; } set { _currentTenant.Value = value; if (HttpContext.Current != null) { HttpContext.Current.Items[HttpContextKey] = _currentTenant.Value; } } } public TenantIdAccessor( ITenantCache tenantCache, IIocResolver iocResolver) { _tenantCache = tenantCache; _iocResolver = iocResolver; _currentTenant = new AsyncLocal(); _abpSession = new Lazy(() => _iocResolver.Resolve(), true); _tenancyNameFinder = new Lazy(() => _iocResolver.Resolve(), true); } ///
/// Gets current tenant id. /// Use wherever possible (if user logged in). /// This method tries to get current tenant id even if current user did not log in. /// /// Set false to skip session usage public int? GetCurrentTenantIdOrNull(bool useSession = true) { if (useSession) { return _abpSession.Value.TenantId; } return CurrentTenantId; } ////// This method is called on request beginning to obtain current tenant id. /// public void SetCurrentTenantId() { var tenancyName = _tenancyNameFinder.Value.GetCurrentTenancyNameOrNull(); if (tenancyName == null) { CurrentTenantId = null; return; } CurrentTenantId = _tenantCache.GetOrNull(tenancyName)?.Id; } }Then replace IAbpSession with your session on your core module's PreInitialize method.
Configuration.ReplaceService();
Finally, use below code in your Global.asax's Application_AuthenticateRequest.
if (User?.Identity != null && User.Identity.IsAuthenticated) { return; } using (var currentTenantAccessor = AbpBootstrapper.IocManager.ResolveAsDisposable()) { currentTenantAccessor.Object.SetCurrentTenantId(); }