I'm still trying to migrate my transparent login router to ASP.NET Core 3 with the new endpoint routing. My current approach is to insert a middleware before the routing happens. When this middleware detects that the user is not logged in, the login action must be invoked instead of whatever the URL requested. When the user has logged in, the same original URL will then serve the requested page. Here's my middleware in the Startup.Configure method:
... app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.Use(async (context, next) => { if (!context.User.Claims.Any()) { var routeData = context.GetRouteData(); routeData.Values["controller"] = "Account"; routeData.Values["action"] = "Login"; } // Call the next delegate/middleware in the pipeline await next(); }); app.UseEndpoints(endpoints => ...
It correctly determines the logged in state but then can't change the routing. The shown code will serve the Account/Loginview, but the code still executes the Home/Index action. This doesn't match so I probably destroyed the routing data with this.
How can I force the Account/Login action and view to be called here from the middleware, without changing the URL (no redirect allowed)?