Skip to content

Commit 1f76cce

Browse files
authored
Add overloads to CookieAuthentication to config options with services (#19268)
* Add overloads to CookieAuthentication to config options with services * Update reference assembly * Use the new method in sampple * Add overloads for other authentication providers * Update ref assemblies * Change IServiceProvider to TService
1 parent 3ec1676 commit 1f76cce

18 files changed

+450
-25
lines changed

src/Identity/Core/ref/Microsoft.AspNetCore.Identity.netcoreapp.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ public static partial class IdentityServiceCollectionExtensions
188188
public static Microsoft.AspNetCore.Identity.IdentityBuilder AddIdentity<TUser, TRole>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services) where TUser : class where TRole : class { throw null; }
189189
public static Microsoft.AspNetCore.Identity.IdentityBuilder AddIdentity<TUser, TRole>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.AspNetCore.Identity.IdentityOptions> setupAction) where TUser : class where TRole : class { throw null; }
190190
public static Microsoft.Extensions.DependencyInjection.IServiceCollection ConfigureApplicationCookie(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationOptions> configure) { throw null; }
191+
public static Microsoft.Extensions.DependencyInjection.IServiceCollection ConfigureApplicationCookie<TService>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationOptions, TService> configure) where TService : class { throw null; }
191192
public static Microsoft.Extensions.DependencyInjection.IServiceCollection ConfigureExternalCookie(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationOptions> configure) { throw null; }
193+
public static Microsoft.Extensions.DependencyInjection.IServiceCollection ConfigureExternalCookie<TService>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationOptions, TService> configure) where TService : class { throw null; }
192194
}
193195
}

src/Identity/Core/src/IdentityServiceCollectionExtensions.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,21 @@ public static IdentityBuilder AddIdentity<TUser, TRole>(
110110
public static IServiceCollection ConfigureApplicationCookie(this IServiceCollection services, Action<CookieAuthenticationOptions> configure)
111111
=> services.Configure(IdentityConstants.ApplicationScheme, configure);
112112

113+
/// <summary>
114+
/// Configures the application cookie.
115+
/// </summary>
116+
/// <typeparam name="TService">TService: A service resolved from the IServiceProvider for use when configuring this authentication provider. If you need multiple services then specify IServiceProvider and resolve them directly.</typeparam>
117+
/// <param name="services">The services available in the application.</param>
118+
/// <param name="configure">An action to configure the <see cref="CookieAuthenticationOptions"/>.</param>
119+
/// <returns>The services.</returns>
120+
public static IServiceCollection ConfigureApplicationCookie<TService>(this IServiceCollection services, Action<CookieAuthenticationOptions, TService> configure) where TService : class
121+
{
122+
services.AddOptions<CookieAuthenticationOptions>(IdentityConstants.ApplicationScheme)
123+
.Configure(configure);
124+
125+
return services;
126+
}
127+
113128
/// <summary>
114129
/// Configure the external cookie.
115130
/// </summary>
@@ -118,5 +133,20 @@ public static IServiceCollection ConfigureApplicationCookie(this IServiceCollect
118133
/// <returns>The services.</returns>
119134
public static IServiceCollection ConfigureExternalCookie(this IServiceCollection services, Action<CookieAuthenticationOptions> configure)
120135
=> services.Configure(IdentityConstants.ExternalScheme, configure);
136+
137+
/// <summary>
138+
/// Configure the external cookie.
139+
/// </summary>
140+
/// <typeparam name="TService">TService: A service resolved from the IServiceProvider for use when configuring this authentication provider. If you need multiple services then specify IServiceProvider and resolve them directly.</typeparam>
141+
/// <param name="services">The services available in the application.</param>
142+
/// <param name="configure">An action to configure the <see cref="CookieAuthenticationOptions"/>.</param>
143+
/// <returns>The services.</returns>
144+
public static IServiceCollection ConfigureExternalCookie<TService>(this IServiceCollection services, Action<CookieAuthenticationOptions, TService> configure) where TService : class
145+
{
146+
services.AddOptions<CookieAuthenticationOptions>(IdentityConstants.ExternalScheme)
147+
.Configure(configure);
148+
149+
return services;
150+
}
121151
}
122152
}

src/Security/Authentication/Certificate/src/CertificateAuthenticationExtensions.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static AuthenticationBuilder AddCertificate(this AuthenticationBuilder bu
2828
/// <param name="authenticationScheme"></param>
2929
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
3030
public static AuthenticationBuilder AddCertificate(this AuthenticationBuilder builder, string authenticationScheme)
31-
=> builder.AddCertificate(authenticationScheme, configureOptions: null);
31+
=> builder.AddCertificate(authenticationScheme, configureOptions: (Action<CertificateAuthenticationOptions, IServiceProvider>)null);
3232

3333
/// <summary>
3434
/// Adds certificate authentication.
@@ -39,6 +39,16 @@ public static AuthenticationBuilder AddCertificate(this AuthenticationBuilder bu
3939
public static AuthenticationBuilder AddCertificate(this AuthenticationBuilder builder, Action<CertificateAuthenticationOptions> configureOptions)
4040
=> builder.AddCertificate(CertificateAuthenticationDefaults.AuthenticationScheme, configureOptions);
4141

42+
/// <summary>
43+
/// Adds certificate authentication.
44+
/// </summary>
45+
/// <typeparam name="TService">TService: A service resolved from the IServiceProvider for use when configuring this authentication provider. If you need multiple services then specify IServiceProvider and resolve them directly.</typeparam>
46+
/// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
47+
/// <param name="configureOptions"></param>
48+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
49+
public static AuthenticationBuilder AddCertificate<TService>(this AuthenticationBuilder builder, Action<CertificateAuthenticationOptions, TService> configureOptions) where TService : class
50+
=> builder.AddCertificate(CertificateAuthenticationDefaults.AuthenticationScheme, configureOptions);
51+
4252
/// <summary>
4353
/// Adds certificate authentication.
4454
/// </summary>
@@ -50,6 +60,32 @@ public static AuthenticationBuilder AddCertificate(
5060
this AuthenticationBuilder builder,
5161
string authenticationScheme,
5262
Action<CertificateAuthenticationOptions> configureOptions)
53-
=> builder.AddScheme<CertificateAuthenticationOptions, CertificateAuthenticationHandler>(authenticationScheme, configureOptions);
63+
{
64+
Action<CertificateAuthenticationOptions, IServiceProvider> configureOptionsWithServices;
65+
if (configureOptions == null)
66+
{
67+
configureOptionsWithServices = null;
68+
}
69+
else
70+
{
71+
configureOptionsWithServices = (options, _) => configureOptions(options);
72+
}
73+
74+
return builder.AddCertificate(authenticationScheme, configureOptionsWithServices);
75+
}
76+
77+
/// <summary>
78+
/// Adds certificate authentication.
79+
/// </summary>
80+
/// <typeparam name="TService">TService: A service resolved from the IServiceProvider for use when configuring this authentication provider. If you need multiple services then specify IServiceProvider and resolve them directly.</typeparam>
81+
/// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
82+
/// <param name="authenticationScheme"></param>
83+
/// <param name="configureOptions"></param>
84+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
85+
public static AuthenticationBuilder AddCertificate<TService>(
86+
this AuthenticationBuilder builder,
87+
string authenticationScheme,
88+
Action<CertificateAuthenticationOptions, TService> configureOptions) where TService : class
89+
=> builder.AddScheme<CertificateAuthenticationOptions, CertificateAuthenticationHandler, TService>(authenticationScheme, configureOptions);
5490
}
5591
}

