Skip to content

Remove Authentication property from WebApplicationBuilder and fix up middleware auto-registration #42585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 7, 2022

Conversation

captainsafia
Copy link
Member

Part of #42481.

We've concluded that we'll remove any authentication-related APIs from the top-level interface of WebApplicationBuilder.

@captainsafia captainsafia added old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels feature-authentication labels Jul 5, 2022
@captainsafia captainsafia requested a review from a team July 5, 2022 21:50
@davidfowl
Copy link
Member

It would be good if we could avoid regressing by making this change also support auto adding middleware by detecting services.

@captainsafia captainsafia changed the title Remove Authentication property from WebApplicationBuilder Remove Authentication property from WebApplicationBuilder and fix up middleware auto-registration Jul 6, 2022
@captainsafia captainsafia requested a review from Tratcher July 6, 2022 20:11
@captainsafia
Copy link
Member Author

Updated this to include changes to the registration of auth(n)/auth(z) middlewares.

Co-authored-by: David Fowler <davidfowl@gmail.com>
@@ -16,4 +16,8 @@
<Reference Include="Microsoft.AspNetCore.Http.Extensions" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="Microsoft.AspNetCore" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😢 Is there another way to do this? Can we use a public service type as our marker?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spoke to @captainsafia about this and I think we can cautiously allow IVT to Microsoft.AspNetCore but not between individual components.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there another way to do this? Can we use a public service type as our marker?

That's where I originally started, but @davidfowl mentioned that using a marker type would be a more reliable option. In fact, the authorization middleware already does this here:

if (app.ApplicationServices.GetService(typeof(AuthorizationPolicyMarkerService)) == null)
{
throw new InvalidOperationException(Resources.FormatException_UnableToFindServices(
nameof(IServiceCollection),
nameof(PolicyServiceCollectionExtensions.AddAuthorization)));
}

This serves as a more reliable indicator in the event that users do something strange and register a public type in DI themselves (unlikely but ya never know).

We could've made these marker types public, but I think it pollutes the public API primarily for our benefit with little value to users.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My concern is that this makes it easier to consume internal APIs from the Microsoft.AspNetCore project in the future without realizing it. In many cases consuming an internal API from another project is a good signal it API should be public and after this change we'll lose that signal.

While no one else may ever use a public AuthenticationMarkerService in particular, I don't think the noise it adds is too onerous either. We can make it EditorVisibleNever like @Tratcher suggests.

Copy link
Member Author

@captainsafia captainsafia Jul 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using EditorVisibleNever is a good recommendation. I'll go ahead and do that on the existing authorization marker type too.

While no one else may ever use a public AuthenticationMarkerService in particular, I don't think the noise it adds is too onerous either.

Perhaps, but I think it's the accumulation of decisions like these that pollutes an API.

Edit: Nevermind. I just realized that Chris's recommendation was to make these marker types public and use EditorBrowsable.Never.

In many cases consuming an internal API from another project is a good signal it API should be public and after this change we'll lose that signal.

Perhaps this removes the compile-time alert, but there are still other gates.

I would rather not use the marker types and use existing public ones then introduce public APIs with low value.

Copy link
Member

@davidfowl davidfowl Jul 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PUBTERNAL is back!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the problem with this either. It seems to be the simplest solution.

Yeah, that's where I started. It's a little ad-hoc but it involves far less ✨ controversy ✨ than this so 🚢

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went ahead and removed the IVT here. IMO, if we're choosing between introducing hidden, public types or using a non-marker service, I lean towards the later.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are some very good usages of IVT here now that I’ve seen this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that IVT could be very convenient. I've long thought will make the WAB much cleaner if we were not restricted to using the public ConfigureWebHostDefaults API or being forced to add a public API that will ever only be used by WAB itself. I can probably be convinced that IVT is justifiable for Microsoft.AspNetCore from any assembly because it's the composition root or whatever giving it a special status.

We should discuss this in a meeting with everyone involved though. If we agree, we can go back to the internal marker service. I don't think it should hold up this PR.

@captainsafia captainsafia enabled auto-merge (squash) July 6, 2022 23:53
@captainsafia captainsafia merged commit 202fafe into main Jul 7, 2022
@captainsafia captainsafia deleted the saf/rm-wab-auth branch July 7, 2022 05:30
@ghost ghost added this to the 7.0-preview7 milestone Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-authentication old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants