如何解决向控制器添加[授权]未能重定向到“身份登录”路由 ASP.NET CORE 3.1 MVC
我有一个asp.net核心3.1 MVC项目。它已添加身份。通常,当我单击一个按钮进入登录页面或注册页面时,它会按预期工作。但是,当我在 Startup.cs 文件中的控制器或全局授权过滤器上使用 [Authorize] 时,该页面不会重定向到登录页面。
在这种情况下,重定向链接为
https:// localhost:5001 / Account / Login?ReturnUrl =%2F
链接应该在哪里
https:// localhost:5001 / Identity / Account / Login?ReturnUrl =%2F
如果仔细查看,我所获得的链接中缺少区域名称“身份” 。我猜我的 Startup.cs 文件配置有问题,但是无法弄清楚是什么。 我的项目中有多个区域和身份
Startup.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
});
,
我刚刚在GitHub上浏览了一下,发现Account/Login
在CookieAuthenticationDefaults中被设置为默认值。然后,我发现它被PostConfigureCookieAuthenticationOptions使用,当未指定任何选项时,它将设置明智的配置选项。因此,在我看来您必须像这样进行配置:
services.ConfigureApplicationCookie(x => x.LoginPath = "/identity/account/login");
x
的类型为CookieAuthenticationOptions
,即PostConfigureCookieAuthenticationOptions
正在读取的类型。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。