切换到 .net core 3 端点路由后,身份 UI 不再有效
Posted
技术标签:
【中文标题】切换到 .net core 3 端点路由后,身份 UI 不再有效【英文标题】:Identity UI no longer works after switching to .net core 3 endpoint routing 【发布时间】:2020-02-04 04:26:58 【问题描述】:在很难让我的区域显示端点路由之后,我设法在这个自我回答的线程中修复它(尽管不是以非常令人满意的方式):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 对我来说根本没有显示,我在挑战时被重定向到正确的 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));
【问题讨论】:
【参考方案1】:Identity UI 是使用 Razor Pages 实现的。对于映射这些的端点路由,请在您的 UseEndpoints
回调中添加对 MapRazorPages
的调用:
app.UseEndpoints(endpoints =>
// ...
endpoints.MapRazorPages();
);
【讨论】:
以上是关于切换到 .net core 3 端点路由后,身份 UI 不再有效的主要内容,如果未能解决你的问题,请参考以下文章
不能将“Microsoft.AspNet.OData.Routing.ODataRoute”与端点路由一起使用。 ASP Net Core 2.2 的异常
具有 Azure 应用服务身份验证的 .NET Core 应用