Search code examples
.net-coremodel-view-controllerasp.net-identity.net-8.0

class with No Name(Empty name) is added in Area/Identity/Data folder ,when add identity using scaffolding identity in project using .net 8.0 and VS22?


I am created a new .Net core web application using vs 2022 and .NET 8.0 Framework. Project created successfully but when i try to add Asp.net core identity using scaffolding identity. I add dbcontext name and 'MyApplicationUser' in User Class section ,then click on Add button. DBcontext class file is added successfully in Area/Identity/Data folder but a class with no name(User Class) is added in this folder.I think the problem is in code-Generator.

Thanks

First Image

Second Image


Solution

  • Although I haven't found a direct solution, I figured out a workaround that achieves the same result.

    1. During project creation when asked for Authentication Type change it from None to Individual Accounts Authentication_Type_UI

    2. Make a file for the ApplicationUser file, and make it inherit from IdentityUser (This file can be put anywhere within the project, I put it in the Data Folder with the ApplicationDbContext) ApplicationUser_Folder_Structure

    using Microsoft.AspNetCore.Identity;
    
    namespace ScaffoldFixExample.Data
    {
        public class ApplicationUser : IdentityUser
        {
        }
    }
    
    1. In the ApplicationDbContext.cs file, change the following line from

      public class ApplicationDbContext : IdentityDbContext

      to

      public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

    2. In the Program.cs file, change the following line from

      builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)

      to

      builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)

    3. In the _LoginPartial.cshtml in /Views/Shared, change the beginning from

    @using Microsoft.AspNetCore.Identity
    @inject SignInManager<IdentityUser> SignInManager
    @inject UserManager<IdentityUser> UserManager
    

    to

    @using Microsoft.AspNetCore.Identity
    @using ScaffoldFixExample.Data
    @inject SignInManager<ApplicationUser> SignInManager
    @inject UserManager<ApplicationUser> UserManager
    

    Note: Replace ScaffoldFixExample with your projects/solutions name you set during creation (root folder name)

    1. Create a new scaffold identity as you would normally - Right click project/root folder - Add - New Scaffold Item - Identity
    2. Select which files you want (or click Override all files)
    3. Set DbContextClass to ApplicationDbContext
    4. Click Add

    Identity Example

    1. Once complete, open up the Package Manager Console - View -> Other Windows -> Package Manager Console
    2. Run update-database
    3. Once update database has finished, simply run the application and it should work properly (ASP.NET should still work, along with having support for register/login with the custom ApplicationUser)

    Now the custom ApplicationUser is all setup, and can be used as you normally would.


    Note: This does result in a slightly different folder structure than the method that is commonly used/used in the question.

    To make it look/be structured the same do the following,

    1. Move the Migrations folder into the root folder

    2. Move the Data folder into the /Area/Identity/ folder.

    3. Update the using in _LoginPartial.cshtml from @using <Project>.Data to @using <Project>.Areas.Identity.Data

    4. Add in the @using <Project>.Areas.Identity.Data in the file /Areas/Identity/Pages/Account/Manage/_ManageNav.cshtml

    Before

    Before

    After

    After