I have done it like this: Reference Code below
public virtual async Task<SaasTenantDto> CreateSiteTenanatAsync(SaasTenantCreateDto input)
{
input.ConnectionStrings = await NormalizedConnectionStringsAsync(input.ConnectionStrings);
Tenant tenant = null;
async Task CreateTenantAsync()
{
tenant = await TenantManager.CreateAsync(input.Name, input.EditionId);
if (!input.ConnectionStrings.Default.IsNullOrWhiteSpace())
{
tenant.SetDefaultConnectionString(input.ConnectionStrings.Default);
}
if (input.ConnectionStrings.Databases != null)
{
foreach (var database in input.ConnectionStrings.Databases)
{
tenant.SetConnectionString(database.DatabaseName, database.ConnectionString);
}
}
await CheckConnectionStringAsync(tenant);
input.MapExtraPropertiesTo(tenant);
tenant.SetActivationState(input.ActivationState);
if (tenant.ActivationState == TenantActivationState.ActiveWithLimitedTime)
{
tenant.SetActivationEndDate(input.ActivationEndDate);
}
/* Auto saving to ensure TenantCreatedEto handler can get the tenant! */
await TenantRepository.InsertAsync(tenant, autoSave: true);
}
if (input.ConnectionStrings.Default.IsNullOrWhiteSpace() &&
input.ConnectionStrings.Databases.IsNullOrEmpty())
{
/* Creating the tenant in the current UOW */
await CreateTenantAsync();
}
else
{
/* Creating the tenant in a separate UOW to ensure it is created
* before creating the database.
* TODO: We should remove inner UOW once https://github.com/abpframework/abp/issues/6126 is done
*/
using (var uow = UnitOfWorkManager.Begin(requiresNew: true))
{
await CreateTenantAsync();
await uow.CompleteAsync();
}
}
var adminName = "admin"; // default
if (input.ExtraProperties.TryGetValue("adminName", out var nameObj) && nameObj is string nameStr && !string.IsNullOrWhiteSpace(nameStr))
{
adminName = nameStr;
}
await DistributedEventBus.PublishAsync(
new TenantCreatedEto
{
Id = tenant.Id,
Name = tenant.Name,
Properties =
{
{"AdminEmail", input.AdminEmailAddress},
{"AdminPassword", input.AdminPassword},
** { "AdminName", adminName}** Added this line still not working
}
}
);
return ObjectMapper.Map<Tenant, SaasTenantDto>(tenant);
}
2 Answer(s)
-
0
To change the default admin username for a new tenant in ABP Framework (so it is not always "admin"), you need to use the new feature introduced in ABP 8.3. This allows you to set the admin username via the DataSeeder by passing the "AdminUserName" property when seeding the tenant.
Your current approach of adding "AdminName" to the event properties will not work, because the ABP seeding logic specifically looks for the "AdminUserName" property.
Here’s how you should do it:
- When seeding the tenant (typically in your migration or seeding logic), pass the "AdminUserName" property:
await _dataSeeder.SeedAsync( new DataSeedContext(tenant.Id) .WithProperty(IdentityDataSeedContributor.AdminEmailPropertyName, input.AdminEmailAddress) .WithProperty(IdentityDataSeedContributor.AdminPasswordPropertyName, input.AdminPassword) .WithProperty(IdentityDataSeedContributor.AdminUserNamePropertyName, adminName) // <-- Set your custom admin username here );
Make sure you are using ABP Framework 8.3 or later, as this feature is only available from that version.
Do not use "AdminName" or "AdminUserName" in the event bus properties unless your own custom event handler is reading it. The official ABP seeding logic only reads from the DataSeedContext properties.
For more details, see the official migration guide:
- https://abp.io/docs/latest/release-info/migration-guides/abp-8-3
- https://abp.io/support/questions/8116
This will ensure the admin user for each tenant is created with the username you specify, not the default "admin".
Sources:
- https://abp.io/docs/latest/release-info/migration-guides/abp-8-3
- https://abp.io/support/questions/8116
This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.
-
0
hi
{ "AdminName", adminName} Added this line still not working
There is a
MyProjectNameTenantDatabaseMigrationHandler
class which implements theIDistributedEventHandler<TenantCreatedEto>
You can change its code to get your
AdminName
and set it toIdentityDataSeedContributor.AdminUserNamePropertyName
Thanks.