asp.net-core - 将 [Authorize] 添加到 Controller 无法重定向到身份登录路由。 ASP.NET 核心 3.1 MVC

标签 asp.net-core asp.net-mvc-3 asp.net-core-identity asp.net-core-routing

我有一个 asp.net core 3.1 MVC 项目。它添加了身份。当我通常单击按钮进入登录页面或注册页面时,它会按预期工作。然而,当我在 Startup.cs 文件中的 Controller 或全局授权过滤器上使用 [Authorize] 时,页面不会重定向到登录页面。

这种情况下的重定向链接是

https://localhost:5001/Account/Login?ReturnUrl=%2F

链接应该在哪里

https://localhost:5001/Identity/Account/Login?ReturnUrl=%2F

如果您仔细观察,我收到的链接中缺少区域名称“身份”。我猜测我的 Startup.cs 文件配置有问题,但无法弄清楚是什么。 我的项目中有多个区域以及身份

启动.cs

public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        /*services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
            .AddEntityFrameworkStores<ApplicationDbContext>();*/

        services.AddIdentity<ApplicationUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
        services.AddTransient<IEmailSender, EmailSender>();
        services.AddControllersWithViews();
        services.AddRazorPages().AddMvcOptions(options => options.Filters.Add(new AuthorizeFilter()));
        services.AddSingleton<IConfiguration>(Configuration);
        services.AddMvc(options => options.EnableEndpointRouting = false);
        services.AddMvc().AddRazorRuntimeCompilation();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthentication();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {

            endpoints.MapRazorPages().RequireAuthorization();
            endpoints.MapControllerRoute(
               name: "Identity",
               pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");

            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
        });
    }
}

任何帮助将不胜感激。谢谢。

最佳答案

services.ConfigureApplicationCookie 中定义登录路径,如下所示:

services.ConfigureApplicationCookie(options =>
{
    options.LoginPath = new PathString("/Identity/Account/Login");
    //other properties
});

关于asp.net-core - 将 [Authorize] 添加到 Controller 无法重定向到身份登录路由。 ASP.NET 核心 3.1 MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64061876/

相关文章:

asp.net-core - ASP.NET Core DI 直接访问容器

c# - 使用 SimpleInjector 在 IExceptionFilter 中注入(inject) Logger

c# - DropDownListFor 没有在 View 中获取所选项目,而是在 View 模型中

asp.net - 如何在 asp.net mvc 3 中使用非 wcf 休息服务?

asp.net-core - Razor 模板中的特殊字符未正确编码

asp.net-core - Asp.net Core 中的用户主机地址

asp.net-mvc-3 - 无法为 mvc3 .net Web 应用程序设置 <customErrors mode ="off">

c# - IdentityUser 的 IUserValidator

c# - net core identity 2.1 中方法 VerifyTwoFactorTokenAsync() 的问题

entity-framework-core - 带有 DB Context Factory 的身份存储