切换到 .net core 3 端点路由后,身份 UI 不再有效

Ron*_*dau 5 c# asp.net-core asp.net-core-identity razor-pages asp.net-core-3.0

在很难让我的区域显示端点路由后,我设法在这个自我回答的线程中修复了它(尽管不是以非常令人满意的方式):从 2.2 迁移到 3.0 后出现问题,默认工作但无法访问区域,无论如何要调试端点解析?

然而,Identity UI 根本没有显示,我在挑战时被重定向到正确的 url,但页面是空白的。我添加了身份 UI 块包,并且从 mvc 路由更改为端点路由,我没有更改任何应该破坏它的内容。

即使我像在 hack 中那样添加路由,我似乎也没有做与默认项目所做的事情和身份在那里工作的事情有多大不同。

通常问题隐藏在线路周围而不是在线上,我发布了我的整个启动文件。

常规(默认)控制器工作。管理区域工作(其中一个页面没有身份验证,我可以访问它)任何其他管理区域页面将我重定向到 /Identity/Account/Login?ReturnUrl=%2Fback(预期行为)但该页面以及任何其他页面我测试的 /Identity 页面是空白的,在调试中运行时没有错误并且连接了调试器。

非常感谢任何帮助,完整启动如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using FranceMontgolfieres.Models;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;

namespace FranceMontgolfieres
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton<IConfiguration>(Configuration);

            services
                .Configure<CookiePolicyOptions>(options =>
                {
                    // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                    options.CheckConsentNeeded = context => true;
                    options.MinimumSameSitePolicy = SameSiteMode.None;
                });

            services
                .AddDbContext<FMContext>(options => options
                    .UseLazyLoadingProxies(true)
                    .UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services
                .AddDefaultIdentity<IdentityUser>()
                .AddRoles<IdentityRole>()
                .AddEntityFrameworkStores<FMContext>();

            services
                .AddMemoryCache();

            services.AddDistributedSqlServerCache(options =>
            {
                options.ConnectionString = Configuration.GetConnectionString("SessionConnection");
                options.SchemaName = "dbo";
                options.TableName = "SessionCache";
            });

            services.AddHttpContextAccessor();

            services
                .AddSession(options => options.IdleTimeout = TimeSpan.FromMinutes(30));

            services.AddControllersWithViews();
            services.AddRazorPages();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseRouting();
            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseAuthentication();
            app.UseAuthorization();
            app.UseSession(); 

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapAreaControllerRoute("Back", "Back", "back/{controller=Home}/{action=Index}/{id?}");
                endpoints.MapControllerRoute("default","{controller=Home}/{action=Index}/{id?}");
            });
        }

        private async Task CreateRoles(IServiceProvider serviceProvider)
        {
            //initializing custom roles 
            var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
            string[] roleNames = { "Admin", "Manager", "Member" };
            IdentityResult roleResult;

            foreach (var roleName in roleNames)
            {
                roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName));
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Kir*_*kin 13

Identity UI 是使用 Razor Pages 实现的。对于端点路由来映射这些,MapRazorPages在你的UseEndpoints回调中添加一个调用:

app.UseEndpoints(endpoints =>
{
    // ...
    endpoints.MapRazorPages();
});
Run Code Online (Sandbox Code Playgroud)