Make authentication linker friendly (#24708)

* Make authentication linker friendly
- Preserve constructors wherever open generics or type arguments exist
This commit is contained in:
David Fowler 2020-08-10 13:06:19 -07:00 committed by GitHub
parent 927ddab349
commit 0e592df3ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 8 deletions

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Security.Claims; using System.Security.Claims;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
@ -54,7 +55,7 @@ namespace Microsoft.AspNetCore.Authentication
/// <typeparam name="THandler">The <see cref="IAuthenticationHandler"/> responsible for the scheme.</typeparam> /// <typeparam name="THandler">The <see cref="IAuthenticationHandler"/> responsible for the scheme.</typeparam>
/// <param name="name">The name of the scheme being added.</param> /// <param name="name">The name of the scheme being added.</param>
/// <param name="displayName">The display name for the scheme.</param> /// <param name="displayName">The display name for the scheme.</param>
public void AddScheme<THandler>(string name, string displayName) where THandler : IAuthenticationHandler public void AddScheme<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]THandler>(string name, string displayName) where THandler : IAuthenticationHandler
=> AddScheme(name, b => => AddScheme(name, b =>
{ {
b.DisplayName = displayName; b.DisplayName = displayName;

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Diagnostics.CodeAnalysis;
namespace Microsoft.AspNetCore.Authentication namespace Microsoft.AspNetCore.Authentication
{ {
@ -17,7 +18,7 @@ namespace Microsoft.AspNetCore.Authentication
/// <param name="name">The name for the authentication scheme.</param> /// <param name="name">The name for the authentication scheme.</param>
/// <param name="displayName">The display name for the authentication scheme.</param> /// <param name="displayName">The display name for the authentication scheme.</param>
/// <param name="handlerType">The <see cref="IAuthenticationHandler"/> type that handles this scheme.</param> /// <param name="handlerType">The <see cref="IAuthenticationHandler"/> type that handles this scheme.</param>
public AuthenticationScheme(string name, string? displayName, Type handlerType) public AuthenticationScheme(string name, string? displayName, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type handlerType)
{ {
if (name == null) if (name == null)
{ {
@ -50,6 +51,7 @@ namespace Microsoft.AspNetCore.Authentication
/// <summary> /// <summary>
/// The <see cref="IAuthenticationHandler"/> type that handles this scheme. /// The <see cref="IAuthenticationHandler"/> type that handles this scheme.
/// </summary> /// </summary>
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
public Type HandlerType { get; } public Type HandlerType { get; }
} }
} }

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Diagnostics.CodeAnalysis;
namespace Microsoft.AspNetCore.Authentication namespace Microsoft.AspNetCore.Authentication
{ {
@ -32,6 +33,7 @@ namespace Microsoft.AspNetCore.Authentication
/// <summary> /// <summary>
/// The <see cref="IAuthenticationHandler"/> type responsible for this scheme. /// The <see cref="IAuthenticationHandler"/> type responsible for this scheme.
/// </summary> /// </summary>
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
public Type? HandlerType { get; set; } public Type? HandlerType { get; set; }
/// <summary> /// <summary>

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
@ -25,7 +26,7 @@ namespace Microsoft.AspNetCore.Authentication
/// </summary> /// </summary>
public virtual IServiceCollection Services { get; } public virtual IServiceCollection Services { get; }
private AuthenticationBuilder AddSchemeHelper<TOptions, THandler>(string authenticationScheme, string? displayName, Action<TOptions>? configureOptions) private AuthenticationBuilder AddSchemeHelper<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]THandler>(string authenticationScheme, string? displayName, Action<TOptions>? configureOptions)
where TOptions : AuthenticationSchemeOptions, new() where TOptions : AuthenticationSchemeOptions, new()
where THandler : class, IAuthenticationHandler where THandler : class, IAuthenticationHandler
{ {
@ -57,7 +58,7 @@ namespace Microsoft.AspNetCore.Authentication
/// <param name="displayName">The display name of this scheme.</param> /// <param name="displayName">The display name of this scheme.</param>
/// <param name="configureOptions">Used to configure the scheme options.</param> /// <param name="configureOptions">Used to configure the scheme options.</param>
/// <returns>The builder.</returns> /// <returns>The builder.</returns>
public virtual AuthenticationBuilder AddScheme<TOptions, THandler>(string authenticationScheme, string? displayName, Action<TOptions>? configureOptions) public virtual AuthenticationBuilder AddScheme<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]THandler>(string authenticationScheme, string? displayName, Action<TOptions>? configureOptions)
where TOptions : AuthenticationSchemeOptions, new() where TOptions : AuthenticationSchemeOptions, new()
where THandler : AuthenticationHandler<TOptions> where THandler : AuthenticationHandler<TOptions>
=> AddSchemeHelper<TOptions, THandler>(authenticationScheme, displayName, configureOptions); => AddSchemeHelper<TOptions, THandler>(authenticationScheme, displayName, configureOptions);
@ -70,7 +71,7 @@ namespace Microsoft.AspNetCore.Authentication
/// <param name="authenticationScheme">The name of this scheme.</param> /// <param name="authenticationScheme">The name of this scheme.</param>
/// <param name="configureOptions">Used to configure the scheme options.</param> /// <param name="configureOptions">Used to configure the scheme options.</param>
/// <returns>The builder.</returns> /// <returns>The builder.</returns>
public virtual AuthenticationBuilder AddScheme<TOptions, THandler>(string authenticationScheme, Action<TOptions>? configureOptions) public virtual AuthenticationBuilder AddScheme<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]THandler>(string authenticationScheme, Action<TOptions>? configureOptions)
where TOptions : AuthenticationSchemeOptions, new() where TOptions : AuthenticationSchemeOptions, new()
where THandler : AuthenticationHandler<TOptions> where THandler : AuthenticationHandler<TOptions>
=> AddScheme<TOptions, THandler>(authenticationScheme, displayName: null, configureOptions: configureOptions); => AddScheme<TOptions, THandler>(authenticationScheme, displayName: null, configureOptions: configureOptions);
@ -85,7 +86,7 @@ namespace Microsoft.AspNetCore.Authentication
/// <param name="displayName">The display name of this scheme.</param> /// <param name="displayName">The display name of this scheme.</param>
/// <param name="configureOptions">Used to configure the scheme options.</param> /// <param name="configureOptions">Used to configure the scheme options.</param>
/// <returns>The builder.</returns> /// <returns>The builder.</returns>
public virtual AuthenticationBuilder AddRemoteScheme<TOptions, THandler>(string authenticationScheme, string? displayName, Action<TOptions>? configureOptions) public virtual AuthenticationBuilder AddRemoteScheme<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]THandler>(string authenticationScheme, string? displayName, Action<TOptions>? configureOptions)
where TOptions : RemoteAuthenticationOptions, new() where TOptions : RemoteAuthenticationOptions, new()
where THandler : RemoteAuthenticationHandler<TOptions> where THandler : RemoteAuthenticationHandler<TOptions>
{ {

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.OAuth; using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
@ -17,12 +18,12 @@ namespace Microsoft.Extensions.DependencyInjection
public static AuthenticationBuilder AddOAuth(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action<OAuthOptions> configureOptions) public static AuthenticationBuilder AddOAuth(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action<OAuthOptions> configureOptions)
=> builder.AddOAuth<OAuthOptions, OAuthHandler<OAuthOptions>>(authenticationScheme, displayName, configureOptions); => builder.AddOAuth<OAuthOptions, OAuthHandler<OAuthOptions>>(authenticationScheme, displayName, configureOptions);
public static AuthenticationBuilder AddOAuth<TOptions, THandler>(this AuthenticationBuilder builder, string authenticationScheme, Action<TOptions> configureOptions) public static AuthenticationBuilder AddOAuth<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]THandler>(this AuthenticationBuilder builder, string authenticationScheme, Action<TOptions> configureOptions)
where TOptions : OAuthOptions, new() where TOptions : OAuthOptions, new()
where THandler : OAuthHandler<TOptions> where THandler : OAuthHandler<TOptions>
=> builder.AddOAuth<TOptions, THandler>(authenticationScheme, OAuthDefaults.DisplayName, configureOptions); => builder.AddOAuth<TOptions, THandler>(authenticationScheme, OAuthDefaults.DisplayName, configureOptions);
public static AuthenticationBuilder AddOAuth<TOptions, THandler>(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action<TOptions> configureOptions) public static AuthenticationBuilder AddOAuth<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]THandler>(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action<TOptions> configureOptions)
where TOptions : OAuthOptions, new() where TOptions : OAuthOptions, new()
where THandler : OAuthHandler<TOptions> where THandler : OAuthHandler<TOptions>
{ {