'Scaffolding for Identity in .Net Core 6

The 'Scaffold Identity into a Razor project without existing authorization' instructions apparently don't work for .Net 6.0.

The following code was automatically added to my Program.cs file as a result of doing Identity scaffolding for a Razor Pages app, using a context that did not previously exist:

builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true).
AddEntityFrameworkStores<ApplicationDbContext)<>();builder.Services.AddDbContext<ApplicationDbContext>(options => 
options.UseSqlServer(connectionString));

I got two error messages:

  • IServiceCollection does not contain a definition for 'AddDefaultIdentity'.
  • The name 'connectionString' does not exist in the current context.

What do you suggest, please?



Solution 1:[1]

IServiceCollection does not contain a definition for 'AddDefaultIdentity'.

Check your Packages.Do you have Microsoft.AspNetCore.Identity.UI ? If not, add it by NuGet Package Manager.

My Packages structure is below:

enter image description here

The name 'connectionString' does not exist in the current context.

Do you have define the connectionString in Program.cs?

My code is below, you can refer it:

 using Microsoft.AspNetCore.Identity;
    using Microsoft.EntityFrameworkCore;
    using WebI2.Data;
    var builder = WebApplication.CreateBuilder(args);
   //Get connectionString from appsetting.json
    var connectionString = builder.Configuration.GetConnectionString("WebI2ContextConnection");
    builder.Services.AddDbContext<WebI2Context>(options =>
        options.UseSqlServer(connectionString));
    builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
        .AddEntityFrameworkStores<WebI2Context>();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Qing Guo