1
votes

I have looked at this guide on how to disable registration template in ASP.NET Core:

https://stackoverflow.com/a/58852405/3850405

It is basically a reference to this article:

https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-5.0&tabs=visual-studio#disable-a-page

Running the command in the server project works fine and everything can be removed. However when you try to Log in again from the client the following error is now present:

An unhandled exception occurred while processing the request. InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.SignInManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Blazor.Server.Areas.Identity.Pages.Account.LoginModel'. Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)

Reading up on Standalone or hosted Blazor WebAssembly apps for Scaffold Identity it says:

Client-side Blazor WebAssembly apps use their own Identity UI approaches and can't use ASP.NET Core Identity scaffolding. Server-side ASP.NET Core apps of hosted Blazor solutions can follow the Razor Pages/MVC guidance in this article and are configured just like any other type of ASP.NET Core app that supports Identity.

The Blazor framework doesn't include Razor component versions of Identity UI pages. Identity UI Razor components can be custom built or obtained from unsupported third-party sources.

Given Server-side ASP.NET Core apps of hosted Blazor solutions can follow > the Razor Pages/MVC guidance in this article and are configured just > like any other type of ASP.NET Core app that supports Identity. and that the Server project is ASP.NET Core Hosted I hope it can work.

https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-5.0&tabs=visual-studio#standalone-or-hosted-blazor-webassembly-apps

1

1 Answers

0
votes

I began seeing the issue when searching for IdentityUser. When running the scaffold a file called IdentityHostingStartup.cs is created that looks like this:

[assembly: HostingStartup(typeof(Blazor.Server.Areas.Identity.IdentityHostingStartup))]
namespace Blazor.Server.Areas.Identity
{
    public class IdentityHostingStartup : IHostingStartup
    {
        public void Configure(IWebHostBuilder builder)
        {
            builder.ConfigureServices((context, services) => {
                services.AddDbContext<ApplicationDbContext>(options =>
                    options.UseSqlServer(
                        context.Configuration.GetConnectionString("ApplicationDbContextConnection")));

                services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
                    .AddEntityFrameworkStores<ApplicationDbContext>();
            });
        }
    }
}

Both cant exist, then Program.cs CreateHostBuilder(args).Build().Run(); will throw this exception:

System.InvalidOperationException: 'Scheme already exists: Identity.Application'

This is because of the code in Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("DefaultConnection")));

    services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
        .AddEntityFrameworkStores<ApplicationDbContext>();

Removing IdentityHostingStartup.cs still throws the same error.

The key is to look at services.AddDefaultIdentity<IdentityUser> from IdentityHostingStartup.cs and services.AddDefaultIdentity<ApplicationUser> from Startup.cs.

To solve the problem it was not enough to follow the guide from Microsoft. This had to be done as well:

Remove these files:

  • Blazor\Server\Areas\Identity\IdentityHostingStartup.cs
  • Blazor\Server\Areas\Identity\Data\ApplicationDbContext.cs
  • Blazor\Server\wwwroot\favicon.ico

Then edit Blazor\Server\Areas\Identity\Pages\Account\login.cshtml.cs

Change every instance of IdentityUser to ApplicationUser.

Now everything works!

I also got an extra row in appsettings.json - ConnectionStrings called ApplicationDbContextConnection. Simply remove this line as well.