src/Security/Authentication/Cookies/ref/Microsoft.AspNetCore.Authentication.Cookies.netcoreapp.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,8 @@ public static partial class CookieExtensions
126126
public static Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddCookie(this Microsoft.AspNetCore.Authentication.AuthenticationBuilder builder, string authenticationScheme) { throw null; }
127127
public static Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddCookie(this Microsoft.AspNetCore.Authentication.AuthenticationBuilder builder, string authenticationScheme, System.Action<Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationOptions> configureOptions) { throw null; }
128128
public static Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddCookie(this Microsoft.AspNetCore.Authentication.AuthenticationBuilder builder, string authenticationScheme, string displayName, System.Action<Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationOptions> configureOptions) { throw null; }
129+
public static Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddCookie<TService>(this Microsoft.AspNetCore.Authentication.AuthenticationBuilder builder, System.Action<Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationOptions, TService> configureOptions) where TService : class { throw null; }
130+
public static Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddCookie<TService>(this Microsoft.AspNetCore.Authentication.AuthenticationBuilder builder, string authenticationScheme, System.Action<Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationOptions, TService> configureOptions) where TService : class { throw null; }
131+
public static Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddCookie<TService>(this Microsoft.AspNetCore.Authentication.AuthenticationBuilder builder, string authenticationScheme, string displayName, System.Action<Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationOptions, TService> configureOptions) where TService : class { throw null; }
129132
}
130133
}

src/Security/Authentication/Cookies/samples/CookieSessionSample/Startup.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ public class Startup
1414
{
1515
public void ConfigureServices(IServiceCollection services)
1616
{
17+
services.AddSingleton<MemoryCacheTicketStore>();
18+
1719
// This can be removed after https://github.com/aspnet/IISIntegration/issues/371
1820
services.AddAuthentication(options =>
1921
{
2022
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
2123
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
22-
}).AddCookie(o => o.SessionStore = new MemoryCacheTicketStore());
24+
}).AddCookie<MemoryCacheTicketStore>((o, ticketStore) => o.SessionStore = ticketStore);
2325
}
2426

2527
public void Configure(IApplicationBuilder app)

src/Security/Authentication/Cookies/src/CookieExtensions.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,40 @@ public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder
1515
=> builder.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme);
1616

1717
public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, string authenticationScheme)
18-
=> builder.AddCookie(authenticationScheme, configureOptions: null);
18+
=> builder.AddCookie(authenticationScheme, configureOptions: (Action<CookieAuthenticationOptions, IServiceProvider>)null);
1919

20-
public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, Action<CookieAuthenticationOptions> configureOptions)
20+
public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, Action<CookieAuthenticationOptions> configureOptions)
21+
=> builder.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, configureOptions);
22+
23+
public static AuthenticationBuilder AddCookie<TService>(this AuthenticationBuilder builder, Action<CookieAuthenticationOptions, TService> configureOptions) where TService : class
2124
=> builder.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, configureOptions);
2225

2326
public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, string authenticationScheme, Action<CookieAuthenticationOptions> configureOptions)
2427
=> builder.AddCookie(authenticationScheme, displayName: null, configureOptions: configureOptions);
2528

29+
public static AuthenticationBuilder AddCookie<TService>(this AuthenticationBuilder builder, string authenticationScheme, Action<CookieAuthenticationOptions, TService> configureOptions) where TService : class
30+
=> builder.AddCookie(authenticationScheme, displayName: null, configureOptions: configureOptions);
31+
2632
public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action<CookieAuthenticationOptions> configureOptions)
33+
{
34+
Action<CookieAuthenticationOptions, IServiceProvider> configureOptionsWithServices;
35+
if (configureOptions == null)
36+
{
37+
configureOptionsWithServices = null;
38+
}
39+
else
40+
{
41+
configureOptionsWithServices = (options, _) => configureOptions(options);
42+
}
43+
44+
return builder.AddCookie(authenticationScheme, displayName, configureOptionsWithServices);
45+
}
46+
47+
public static AuthenticationBuilder AddCookie<TService>(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action<CookieAuthenticationOptions, TService> configureOptions) where TService : class
2748
{
2849
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IPostConfigureOptions<CookieAuthenticationOptions>, PostConfigureCookieAuthenticationOptions>());
2950
builder.Services.AddOptions<CookieAuthenticationOptions>(authenticationScheme).Validate(o => o.Cookie.Expiration == null, "Cookie.Expiration is ignored, use ExpireTimeSpan instead.");
30-
return builder.AddScheme<CookieAuthenticationOptions, CookieAuthenticationHandler>(authenticationScheme, displayName, configureOptions);
51+
return builder.AddScheme<CookieAuthenticationOptions, CookieAuthenticationHandler, TService>(authenticationScheme, displayName, configureOptions);
3152
}
3253
}
3354
}

