Layout for Identity/Account/Manage pages broken after scaffolding full .Net Identity UI source

Viewed 788

I'm following along to these to guides:

  1. Scaffold Identity into an MVC project without existing authorization
  2. Create full Identity UI source

After following the 1st guide I get what I expect for the Identity/Account/Manage pages: enter image description here

However, after following the 2nd guide the layout is broken. The side menu is missing. The app is no longer finding Areas/Identity/Pages/Account/Manage/_Layout.cshtml, and I don't understand why.

enter image description here

This is the git diff.

namespace WebIdentity.Areas.Identity
{
    public class IdentityHostingStartup : IHostingStartup
    {
        public void Configure(IWebHostBuilder builder)
        {
            builder.ConfigureServices((context, services) => {
                services.AddDbContext<IdentityDbContext>(options =>
                    options.UseSqlServer(
                        context.Configuration.GetConnectionString("IdentityDbContextConnection")));
 
-                services.AddDefaultIdentity<User>(options => options.SignIn.RequireConfirmedAccount = true)
-                    .AddEntityFrameworkStores<IdentityDbContext>();
+                services
+                    .AddIdentity<User, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = true)
+                    .AddEntityFrameworkStores<IdentityDbContext>()
+                    .AddDefaultTokenProviders();
+
+                services
+                    .AddMvc()
+                    .AddRazorPagesOptions(options =>
+                    {
+                        options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Manage");
+                        options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
+                    });
+
+                services.ConfigureApplicationCookie(options =>
+                {
+                    options.LoginPath = $"/Identity/Account/Login";
+                    options.LogoutPath = $"/Identity/Account/Logout";
+                    options.AccessDeniedPath = $"/Identity/Account/AccessDenied";
+                });
+
+                services.AddSingleton<IEmailSender, EmailSender>();
             });
         }
     }

2 Answers

just add a _ViewStart.cshtml in the Areas/Identity/Pages/Account/Manage folder with:

@{
   ViewData["ParentLayout"] = Layout;
   Layout = "_Layout.cshtml";
}

Calling AddDefaultIdentity is similar to calling the following:

1:AddIdentity

2:AddDefaultUI

3:AddDefaultTokenProviders

You need to add default UI in your startup,like below(add .AddDefaultUI()):

  services
  .AddIdentity<User, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = true)
  .AddDefaultUI()
  .AddEntityFrameworkStores<IdentityDbContext>()
  .AddDefaultTokenProviders();

You can see more details about AddDefaultIdentity here.

Related