使用Orchard Core构建ASP.NET Core模块化应用时Identity路由无法访问求助
Orchard Core 模块集成Identity Framework无法访问默认登录路由解决方案
- 确认Identity相关应用部分已被加载
Orchard Core 模块默认不会自动引入Identity Razor类库的视图与路由配置,需要在模块的ConfigureServices方法中注册Identity服务时,显式将Identity相关程序集加入应用部分集合,示例代码:
// 原有Identity注册逻辑 services.AddDefaultIdentity<IdentityUser>() .AddEntityFrameworkStores<YourModuleDbContext>() .AddDefaultTokenProviders(); // 新增应用部分加载逻辑 var identityRazorAssembly = typeof(IdentityUser).Assembly; services.AddControllersWithViews() .AddApplicationPart(identityRazorAssembly); services.AddRazorPages() .AddApplicationPart(identityRazorAssembly);
- 配置Identity路由匹配模块前缀
默认Identity生成的路由规则无自定义前缀,需要在注册Razor Pages时添加路由约定,将模块路由前缀拼接到Identity路由前,示例代码:
services.AddRazorPages(options => { options.Conventions.AddAreaFolderRouteModelConvention("Identity", "/Account/", routeModel => { foreach (var selector in routeModel.Selectors) { var attrRoute = selector.AttributeRouteModel; attrRoute.Template = AttributeRouteModel.CombineTemplates( // 替换为你的模块实际路由前缀 "modular_project_name", attrRoute.Template ); } }); });
- 调整模块启动顺序避免路由覆盖
模块启动类需要实现IStartup接口,同时将Order属性设置为小于0,确保模块的路由配置逻辑早于宿主默认路由配置执行,避免Identity路由被全局规则覆盖。 - 放行Identity路由的权限校验
Orchard Core 默认的权限拦截机制会拦截未授权访问的路由,你可以单独为Identity路由放行匿名访问权限,示例代码:
public void Configure(IApplicationBuilder app, IHostEnvironment env) { // 放在UseAuthorization之前配置 app.MapWhen(context => context.Request.Path.StartsWithSegments("/modular_project_name/Identity"), identityApp => { identityApp.UseRouting(); identityApp.UseAuthorization(); identityApp.UseEndpoints(endpoints => { endpoints.MapRazorPages().AllowAnonymous(); }); } ); }
内容的提问来源于stack exchange,提问作者Ken J
相关产品推荐
相关产品推荐

