JonPSmith / AuthPermissions.AspNetCore

This library provides extra authorization and multi-tenant features to an ASP.NET Core application.
https://www.thereformedprogrammer.net/finally-a-library-that-improves-role-authorization-in-asp-net-core/
MIT License
764 stars 155 forks source link

`AddSuperUserToIndividualAccounts` doesn't seem to be working #109

Closed pradeepgururani closed 2 months ago

pradeepgururani commented 2 months ago

We are using v 6.2.0 of library and calling .AddSuperUserToIndividualAccounts() to seed Super user.

However it doesn't seem to be working. It does create a user however leaves out creating AuthP user and any association with AccessAll permission. My conclusion is based on following code from StartupServiceIndividualAccountsAddSuperUser.cs:

private static async Task CheckAddNewUserAsync(UserManager<TIdentityUser> userManager, string email, string password)
        {
            var user = await userManager.FindByEmailAsync(email);
            if (user != null)
                return;

            user = new TIdentityUser { UserName = email, Email = email };
            var result = await userManager.CreateAsync(user, password);
            if (!result.Succeeded)
            {
                var errorDescriptions = string.Join("\n", result.Errors.Select(x => x.Description));
                throw new InvalidOperationException(
                    $"Tried to add user {email}, but failed. Errors:\n {errorDescriptions}");
            }
        }

On database I can see that super user is created in AspNetUsers table but not in AuthUsers.

Please guide.

JonPSmith commented 2 months ago

Hi @pradeepgururani,

There is a section in the AuthP that covers this - see How to set up a super user when deploying a new application.

As you will see you need to use both the ASP.NET Core user and the Bulk Load feature.