Skip to content

Blazor Identity scaffolds Identity into the wrong folder when a db context already exists #3716

@guardrex

Description

@guardrex

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Blazor Identity scaffolds Identity into the wrong folder when db context already exists

Scaffolding Blazor Identity into a project with an existing context adds an {APP NAMESPACE} > Components folder using the app's name. The folder contains its own Components folder, which contains the Account folder, which contains the Identity assets ...

Image

This is incorrect. The Account folder should be placed in the app's existing Components folder. Because the Identity components are placed the way they are, there are 174 type and namespace violations. This is because the namespaces don't have a double app name in them. The scaffolder correctly makes the namespaces to Account assets with a single BlazorWebAppMovies namespace segment. For example, the Razor components (Pages assets) for Identity are correctly referenced as ...

using BlazorWebAppMovies.Components.Account.Pages;

Also, scaffolding creates an Account > Shared folder in the app's existing Components folder, which is correct, and places just one file in it ... PasskeySubmit.razor.js, but the Razor component that would go with the file (PasskeySubmit.razor) is in the incorrectly-added BlazorWebAppMovies > BlazorWebAppMovies > Components > Account > Shared folder.

The same thing happens if you try to scaffold Identity using a new db context into this sample app.

Expected Behavior

The Account folder should be placed in the app's existing Components folder.

Steps To Reproduce

I'm beginning this process with the Blazor movie dB sample app, which has an existing context ...

The purpose of this is to use the movie dB sample app as the starter app for a different tutorial on using authz in ASP.NET Core at https://learn.microsoft.com/aspnet/core/security/authorization/secure-data. The current article is based on Razor Pages, and we want the article to be based on Blazor.

I used the following steps. The first three steps are to get the existing db context set up to inherit from IdentityDbContext, which is required for scaffolding using an existing context. However, the same outcome occurs if I create a new database context just for Identity.

  1. Install NuGet package: Microsoft.AspNetCore.Identity.EntityFrameworkCore

  2. Add an application user.

    Data/ApplicationUser.cs:

    using Microsoft.AspNetCore.Identity;
    
    namespace BlazorWebAppMovies.Data;
    
    public class ApplicationUser : IdentityUser
    {
        // Add custom user properties here
    }
  3. Change BlazorWebAppMoviesContext to inherit from IdentityDbContext<ApplicationUser>:

    - public class BlazorWebAppMoviesContext : DbContext
    + public class BlazorWebAppMoviesContext : IdentityDbContext<ApplicationUser>
  4. In Solution Explorer, right-click the project and select Add > New Scaffolded Item.

  5. In the Add New Scaffolded Item dialog, select Identity > Blazor Identity. Select the Add button.

  6. In the Add Blazor Identity dialog, select BlazorWebAppMoviesContext for the DbContext class. Select the Add button to scaffold Identity into the app. Wait until the scaffolding process is complete.

Exceptions (if any)

None

.NET Version

10.0.201

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions