dotnet/aspnetcore

[Refactoring] Move local authz policies to global authz policies

开放

#45,220 创建于 2022年11月21日

 (3 条评论) (0 个反应) (0 位负责人)C# (10,653 个派生)batch import
analyzerarea-authhelp wanted

仓库指标

星标
 (37,933 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

Background and Motivation

In .NET 7, we introduced the RequireAuthorization extension method that allowed a user to construct and add an authorization policy onto an endpoint with on invocation. There are scenarios were users would want to factor these policies out to from endpoint-specific (local) to global policies on the application.

Proposed Refactoring

Refactoring Behavior and Message

When right-clicking on a line of code with a RequireAuthorization option the refactoring will be provided with the following message:

Convert to global authorization policy

Usage Scenarios

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthorization();

var app = builder.Build();

app.UseAuthorization();

app.MapGet("/", () => "Hello world!")
  .RequireAuthorization(policy => p.RequireClaim("scope", "api-access")));

app.Run();

贡献者指南