asp.net 核心 3 AuthorizeAttribute 或管理页面总是要求登录
asp.net core 3 AuthorizeAttribute or manage page always asks to login
OSX ASP.Net 核心 3 MVC 命令行构建..
该应用程序允许用户正常登录并且似乎工作正常。
当我添加
@if (SignInManager.IsSignedIn(User))
{
到视图的 cshtml 文件(老兄,我不想说剃刀页面)它要么在登录时显示该部分的信息,要么在未登录时不显示。效果很好。
当我将 AuthorizeAttribute 添加到控制器时 class 就像
namespace mywebapp.Controllers
{
[Authorize]
public class FancyController : Controller
{
private readonly ApplicationDbContext _context;
... etc...
并尝试在不登录的情况下访问此页面,它会将我重定向到登录页面。
但是...即使我已登录,它也会这样做。它永远不允许访问该页面。
我试图查看我的配置步骤是否有问题,正如我在其他各种问题的答案中看到的修复...
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
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();
}
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.UseAuthorization();
app.UseAuthentication();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
endpoints.MapControllers();
});
}
但是我尝试的任何命令都不能解决这个问题。一次尝试实际上总是允许访问,无论是否登录。我在常规 .Net MVC 5 中很好地使用了该属性...
如何让登录和 [授权] 属性在 Core 3 中协同工作?
只需切换:
app.UseAuthorization();
app.UseAuthentication();
所以你在authorization
之前使用authentication
。
然后再重建。
OSX ASP.Net 核心 3 MVC 命令行构建..
该应用程序允许用户正常登录并且似乎工作正常。
当我添加
@if (SignInManager.IsSignedIn(User))
{
到视图的 cshtml 文件(老兄,我不想说剃刀页面)它要么在登录时显示该部分的信息,要么在未登录时不显示。效果很好。
当我将 AuthorizeAttribute 添加到控制器时 class 就像
namespace mywebapp.Controllers
{
[Authorize]
public class FancyController : Controller
{
private readonly ApplicationDbContext _context;
... etc...
并尝试在不登录的情况下访问此页面,它会将我重定向到登录页面。
但是...即使我已登录,它也会这样做。它永远不允许访问该页面。
我试图查看我的配置步骤是否有问题,正如我在其他各种问题的答案中看到的修复...
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
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();
}
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.UseAuthorization();
app.UseAuthentication();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
endpoints.MapControllers();
});
}
但是我尝试的任何命令都不能解决这个问题。一次尝试实际上总是允许访问,无论是否登录。我在常规 .Net MVC 5 中很好地使用了该属性...
如何让登录和 [授权] 属性在 Core 3 中协同工作?
只需切换:
app.UseAuthorization();
app.UseAuthentication();
所以你在authorization
之前使用authentication
。
然后再重建。