繁体   English   中英

为授权标签设置默认重定向

[英]Set Default Redirect For Authorize Tag

Asp.net Core 3+ 有一些不同的约定——对我来说是裸露的。

我有一个 controller 我正在尝试使用身份验证中间件。 在 VS2019 中创建新的核心项目时,我使用了默认的“脚手架”。 使用 asp.net 内核 3.1 的 MVC 项目模板。

我的 controller 有 [Authorize] 标签。

[Authorize]
public class AgentController : Controller
{
}

在过去的生活中.. 如果未经授权,我知道在哪里设置默认重定向。

它强制重定向到 /Identity/SignIn - 一组默认的 razor 页面,似乎是内置的。我需要它重定向到特定的控制器/操作。 帐户/登录

这是我的启动:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddDefaultIdentity<AgentUser>(options => options.SignIn.RequireConfirmedAccount = false)
            .AddEntityFrameworkStores<ApplicationDbContext>();
        services.AddControllersWithViews();
        //services.AddRazorPages();

    }

    // 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.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
        });
    }

您可以在ConfigureServices中(在Startup中)配置具体路径:

services.ConfigureApplicationCookie(config =>
{
    config.Cookie.Name = "Identity.Cookie";
    config.LoginPath = "/Account/SignIn";
});

当同时添加services.AddRazorPages()services.AddControllersWithViews()时,需要避免相同的路由。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM