首页 > 解决方案 > 切换到 .net core 3 端点路由后,身份 UI 不再有效

问题描述

在很难让我的区域显示端点路由之后,我设法在这个自我回答的线程中修复它(尽管不是以非常令人满意的方式):从 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));
            }
        }
    }
}

标签: c#asp.net-corerazor-pagesasp.net-core-3.0asp.net-core-identity

解决方案


Identity UI 是使用 Razor Pages 实现的。要映射这些端点路由,请MapRazorPagesUseEndpoints回调中添加调用:

app.UseEndpoints(endpoints =>
{
    // ...
    endpoints.MapRazorPages();
});

推荐阅读