From 0e592df3ecfb402fc33cd8e31603b217393b5f94 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Mon, 10 Aug 2020 13:06:19 -0700 Subject: [PATCH] Make authentication linker friendly (#24708) * Make authentication linker friendly - Preserve constructors wherever open generics or type arguments exist --- .../src/AuthenticationOptions.cs | 3 ++- .../src/AuthenticationScheme.cs | 4 +++- .../src/AuthenticationSchemeBuilder.cs | 2 ++ .../Authentication/Core/src/AuthenticationBuilder.cs | 9 +++++---- src/Security/Authentication/OAuth/src/OAuthExtensions.cs | 5 +++-- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Http/Authentication.Abstractions/src/AuthenticationOptions.cs b/src/Http/Authentication.Abstractions/src/AuthenticationOptions.cs index ee76d6d1e0f3..ca512b0c5fc7 100644 --- a/src/Http/Authentication.Abstractions/src/AuthenticationOptions.cs +++ b/src/Http/Authentication.Abstractions/src/AuthenticationOptions.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Security.Claims; using Microsoft.AspNetCore.Http; @@ -54,7 +55,7 @@ public void AddScheme(string name, Action configure /// The responsible for the scheme. /// The name of the scheme being added. /// The display name for the scheme. - public void AddScheme(string name, string displayName) where THandler : IAuthenticationHandler + public void AddScheme<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]THandler>(string name, string displayName) where THandler : IAuthenticationHandler => AddScheme(name, b => { b.DisplayName = displayName; diff --git a/src/Http/Authentication.Abstractions/src/AuthenticationScheme.cs b/src/Http/Authentication.Abstractions/src/AuthenticationScheme.cs index a268cce022a7..2749c2daa277 100644 --- a/src/Http/Authentication.Abstractions/src/AuthenticationScheme.cs +++ b/src/Http/Authentication.Abstractions/src/AuthenticationScheme.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Diagnostics.CodeAnalysis; namespace Microsoft.AspNetCore.Authentication { @@ -17,7 +18,7 @@ public class AuthenticationScheme /// The name for the authentication scheme. /// The display name for the authentication scheme. /// The type that handles this scheme. - public AuthenticationScheme(string name, string? displayName, Type handlerType) + public AuthenticationScheme(string name, string? displayName, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type handlerType) { if (name == null) { @@ -50,6 +51,7 @@ public AuthenticationScheme(string name, string? displayName, Type handlerType) /// /// The type that handles this scheme. /// + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] public Type HandlerType { get; } } } diff --git a/src/Http/Authentication.Abstractions/src/AuthenticationSchemeBuilder.cs b/src/Http/Authentication.Abstractions/src/AuthenticationSchemeBuilder.cs index 7139de0a2f19..aef541737684 100644 --- a/src/Http/Authentication.Abstractions/src/AuthenticationSchemeBuilder.cs +++ b/src/Http/Authentication.Abstractions/src/AuthenticationSchemeBuilder.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Diagnostics.CodeAnalysis; namespace Microsoft.AspNetCore.Authentication { @@ -32,6 +33,7 @@ public AuthenticationSchemeBuilder(string name) /// /// The type responsible for this scheme. /// + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] public Type? HandlerType { get; set; } /// diff --git a/src/Security/Authentication/Core/src/AuthenticationBuilder.cs b/src/Security/Authentication/Core/src/AuthenticationBuilder.cs index c89c15300321..ba461dc4c3c6 100644 --- a/src/Security/Authentication/Core/src/AuthenticationBuilder.cs +++ b/src/Security/Authentication/Core/src/AuthenticationBuilder.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Diagnostics.CodeAnalysis; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; @@ -25,7 +26,7 @@ public AuthenticationBuilder(IServiceCollection services) /// public virtual IServiceCollection Services { get; } - private AuthenticationBuilder AddSchemeHelper(string authenticationScheme, string? displayName, Action? configureOptions) + private AuthenticationBuilder AddSchemeHelper(string authenticationScheme, string? displayName, Action? configureOptions) where TOptions : AuthenticationSchemeOptions, new() where THandler : class, IAuthenticationHandler { @@ -57,7 +58,7 @@ private AuthenticationBuilder AddSchemeHelper(string authent /// The display name of this scheme. /// Used to configure the scheme options. /// The builder. - public virtual AuthenticationBuilder AddScheme(string authenticationScheme, string? displayName, Action? configureOptions) + public virtual AuthenticationBuilder AddScheme(string authenticationScheme, string? displayName, Action? configureOptions) where TOptions : AuthenticationSchemeOptions, new() where THandler : AuthenticationHandler => AddSchemeHelper(authenticationScheme, displayName, configureOptions); @@ -70,7 +71,7 @@ public virtual AuthenticationBuilder AddScheme(string authen /// The name of this scheme. /// Used to configure the scheme options. /// The builder. - public virtual AuthenticationBuilder AddScheme(string authenticationScheme, Action? configureOptions) + public virtual AuthenticationBuilder AddScheme(string authenticationScheme, Action? configureOptions) where TOptions : AuthenticationSchemeOptions, new() where THandler : AuthenticationHandler => AddScheme(authenticationScheme, displayName: null, configureOptions: configureOptions); @@ -85,7 +86,7 @@ public virtual AuthenticationBuilder AddScheme(string authen /// The display name of this scheme. /// Used to configure the scheme options. /// The builder. - public virtual AuthenticationBuilder AddRemoteScheme(string authenticationScheme, string? displayName, Action? configureOptions) + public virtual AuthenticationBuilder AddRemoteScheme(string authenticationScheme, string? displayName, Action? configureOptions) where TOptions : RemoteAuthenticationOptions, new() where THandler : RemoteAuthenticationHandler { diff --git a/src/Security/Authentication/OAuth/src/OAuthExtensions.cs b/src/Security/Authentication/OAuth/src/OAuthExtensions.cs index 22c541a0acf9..aca0634cb1fd 100644 --- a/src/Security/Authentication/OAuth/src/OAuthExtensions.cs +++ b/src/Security/Authentication/OAuth/src/OAuthExtensions.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Diagnostics.CodeAnalysis; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.OAuth; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -17,12 +18,12 @@ public static AuthenticationBuilder AddOAuth(this AuthenticationBuilder builder, public static AuthenticationBuilder AddOAuth(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) => builder.AddOAuth>(authenticationScheme, displayName, configureOptions); - public static AuthenticationBuilder AddOAuth(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) + public static AuthenticationBuilder AddOAuth(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) where TOptions : OAuthOptions, new() where THandler : OAuthHandler => builder.AddOAuth(authenticationScheme, OAuthDefaults.DisplayName, configureOptions); - public static AuthenticationBuilder AddOAuth(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) + public static AuthenticationBuilder AddOAuth(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) where TOptions : OAuthOptions, new() where THandler : OAuthHandler {