Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core 2.1 Identity: How to remove the Default UI razor pages?

Expanding the answer in this question: Change routing in ASP.NET Core Identity UI?

Javier recommends one of the following options when wanting to customise the URLs:

  • Use the scaffolding element of the Default UI and make all necessary customisations yourself.
  • Use a redirection rule that points the old routes to the new routes.
  • Don't use the Default UI at all.

From a new ASP.NET Core 2.1 MVC project, with Authentication: Individual User Accounts set, how do you NOT use the Default UI? It seems to be installed by default with Identity Core.

enter image description here

After the project is created, what is the method to remove the Default UI razor pages, and still use Identity Core?

Can I just delete the /Identity/ area, and create my own AccountController instead?

like image 225
Daniel Congrove Avatar asked Jul 11 '18 15:07

Daniel Congrove


People also ask

How do you scaffold identity pages?

From Solution Explorer, right-click on the project > Add > New Scaffolded Item. From the left pane of the Add Scaffold dialog, select Identity > Add. In the Add Identity dialog, select the options you want. Select your existing layout page so your layout file isn't overwritten with incorrect markup.

Do I need IdentityServer4?

Why do we need IdentityServer4? ASP.NET Identity can receive a security token from a third-party login provider like Facebook, Google, Microsoft and Twitter. But If you want to issue a security token for a local ASP.NET Identity user you need to work with a third-party library like IdentityServer4, OpenIddict.

What is razor pages in ASP.NET Core?

Razor Pages is a newer, simplified web application programming model. It removes much of the ceremony of ASP.NET MVC by adopting a file-based routing approach. Each Razor Pages file found under the Pages directory equates to an endpoint.

What is Microsoft ASP.NET Core identity UI?

ASP.NET Core Identity UI is the default Razor Pages built-in UI for the ASP.NET Core Identity framework. This package was built from the source code at https://github.com/dotnet/aspnetcore/tree/bb01bbf4433e27289b99001b7de6a582879d1835. Product. Versions. .NET.


2 Answers

Using the article linked by Panagiotis Kanavos, I was able to reach a solution.

From the ASP.NET Core 2.1.0-preview1, there was a line .AddDefaultUI(), which you didn't have to include in Startup.cs.

services.AddIdentity<IdentityUser, IdentityRole>(options => options.Stores.MaxLengthForKeys = 128)     .AddEntityFrameworkStores<ApplicationDbContext>()     .AddDefaultUI()     .AddDefaultTokenProviders(); 

In the final release version of Core 2.1 however, the same section was simplified to:

services.AddDefaultIdentity<IdentityUser>()     .AddEntityFrameworkStores<ApplicationDbContext>(); 

The solution, if you change AddDefaultIdentity back to AddIdentity, you can override the defaults. I.E. don't include .AddDefaultUI() (and also don't scaffold the UI) and you can write your own.

services.AddIdentity<IdentityUser, IdentityRole>(options => options.Stores.MaxLengthForKeys = 128)     .AddEntityFrameworkStores<ApplicationDbContext>()     // .AddDefaultUI()     .AddDefaultTokenProviders(); 

Then, I think it's safe to delete the /Areas/Identity/ folder, but I'm not 100%

Update:

I cleaned up my answer to detail the final solution I ended up going with, to remove the default identity UI razor pages that come with ASP.NET Core 2.1 and and use MVC instead.

1) In Startup.cs,

    public void ConfigureServices(IServiceCollection services)     {         // Unrelated stuff commented out...          // BEGIN: Identity Setup (Overrides default identity)         services.AddIdentity<ApplicationUser, IdentityRole>(options => options.Stores.MaxLengthForKeys = 128)             .AddEntityFrameworkStores<ApplicationDbContext>()             .AddDefaultTokenProviders();         // END: Identity Setup          services.Configure<IdentityOptions>(options =>         {             // Set your identity Settings here (password length, etc.)         });          // More unrelated stuff commented out...          services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);          // Added after AddMvc()         services.ConfigureApplicationCookie(options =>         {             options.LoginPath = $"/account/login";             options.LogoutPath = $"/account/logout";             options.AccessDeniedPath = $"/account/access-denied";         });          // More unrelated stuff commented out...     } 

And obviously, replace both ApplicationUser, and IdentityRole with your own classes if required.

2) Delete the Area folder for Identity that came default with your ASP.NET Core 2.1 project.

3) Create a new separate ASP.NET Core 2.0 project (not "2.1"), with Individual User Account authentication selected in the project creation window.

4) Copy the AccountController and ManageController, with the corresponding ViewModels and Views, from the 2.0 project to your ASP.NET Core 2.1 project.

Doing the above, I haven't run into any issues so far.

like image 103
Daniel Congrove Avatar answered Nov 01 '22 07:11

Daniel Congrove


A little late, but there is a much easier way to do it. You can add new scaffolding to override everything. Check out this article.

like image 26
hivie7510 Avatar answered Nov 01 '22 08:11

hivie7510