Search code examples
asp.net-core-mvcasp.net-core-identityasp.net-core-2.2

How to make Identity use of MVC instead of razor pages in asp.net core 2.2


Since asp.net core 2.1 the MVC is not supported as default model with Identity. I want to run Identity with MVC in asp.net core 2.2

Make a long story short. This is a asp.net 4.6.2 site with local db that I want upgrade to asp.net core 2.2 . I thought that this would be simple thing. So wrong I was. So what did I do:

  1. Created an new ASP.net core 2.0 project with local Identity
  2. Scaffolded the ef context with database first with Scaffold-DbContext
  3. Fixed so EF is loaded correctly in Startup.
  4. Removed aspnetusers, aspnetuserroles, aspnetuserlogin, aspnetroles models from generated scaffold by the Scaffold-Dbcontext
  5. Updated ApplicationDbContext to inherits from IdentityDbContext. ApplicationUser inherits from IdentityUser
  6. After I faked that the migration have been applied, I manually added following columns in aspnetusers

6.1 ConcurrencyStamp

6.2 NormalizedUserName

6.3 NormalizedEmail

6.4 Changed name LockoutEnd? to LockoutEnd

The system builds but still I cant get it to use the Account controller. I have followed the steps in https://learn.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-2.2 without no avail. It seems what I do I can't get it to read the mvc controller even after I scaffolded the Identity.

I think the difference in implementations of Identity in asp.net core 2.X Identity is either to high or to narrow regarding earlier implementations.

Included part of my startup

       services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddIdentity<ApplicationUser, IdentityRole>()
            // services.AddDefaultIdentity<IdentityUser>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
            .AddRazorPagesOptions(options =>
            {
                options.AllowAreas = true;
                //options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Manage");
                //options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
            });

        services.ConfigureApplicationCookie(options =>
        {
            options.LoginPath = $"/Account/Login";
            options.LogoutPath = $"/Account/Logout";
            options.AccessDeniedPath = $"/Account/AccessDenied";
        });

        // using Microsoft.AspNetCore.Identity.UI.Services;
        services.AddSingleton<IEmailSender, EmailSender>();
    }

I want to use MVC with Identity Core 2.2 with an old database from Identity .Net.

So any suggestions?


Solution

  • It's not clear what you're doing exactly, but it sounds like you've scaffolded the Identity Razor Pages into your project. If that's the case, those are going to take precedent over your controller, since it's a more physical route.

    All the other code looks good. You're using AddIdentity instead of AddDefaultIdentity (so the default UI is not included). Assuming you've actually created an AccountController and your routing is set up correctly, then it should work, as long as you don't have the default UI physically scaffolded into your project.