Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

SET SUBDOMAIN FOR HOST TENANT #5560


User avatar
0
luqc1985 created

hi , I'm trying to use the subdomain as Tenant approach. I updated my appconfig to use the https://{TENANCY_NAME}.domain.com/.

When the tenant is already registered, it will locate the tenant login page. If it is not registered, it will locate the host login page. I want to go to the Host login page only when I enter admin.domain.com. Other unregistered tenants are located. 404 page. What should I do?

Thanks in advance!


8 Answer(s)
  • User Avatar
    0
    alper created
    Support Team

    hi,

    for your subdomain issues you can create your own DomainTenantResolveContributor

    #5482@5506404d-9f34-4a1a-a88f-6c5d1da6fbbe

  • User Avatar
    0
    luqc1985 created

    <cite>alper: </cite> hi,

    for your subdomain issues you can create your own DomainTenantResolveContributor

    #5482@5506404d-9f34-4a1a-a88f-6c5d1da6fbbe

    public class MyDomainTenantResolveContributor : ITenantResolveContributor, ITransientDependency
    	{
    		private readonly IHttpContextAccessor _httpContextAccessor;
    		private readonly IWebMultiTenancyConfiguration _multiTenancyConfiguration;
    		private readonly ITenantStore _tenantStore;
    
    		public MyDomainTenantResolveContributor(
    			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 (string.Equals(tenancyName, "admin", StringComparison.OrdinalIgnoreCase))
    			{
    				return null;
    			}
    
    			var tenantInfo = _tenantStore.Find(tenancyName);
    			if (tenantInfo == null)
    			{
    				throw new UserFriendlyException("404", "404Test");
    			}
    
    			return tenantInfo.Id;
    		}
    	}
    
    public override void PreInitialize()
            {
    			IocManager.Register<ITenantResolveContributor, MyDomainTenantResolveContributor>(DependencyLifeStyle.Transient);
    }
    

    Like this? But it doesn't seem to be enforced...

  • User Avatar
    0
    maliming created
    Support Team

    add it to resolvers in the PreInitalize method of your web module

    Configuration.MultiTenancy.Resolvers.Insert(0,new MyDomainTenantResolveContributor());
    
  • User Avatar
    0
    luqc1985 created

    <cite>maliming: </cite> add it to resolvers in the PreInitalize method of your web module

    Configuration.MultiTenancy.Resolvers.Insert(0,new MyDomainTenantResolveContributor());
    
    public override void PreInitialize()
            {
    			Configuration.MultiTenancy.Resolvers.Insert(0, typeof(MyDomainTenantResolveContributor));
    }
    

    It's ready to go, thank you very much

  • User Avatar
    0
    alper created
    Support Team

    super!

  • User Avatar
    0
    michelmk2 created

    @luqc1985 Does switching back to the host (using the "Back to my account" button) from any other tenants works too?

  • User Avatar
    0
    luqc1985 created

    @michelmk2 I didn't notice , that I just tried it, it is also 404. Back to my account also needs to modify the corresponding link. There is no tenant information in the abp object, only appPath is found.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Latest problem is related to https://github.com/aspnetzero/aspnet-zero-core/issues/1609.

    Please reopen the issue if it doesn't help.