Open Closed

How to login as host when sub-domain multi-tenancy is enabled? #9276


0
compassinformatics17 created

Hi Guys, This one seems like it should be easy but I cannot seem to find any reference to it in the docs.

I have enabled sub-domain wildcard multi-tenancy and it works just fine as specifed in the docs.

But when it is enabled, how do I then log in as the host user? If I go to the root domain sitename.com, it just defaults to the default tenant. Is there a way to configure a url for the host such as admin.sitename.com?

Thanks, Barry.


3 Answer(s)
  • 0
    maliming created
    Support Team

    If I go to the root domain sitename.com, it just defaults to the default tenant.

    If I remember correctly, sitename.com will resolve to host.

    If you want to use admin.sitename.com to resolve to host, you can customize the logic of DomainTenantResolveContributor. eg www.

    https://github.com/aspnetboilerplate/aspnetboilerplate/blob/e0ded5d8702f389aa1f5947d3446f16aec845287/src/Abp.AspNetCore/AspNetCore/MultiTenancy/DomainTenantResolveContributor.cs#L41-L66

  • 0
    compassinformatics17 created

    Brilliant thanks and apologies for the late reply, was on other projects.

    For anyone trying to do the same see the step by step below:

    1. Create a custom DomainTenantResolver. I have stored this in the root of the Web.Core project.
    using System;
    using System.Linq;
    using Abp.Dependency;
    using Abp.Extensions;
    using Abp.MultiTenancy;
    using Abp.Text;
    using Abp.Web.MultiTenancy;
    using Microsoft.AspNetCore.Http;
    
    namespace MyProject.Web
    {
        public class CustomDomainTenantResolveContributor : ITenantResolveContributor, ITransientDependency
        {
            private readonly IHttpContextAccessor _httpContextAccessor;
            private readonly IWebMultiTenancyConfiguration _multiTenancyConfiguration;
            private readonly ITenantStore _tenantStore;
    
            public CustomDomainTenantResolveContributor(
                IHttpContextAccessor httpContextAccessor,
                IWebMultiTenancyConfiguration multiTenancyConfiguration,
                ITenantStore tenantStore)
            {
                _httpContextAccessor = httpContextAccessor;
                _multiTenancyConfiguration = multiTenancyConfiguration;
                _tenantStore = tenantStore;
            }
    
            public int? ResolveTenantId()
            {
                if (_multiTenancyConfiguration.DomainFormat.IsNullOrEmpty())
                {
                    return null;
                }
    
                var httpContext = _httpContextAccessor.HttpContext;
                if (httpContext == null)
                {
                    return null;
                }
    
                var hostName = httpContext.Request.Host.Host.RemovePreFix("http://", "https://").RemovePostFix("/");
                var domainFormat = _multiTenancyConfiguration.DomainFormat.RemovePreFix("http://", "https://").Split(':')[0].RemovePostFix("/");
                var result = new FormattedStringValueExtracter().Extract(hostName, domainFormat, true, '/');
    
                if (!result.IsMatch || !result.Matches.Any())
                {
                    return null;
                }
    
                var tenancyName = result.Matches[0].Value;
                if (tenancyName.IsNullOrEmpty())
                {
                    return null;
                }
    
                if (string.Equals(tenancyName, "www", StringComparison.OrdinalIgnoreCase))
                {
                    return null;
                }
    
                // Allow the use of admin.domain.com as the host url.
                if (string.Equals(tenancyName, "admin", StringComparison.OrdinalIgnoreCase))
                {
                    return null;
                }
    
                var tenantInfo = _tenantStore.Find(tenancyName);
                if (tenantInfo == null)
                {
                    return null;
                }
    
                return tenantInfo.Id;
            }
        }
    }
    
    1. Add your custom resolver to MyProjectWebCoreModule.cs in the Web.Core project.

       public override void PreInitialize()
       {
           ...previous code
      
           Configuration.MultiTenancy.Resolvers.Insert(0, typeof(CustomDomainTenantResolveContributor));
      
           ...any additional code
       }
      
    2. Finally set up sub-domain bindings and dns entries in your hosting config.

    Thanks, Barry.

  • 0
    marble68 created

    Thanks for this - but any idea why it causes all the tests to fail?