Invalid Tenancy Name

2020-06-06 07:37发布

问题:

I am working on an ASP.NET Boilerplate service project.

When I am saving a client, it returns an error:

Tenancy Name is not valid

The tenancy name contains spaces. TenantDto maps to Tenant object without any error. Database table TenancyName column is nvarchar(64). Error occurs when it is saving.

回答1:

From the documentation on Tenant Management:

AbpTenant class defines some base properties, most important ones are:

  • TenancyName: This is unique name of a tenant in the application. It should not be changed normally. It can be used to allocate subdomains to tenants like 'mytenant.mydomain.com'. Tenant.TenancyNameRegex constant defines the naming rule.
  • Name: An arbitrary, human-readable, long name of the tenant.

TenancyNameRegex is "^[a-zA-Z][a-zA-Z0-9_-]{1,}$" as spaces are not allowed in subdomains.

As quoted above, use Name for the human-readable name (with spaces) of the tenant.



回答2:

Before saving a tenant, it's being validated against TenancyNameRegex regular expression. Thus a tenant name cannot contain space (by design). Do not remove the regex check, but add a client-side validation to check the tenant name.

protected virtual Task ValidateTenancyNameAsync(string tenancyName)
{
    if (!Regex.IsMatch(tenancyName, AbpTenant<TUser>.TenancyNameRegex))
    {
         throw new UserFriendlyException(L("InvalidTenancyName"));
    }

    return Task.FromResult(0);
}

See the code => https://github.com/aspnetboilerplate/aspnetboilerplate/blob/45fe6d9f38b79ab111eaf2a54b507b87c92e544e/src/Abp.Zero.Common/MultiTenancy/AbpTenantManager.cs#L222