3

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>();
             });
         }
     }

br3nt
  • 9,017
  • 3
  • 42
  • 63

2 Answers2

8

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

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

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.

Yinqiu
  • 6,609
  • 1
  • 6
  • 14
  • Okay, yes what you propose does work, however, according to [these docs](https://learn.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-5.0) `AddDefaultUI` shouldn't be needed. It says the following multiple times: `If the Identity scaffolder was used to add Identity files to the project, remove the call to AddDefaultUI`. Following [the code](https://github.com/aspnet/Identity/blob/master/src/UI/IdentityServiceCollectionUIExtensions.cs#L47-L65) it's doing a lot more than what I want it to :/. – br3nt Nov 26 '20 at 07:43
  • Are you a scaffolding project? Or did you choose personal authentication when you created the project.The project generated by the scaffold should be generated like [this](https://learn.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-5.0&tabs=visual-studio). – Yinqiu Nov 26 '20 at 08:10
  • I first created a new ASP.Net .Net 5.0 MVC app without authentication. I then followed along to the [Scaffold Identity into an MVC project without existing authorization](https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-5.0&tabs=visual-studio#scaffold-identity-into-an-mvc-project-without-existing-authorization) guide, and then followed the [Create full Identity UI source](https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-5.0&tabs=visual-studio#create-full-identity-ui-source) guide. – br3nt Nov 26 '20 at 23:04
  • I also repeated that process but using .Net Core 3.1. – br3nt Nov 26 '20 at 23:04
  • And I also repeated the step but selected the user authentication option, and then followed the [Scaffold Identity into an MVC project with authorization](https://learn.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-5.0&tabs=visual-studio#scaffold-identity-into-an-mvc-project-with-authorization) guide and then followed the [Create full Identity UI source](https://learn.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-5.0&tabs=visual-studio#create-full-identity-ui-source) guide. – br3nt Nov 26 '20 at 23:04
  • So, it doesn't matter which route you take... there's obviously something missing from the [Create full Identity UI source](https://learn.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-5.0&tabs=visual-studio#create-full-identity-ui-source) guide. – br3nt Nov 26 '20 at 23:05
  • 1
    The understanding of this sentence in the document should be that before 2.1, `AddDefaultIdentity` does not contain the `AddDefaultUI` method, so you need to add `AddDefaultUI`, you can see [here](https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-2.0&tabs=visual-studio#create-a-web-app-with-authentication-1), so you need to add the `AddDefaultUI`, you can see here, so the understanding of the removal of AddDefaultUI in the document should be It is `AddDefaultIdentity` that needs to remove `AddDefaultUI`, not `AddIdentity` needs to be removed. – Yinqiu Dec 02 '20 at 03:26