src/Security/Authentication/Core/ref/Microsoft.AspNetCore.Authentication.netcoreapp.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ public partial class AuthenticationBuilder
1616
public AuthenticationBuilder(Microsoft.Extensions.DependencyInjection.IServiceCollection services) { }
1717
public virtual Microsoft.Extensions.DependencyInjection.IServiceCollection Services { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
1818
public virtual Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddPolicyScheme(string authenticationScheme, string displayName, System.Action<Microsoft.AspNetCore.Authentication.PolicySchemeOptions> configureOptions) { throw null; }
19+
public virtual Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddPolicyScheme<TService>(string authenticationScheme, string displayName, System.Action<Microsoft.AspNetCore.Authentication.PolicySchemeOptions, TService> configureOptions) where TService : class { throw null; }
1920
public virtual Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddRemoteScheme<TOptions, THandler>(string authenticationScheme, string displayName, System.Action<TOptions> configureOptions) where TOptions : Microsoft.AspNetCore.Authentication.RemoteAuthenticationOptions, new() where THandler : Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler<TOptions> { throw null; }
21+
public virtual Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddRemoteScheme<TOptions, THandler, TService>(string authenticationScheme, string displayName, System.Action<TOptions, TService> configureOptions) where TOptions : Microsoft.AspNetCore.Authentication.RemoteAuthenticationOptions, new() where THandler : Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler<TOptions> where TService : class { throw null; }
2022
public virtual Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddScheme<TOptions, THandler>(string authenticationScheme, System.Action<TOptions> configureOptions) where TOptions : Microsoft.AspNetCore.Authentication.AuthenticationSchemeOptions, new() where THandler : Microsoft.AspNetCore.Authentication.AuthenticationHandler<TOptions> { throw null; }
2123
public virtual Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddScheme<TOptions, THandler>(string authenticationScheme, string displayName, System.Action<TOptions> configureOptions) where TOptions : Microsoft.AspNetCore.Authentication.AuthenticationSchemeOptions, new() where THandler : Microsoft.AspNetCore.Authentication.AuthenticationHandler<TOptions> { throw null; }
24+
public virtual Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddScheme<TOptions, THandler, TService>(string authenticationScheme, System.Action<TOptions, TService> configureOptions) where TOptions : Microsoft.AspNetCore.Authentication.AuthenticationSchemeOptions, new() where THandler : Microsoft.AspNetCore.Authentication.AuthenticationHandler<TOptions> where TService : class { throw null; }
25+
public virtual Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddScheme<TOptions, THandler, TService>(string authenticationScheme, string displayName, System.Action<TOptions, TService> configureOptions) where TOptions : Microsoft.AspNetCore.Authentication.AuthenticationSchemeOptions, new() where THandler : Microsoft.AspNetCore.Authentication.AuthenticationHandler<TOptions> where TService : class { throw null; }
2226
}
2327
public abstract partial class AuthenticationHandler<TOptions> : Microsoft.AspNetCore.Authentication.IAuthenticationHandler where TOptions : Microsoft.AspNetCore.Authentication.AuthenticationSchemeOptions, new()
2428
{

0 commit comments

Comments
 (0)