ASP.NET Core(3.0) 上的身份在使用端点时无法正常工作
Identity on ASP.NET Core(3.0) not functioning when using endpoints
当我使用路由时,Identity 按预期运行。
//Identity functions as expected:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "areas",
template: "{area:exists}/{controller=Home}/{action=Index}");
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
但是,当我使用端点时,用户卡在登录视图中(任何尝试访问使用 [Authorize] 装饰的 Action 方法都会重定向到我的登录视图,即使在 SignInResult 成功之后也是如此)。
// Identity not functioning when using endpoints:
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<ChatHub>("/chatHub");
endpoints.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=Home}/{action=Index}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
该应用程序的其余部分使用端点运行良好,我的问题仅与身份有关。
If the app uses authentication/authorization features such as
AuthorizePage
or [Authorize]
, place the call to UseAuthentication
and
UseAuthorization
: after, UseRouting
and UseCors
, but before
UseEndpoints
public void Configure(IApplicationBuilder app)
{
...
app.UseStaticFiles();
app.UseRouting();
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
});
...
}
请阅读this了解更多详情
当我使用路由时,Identity 按预期运行。
//Identity functions as expected:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "areas",
template: "{area:exists}/{controller=Home}/{action=Index}");
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
但是,当我使用端点时,用户卡在登录视图中(任何尝试访问使用 [Authorize] 装饰的 Action 方法都会重定向到我的登录视图,即使在 SignInResult 成功之后也是如此)。
// Identity not functioning when using endpoints:
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<ChatHub>("/chatHub");
endpoints.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=Home}/{action=Index}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
该应用程序的其余部分使用端点运行良好,我的问题仅与身份有关。
If the app uses authentication/authorization features such as
AuthorizePage
or[Authorize]
, place the call toUseAuthentication
andUseAuthorization
: after,UseRouting
andUseCors
, but beforeUseEndpoints
public void Configure(IApplicationBuilder app)
{
...
app.UseStaticFiles();
app.UseRouting();
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
});
...
}
请阅读this了解更多详情