Skip to content

Commit 0e592df

Browse files
authored
Make authentication linker friendly (#24708)
* Make authentication linker friendly - Preserve constructors wherever open generics or type arguments exist
1 parent 927ddab commit 0e592df

File tree

5 files changed

+15
-8
lines changed

5 files changed

+15
-8
lines changed

src/Http/Authentication.Abstractions/src/AuthenticationOptions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Security.Claims;
78
using Microsoft.AspNetCore.Http;
89

@@ -54,7 +55,7 @@ public void AddScheme(string name, Action<AuthenticationSchemeBuilder> configure
5455
/// <typeparam name="THandler">The <see cref="IAuthenticationHandler"/> responsible for the scheme.</typeparam>
5556
/// <param name="name">The name of the scheme being added.</param>
5657
/// <param name="displayName">The display name for the scheme.</param>
57-
public void AddScheme<THandler>(string name, string displayName) where THandler : IAuthenticationHandler
58+
public void AddScheme<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]THandler>(string name, string displayName) where THandler : IAuthenticationHandler
5859
=> AddScheme(name, b =>
5960
{
6061
b.DisplayName = displayName;

src/Http/Authentication.Abstractions/src/AuthenticationScheme.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56

67
namespace Microsoft.AspNetCore.Authentication
78
{
@@ -17,7 +18,7 @@ public class AuthenticationScheme
1718
/// <param name="name">The name for the authentication scheme.</param>
1819
/// <param name="displayName">The display name for the authentication scheme.</param>
1920
/// <param name="handlerType">The <see cref="IAuthenticationHandler"/> type that handles this scheme.</param>
20-
public AuthenticationScheme(string name, string? displayName, Type handlerType)
21+
public AuthenticationScheme(string name, string? displayName, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type handlerType)
2122
{
2223
if (name == null)
2324
{
@@ -50,6 +51,7 @@ public AuthenticationScheme(string name, string? displayName, Type handlerType)
5051
/// <summary>
5152
/// The <see cref="IAuthenticationHandler"/> type that handles this scheme.
5253
/// </summary>
54+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
5355
public Type HandlerType { get; }
5456
}
5557
}

src/Http/Authentication.Abstractions/src/AuthenticationSchemeBuilder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56

67
namespace Microsoft.AspNetCore.Authentication
78
{
@@ -32,6 +33,7 @@ public AuthenticationSchemeBuilder(string name)
3233
/// <summary>
3334
/// The <see cref="IAuthenticationHandler"/> type responsible for this scheme.
3435
/// </summary>
36+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
3537
public Type? HandlerType { get; set; }
3638

3739
/// <summary>

src/Security/Authentication/Core/src/AuthenticationBuilder.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using Microsoft.Extensions.DependencyInjection;
67
using Microsoft.Extensions.DependencyInjection.Extensions;
78
using Microsoft.Extensions.Options;
@@ -25,7 +26,7 @@ public AuthenticationBuilder(IServiceCollection services)
2526
/// </summary>
2627
public virtual IServiceCollection Services { get; }
2728

28-
private AuthenticationBuilder AddSchemeHelper<TOptions, THandler>(string authenticationScheme, string? displayName, Action<TOptions>? configureOptions)
29+
private AuthenticationBuilder AddSchemeHelper<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]THandler>(string authenticationScheme, string? displayName, Action<TOptions>? configureOptions)
2930
where TOptions : AuthenticationSchemeOptions, new()
3031
where THandler : class, IAuthenticationHandler
3132
{
@@ -57,7 +58,7 @@ private AuthenticationBuilder AddSchemeHelper<TOptions, THandler>(string authent
5758
/// <param name="displayName">The display name of this scheme.</param>
5859
/// <param name="configureOptions">Used to configure the scheme options.</param>
5960
/// <returns>The builder.</returns>
60-
public virtual AuthenticationBuilder AddScheme<TOptions, THandler>(string authenticationScheme, string? displayName, Action<TOptions>? configureOptions)
61+
public virtual AuthenticationBuilder AddScheme<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]THandler>(string authenticationScheme, string? displayName, Action<TOptions>? configureOptions)
6162
where TOptions : AuthenticationSchemeOptions, new()
6263
where THandler : AuthenticationHandler<TOptions>
6364
=> AddSchemeHelper<TOptions, THandler>(authenticationScheme, displayName, configureOptions);
@@ -70,7 +71,7 @@ public virtual AuthenticationBuilder AddScheme<TOptions, THandler>(string authen
7071
/// <param name="authenticationScheme">The name of this scheme.</param>
7172
/// <param name="configureOptions">Used to configure the scheme options.</param>
7273
/// <returns>The builder.</returns>
73-
public virtual AuthenticationBuilder AddScheme<TOptions, THandler>(string authenticationScheme, Action<TOptions>? configureOptions)
74+
public virtual AuthenticationBuilder AddScheme<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]THandler>(string authenticationScheme, Action<TOptions>? configureOptions)
7475
where TOptions : AuthenticationSchemeOptions, new()
7576
where THandler : AuthenticationHandler<TOptions>
7677
=> AddScheme<TOptions, THandler>(authenticationScheme, displayName: null, configureOptions: configureOptions);
@@ -85,7 +86,7 @@ public virtual AuthenticationBuilder AddScheme<TOptions, THandler>(string authen
8586
/// <param name="displayName">The display name of this scheme.</param>
8687
/// <param name="configureOptions">Used to configure the scheme options.</param>
8788
/// <returns>The builder.</returns>
88-
public virtual AuthenticationBuilder AddRemoteScheme<TOptions, THandler>(string authenticationScheme, string? displayName, Action<TOptions>? configureOptions)
89+
public virtual AuthenticationBuilder AddRemoteScheme<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]THandler>(string authenticationScheme, string? displayName, Action<TOptions>? configureOptions)
8990
where TOptions : RemoteAuthenticationOptions, new()
9091
where THandler : RemoteAuthenticationHandler<TOptions>
9192
{

src/Security/Authentication/OAuth/src/OAuthExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using Microsoft.AspNetCore.Authentication;
67
using Microsoft.AspNetCore.Authentication.OAuth;
78
using Microsoft.Extensions.DependencyInjection.Extensions;
@@ -17,12 +18,12 @@ public static AuthenticationBuilder AddOAuth(this AuthenticationBuilder builder,
1718
public static AuthenticationBuilder AddOAuth(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action<OAuthOptions> configureOptions)
1819
=> builder.AddOAuth<OAuthOptions, OAuthHandler<OAuthOptions>>(authenticationScheme, displayName, configureOptions);
1920

20-
public static AuthenticationBuilder AddOAuth<TOptions, THandler>(this AuthenticationBuilder builder, string authenticationScheme, Action<TOptions> configureOptions)
21+
public static AuthenticationBuilder AddOAuth<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]THandler>(this AuthenticationBuilder builder, string authenticationScheme, Action<TOptions> configureOptions)
2122
where TOptions : OAuthOptions, new()
2223
where THandler : OAuthHandler<TOptions>
2324
=> builder.AddOAuth<TOptions, THandler>(authenticationScheme, OAuthDefaults.DisplayName, configureOptions);
2425

25-
public static AuthenticationBuilder AddOAuth<TOptions, THandler>(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action<TOptions> configureOptions)
26+
public static AuthenticationBuilder AddOAuth<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]THandler>(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action<TOptions> configureOptions)
2627
where TOptions : OAuthOptions, new()
2728
where THandler : OAuthHandler<TOptions>
2829
{

0 commit comments

Comments
 (0)