【问题标题】:Set Default Redirect For Authorize Tag为授权标签设置默认重定向
【发布时间】:2021-05-31 23:45:26
【问题描述】:

Asp.net Core 3+ 有一些不同的约定 - 与我无关。

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

我的控制器带有 [Authorize] 标签。

[Authorize]
public class AgentController : Controller
{
}

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

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

这是我的启动:

    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();
        });
    }

【问题讨论】:

    标签: c# asp.net-core asp.net-core-mvc asp.net-core-identity


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-14
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      相关资源
      最近更新 更多