几乎所有的dotnet默认模板在管道配置中都有一行app.UseAuthorization()
,而没有app.UseAuthentication()
。例如,inline是在.net 6.0中通过运行dotnet new webapp
创建的Web应用程序
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/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.MapRazorPages();
app.Run();
令我感兴趣的是,授权只能与身份验证一起使用,因此必须同时具有app.UseAuthentication()
和app.UseAuthorization()
。顺序也必须精确,即app.UseAuthentication()
后面必须跟app.UseAuthorization()
。
但是,使用默认模板创建的管道只有app.UseAuthorization()
。有人能说明在管道中只添加app.UseAuthorization()
的目的是什么吗?
这是一个已知的问题:添加应用程序的目的。使用默认模板创建无身份验证应用程序时使用Authorization((中间件
从3.1开始,端点路由替换了一些MVC结构,包括用AuthorizationMiddleware替换身份验证过滤器。因此,每当您碰巧使用MVC时,您都需要中间件。特别是因为您可能有不需要对用户进行身份验证的授权规则。例如,RequireHttpsAttribute是一个身份验证筛选器,它需要仅允许通过TLS连接的资源。您可以设想其他基于HTTP请求特征而不是登录用户的身份验证要求