【问题标题】:ASP.NET Core Authorize Redirection to wrong URLASP.NET Core 授权重定向到错误的 URL
【发布时间】:2017-04-22 09:44:25
【问题描述】:

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

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

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

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

【问题讨论】:

标签: redirect routes asp.net-core asp.net-core-mvc .net-core


【解决方案1】:

@Dmitry 的答案在 ASP.NET Core 3.1 中不再有效。根据您可以找到 here 的文档,您必须将以下代码添加到 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";
     });

【讨论】:

    【解决方案2】:

    这对我有用(Startup.ConfigureServices):

    services.AddIdentity<User, UserRole>(options => 
    {
        options.Cookies.ApplicationCookie.LoginPath = new PathString("/Admin/Account/Login");
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-30
      • 2020-04-25
      • 1970-01-01
      相关资源
      最近更新 更多