切换到 .net core 3 端点路由后,身份 UI 不再有效 [英] Identity UI no longer works after switching to .net core 3 endpoint routing

查看:30
本文介绍了切换到 .net core 3 端点路由后,身份 UI 不再有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在让我的区域显示端点路由很困难之后,我设法在这个自我回答的线程中修复了它(尽管不是以非常令人满意的方式):从 2.2 迁移到 3.0 后的问题,默认有效但可以't access area,有没有办法调试端点解析?

After having a hard time getting my area to show with endpoint routing i managed to fix it in this self answered thread (albeit not in a very satisfactory way) : Issue after migrating from 2.2 to 3.0, default works but can't access area, is there anyway to debug the endpoint resolution?

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

However Identity UI doesn't show at all for me, i get redirected on challenge to the proper url but the page is blank. I have the identity UI nugget package added and, changing from mvc routing to endpoint routing, i didn't change anything that should break it.

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

I also don't seem to do much different than what the default project does and identity works there even if i add a route as i did in my hack.

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

As often the issue hides around the line and not on it i'm posting my whole startup file.

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

Regular (default) controllers work. Admin area works (one of the page doesn't have authentication and i can access it) Any other Admin area page redirect me to /Identity/Account/Login?ReturnUrl=%2Fback (expected behavior) but that page as well as any other /Identity page i tested is blank with no error while running in debug and with a debugger attached.

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

Any help is most appreciated, full startup bellow:

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

推荐答案

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

The Identity UI is implemented using Razor Pages. For endpoint-routing to map these, add a call to MapRazorPages in your UseEndpoints callback:

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

这篇关于切换到 .net core 3 端点路由后,身份 UI 不再有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