Error when trying to add a new user

I am logged in as main admin and want to create users ?
A role was provided and was definitely not null.
See attached screenshot.
Thanks

Hi @Duel,

Can you reproduce the same in this app?

Here is what I see when logged with tenantsadmin/tenantsadmin:


I was able to reproduce such problem if there is no https://localhost:5001/ in tenant Host which is needed in development - you can add multiple hosts separated with coma. We will check on how to improve this template to not need this.

UPDATE: Here is how to change AddToRoleAsync method:

public override async Task AddToRoleAsync(ApplicationUser user, string normalizedRoleName, CancellationToken cancellationToken = default)
{
    if (user.NormalizedUserName.ToLower() == "tenantsadmin")
    {
        await base.AddToRoleAsync(user, normalizedRoleName, cancellationToken);
    }

    var tenant = user.ApplicationTenant ?? GetTenant();
    ApplicationRole role = null;

    if (tenant != null)
    {
        role = await Context.Set<ApplicationRole>().SingleOrDefaultAsync(r => r.NormalizedName == normalizedRoleName && r.TenantId == tenant.Id, cancellationToken);
    }

    Context.Set<IdentityUserRole<string>>().Add(new IdentityUserRole<string>
    {
        RoleId = role.Id,
        UserId = user.Id
    });

    await Context.SaveChangesAsync();
}
1 Like