gpt4 book ai didi

c# - 无法通过启用的 UseAuthentication 访问 HomeController 路由

转载 作者:太空宇宙 更新时间:2023-11-03 14:48:24 24 4
gpt4 key购买 nike

如标题所述,我想在 .NET Core 2.1 中实现某种授权,但我无法按照他们的教程进行操作。我已经设置了 Startup.cs 文件

public void ConfigureServices(IServiceCollection services)
{

string connection = Configuration.GetConnectionString("DefaultConnection");
if (_currentEnvironment.IsDevelopment())
services.AddDbContext<PagesContext>(options => options.UseSqlServer(connection), ServiceLifetime.Singleton);
else
services.AddDbContext<PagesContext>(options => options.UseMySql(connection), ServiceLifetime.Singleton);

services.AddIdentity<User, IdentityRole>(opts=>
{
opts.Password.RequireDigit = false;
opts.Password.RequiredLength = 8;
opts.Password.RequireLowercase = false;
opts.Password.RequireNonAlphanumeric = false;
opts.Password.RequireUppercase = false;
opts.User.AllowedUserNameCharacters = null;
}).AddEntityFrameworkStores<PagesContext>()
.AddDefaultTokenProviders();

services.AddSingleton<IStringLocalizerFactory, EFStringLocalizerFactory>();
services.AddMvc();

}


public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseStatusCodePages();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("en-GB"),
new CultureInfo("en"),
new CultureInfo("ru-RU"),
new CultureInfo("ru"),
new CultureInfo("uk-UA"),
new CultureInfo("ua")
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("ru-RU"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
});
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(
routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
}
);

我有包含以下内容的 HomeController.cs 文件:

    [HttpGet]
public async Task<IActionResult> Index()
{

var user = await _userManager.GetUserAsync(HttpContext.User);
if (user != null)
{
if (await _userManager.IsInRoleAsync(user, "Admin"))
{
return LocalRedirectPermanent("/Admin/Index");
}
else if (await _userManager.IsInRoleAsync(user, "Carrier"))
{
return LocalRedirectPermanent("/Carrier/Index");
}
return LocalRedirectPermanent("/Passenger/Index");
}
Page indexPage = db.Page.Include(u => u.Title).Include(u => u.Description).FirstOrDefault(p => p.Tag == "main_page");
return View();
}

从我要检测的代码可以看出,如果用户登录 - 显示相应的页面。如果不是 - 启用登录程序的页面。但是,当使用身份验证时,默认路由将被忽略。如果我从 Visual Studio 启动它,它会将我导航到/Account/AccessDenied?ReturnUrl=%2FPassenger%2FIndex,而不是/Home/Index。我该如何覆盖它?

最佳答案

我找到了。看来,这不是 .NET Core 的错。它基于标题。在我的例子中,LocalRedirectPermanent("/Admin/Index") 被多次调用,因此,它自动调用“/Admin/Index”,绕过“/Home/Index”。我认为,这是由于 this .因此,我在我的浏览器 (Firefox) 中以这种方式修复:重置历史记录(表单、cookie 等)并在 Visual Studio Build->Rebuild Solution 中执行。它又开始工作了。希望,这对某人有帮助

关于c# - 无法通过启用的 UseAuthentication 访问 HomeController 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53068658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com