question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Analyzer/Codefixer] Recommend using `AddAuthorizationBuilder` for configuring global policies

See original GitHub issue

Background and Motivation

In .NET 7, we introduced an AddAuthorizationBuilder extension method on IServiceCollection that would register authorization-related services and provide an AuthorizationBuilder for constructing policies. This is an abbreviated syntax that allows reduces nesting in the original pattern of calling AddAuthorization and providing a policy construct as a callback.

Proposed Analyzer

Analyzer Behavior and Message

When the user provides code where AddAuthorizationBuilder would provide a more abbreviated style, recommend a codefix with the following message:

Use AddAuthorizationBuilder to register authorization services and construct policies.

Category

  • Design
  • Documentation
  • Globalization
  • Interoperability
  • Maintainability
  • Naming
  • Performance
  • Reliability
  • Security
  • Style
  • Usage

Severity Level

  • Error
  • Warning
  • Info
  • Hidden

Usage Scenarios

Before

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthorization(options =>
{
    options.AddPolicy("AtLeast21", policy =>
        policy.Requirements.Add(new MinimumAgeRequirement(21)));
});

var app = builder.Build();

app.UseAuthorization();

app.Run();

After

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthorizationBuilder()
  .AddPolicy("AtLeast21", policy =>
  {
        policy.Requirements.Add(new MinimumAgeRequirement(21)));
  });

var app = builder.Build();

app.UseAuthorization();

app.Run();

Risks

Marking this as an info-only analyzer strikes a good balance between informing the user of this feature without being too presumptuous (via a warning). A refactoring would not have been good at educating the user about the functionality.

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
captainsafiacommented, Mar 20, 2023

I’m interested in helping out with this. Should the analyzer + codefix be added to Framework/AspNetCoreAnalyzers?

Yes please and thank you! 🙏🏽

1reaction
captainsafiacommented, Nov 29, 2022

Is the message to passive? Starting with “Recommend using” seems wrong.

With regard to this, it seems like the general convention is to start with “Use …” so I’ve updated the description to mimick this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ASP0025: Use AddAuthorizationBuilder to register ...
Learn about analysis rule ASP0025: Use AddAuthorizationBuilder to register authorization services and construct policies.
Read more >
ASP.NET Core updates in .NET 8 Preview 5
New analyzer for recommended AuthorizationBuilder usage. In .NET 7 we introduced support for an AddAuthorizationBuilder API that allowed users ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found