ASP.NET Core 授权重定向到错误的 URL [英] ASP.NET Core Authorize Redirection to wrong URL

查看:39
本文介绍了ASP.NET Core 授权重定向到错误的 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一个映射了以下路由的 Web 应用程序:

I am trying to run a web application with the following route mapped:

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                "default",
                "WoL/{controller=Account}/{action=Login}/{id?}");
        });

如果用户未通过身份验证并尝试访问具有 AuthorizeAttribute 的操作,则应将用户重定向到默认登录 URL(如上所示).但是用户被重定向到/Account/Login"而不是/WoL/Account/Login".如果用户未通过身份验证,如何将用户重定向到/WoL/Account/Login"?我已经配置了以下 Cookie 身份验证:

If the user is not authenticated and tries to access a action having the AuthorizeAttribute, the user should be redirected to the default login URL (as seen above). But the user gets redirected to "/Account/Login" instead of "/WoL/Account/Login". How can I redirect the user to "/WoL/Account/Login", if the user is not authenticated? I have configured the following Cookie Authentication:

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            LoginPath = new PathString("/WoL/Account/Login"),
            AutomaticChallenge = true
        });

推荐答案

@Dmitry 的答案在 ASP.NET Core 3.1 中不再有效.根据您可以找到的文档 这里,你必须在ConfigureServices中添加以下代码:

The answer of @Dmitry is not working anymore in ASP.NET Core 3.1. Based on the documentation that you can find here, you have to add the following code to the ConfigureServices:

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Latest)
   .AddRazorPagesOptions(options =>
   {
       options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Manage");
       options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
    });

    services.ConfigureApplicationCookie(options =>
    {
       options.LoginPath = $"/Identity/Account/Login";
       options.LogoutPath = $"/Identity/Account/Logout";
       options.AccessDeniedPath = $"/Identity/Account/AccessDenied";
     });

这篇关于ASP.NET Core 授权重定向到错误的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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