From 1f76cce14ac4e4698a554b65a24f28000b50396e Mon Sep 17 00:00:00 2001 From: Kahbazi Date: Tue, 7 Apr 2020 01:53:49 +0430 Subject: [PATCH] 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 --- ...icrosoft.AspNetCore.Identity.netcoreapp.cs | 2 + .../IdentityServiceCollectionExtensions.cs | 30 ++++++ .../CertificateAuthenticationExtensions.cs | 40 +++++++- ...tCore.Authentication.Cookies.netcoreapp.cs | 3 + .../samples/CookieSessionSample/Startup.cs | 4 +- .../Cookies/src/CookieExtensions.cs | 27 +++++- ...ft.AspNetCore.Authentication.netcoreapp.cs | 4 + .../Core/src/AuthenticationBuilder.cs | 95 +++++++++++++++++-- .../Facebook/src/FacebookExtensions.cs | 23 ++++- .../Google/src/GoogleExtensions.cs | 23 ++++- .../JwtBearer/src/JwtBearerExtensions.cs | 23 ++++- .../src/MicrosoftAccountExtensions.cs | 25 ++++- .../Negotiate/src/NegotiateExtensions.cs | 47 ++++++++- ...NetCore.Authentication.OAuth.netcoreapp.cs | 4 + .../OAuth/src/OAuthExtensions.cs | 32 ++++++- .../src/OpenIdConnectExtensions.cs | 23 ++++- .../Twitter/src/TwitterExtensions.cs | 23 ++++- .../src/WsFederationExtensions.cs | 47 ++++++++- 18 files changed, 450 insertions(+), 25 deletions(-) diff --git a/src/Identity/Core/ref/Microsoft.AspNetCore.Identity.netcoreapp.cs b/src/Identity/Core/ref/Microsoft.AspNetCore.Identity.netcoreapp.cs index 0ccafd325b..61b1891d26 100644 --- a/src/Identity/Core/ref/Microsoft.AspNetCore.Identity.netcoreapp.cs +++ b/src/Identity/Core/ref/Microsoft.AspNetCore.Identity.netcoreapp.cs @@ -188,6 +188,8 @@ namespace Microsoft.Extensions.DependencyInjection public static Microsoft.AspNetCore.Identity.IdentityBuilder AddIdentity(this Microsoft.Extensions.DependencyInjection.IServiceCollection services) where TUser : class where TRole : class { throw null; } public static Microsoft.AspNetCore.Identity.IdentityBuilder AddIdentity(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action setupAction) where TUser : class where TRole : class { throw null; } public static Microsoft.Extensions.DependencyInjection.IServiceCollection ConfigureApplicationCookie(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action configure) { throw null; } + public static Microsoft.Extensions.DependencyInjection.IServiceCollection ConfigureApplicationCookie(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action configure) where TService : class { throw null; } public static Microsoft.Extensions.DependencyInjection.IServiceCollection ConfigureExternalCookie(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action configure) { throw null; } + public static Microsoft.Extensions.DependencyInjection.IServiceCollection ConfigureExternalCookie(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action configure) where TService : class { throw null; } } } diff --git a/src/Identity/Core/src/IdentityServiceCollectionExtensions.cs b/src/Identity/Core/src/IdentityServiceCollectionExtensions.cs index 45e3d567eb..ffbc9f563e 100644 --- a/src/Identity/Core/src/IdentityServiceCollectionExtensions.cs +++ b/src/Identity/Core/src/IdentityServiceCollectionExtensions.cs @@ -110,6 +110,21 @@ namespace Microsoft.Extensions.DependencyInjection public static IServiceCollection ConfigureApplicationCookie(this IServiceCollection services, Action configure) => services.Configure(IdentityConstants.ApplicationScheme, configure); + /// + /// Configures the application cookie. + /// + /// 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. + /// The services available in the application. + /// An action to configure the . + /// The services. + public static IServiceCollection ConfigureApplicationCookie(this IServiceCollection services, Action configure) where TService : class + { + services.AddOptions(IdentityConstants.ApplicationScheme) + .Configure(configure); + + return services; + } + /// /// Configure the external cookie. /// @@ -118,5 +133,20 @@ namespace Microsoft.Extensions.DependencyInjection /// The services. public static IServiceCollection ConfigureExternalCookie(this IServiceCollection services, Action configure) => services.Configure(IdentityConstants.ExternalScheme, configure); + + /// + /// Configure the external cookie. + /// + /// 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. + /// The services available in the application. + /// An action to configure the . + /// The services. + public static IServiceCollection ConfigureExternalCookie(this IServiceCollection services, Action configure) where TService : class + { + services.AddOptions(IdentityConstants.ExternalScheme) + .Configure(configure); + + return services; + } } } diff --git a/src/Security/Authentication/Certificate/src/CertificateAuthenticationExtensions.cs b/src/Security/Authentication/Certificate/src/CertificateAuthenticationExtensions.cs index d49f2c274b..4926a21382 100644 --- a/src/Security/Authentication/Certificate/src/CertificateAuthenticationExtensions.cs +++ b/src/Security/Authentication/Certificate/src/CertificateAuthenticationExtensions.cs @@ -28,7 +28,7 @@ namespace Microsoft.Extensions.DependencyInjection /// /// The . public static AuthenticationBuilder AddCertificate(this AuthenticationBuilder builder, string authenticationScheme) - => builder.AddCertificate(authenticationScheme, configureOptions: null); + => builder.AddCertificate(authenticationScheme, configureOptions: (Action)null); /// /// Adds certificate authentication. @@ -39,6 +39,16 @@ namespace Microsoft.Extensions.DependencyInjection public static AuthenticationBuilder AddCertificate(this AuthenticationBuilder builder, Action configureOptions) => builder.AddCertificate(CertificateAuthenticationDefaults.AuthenticationScheme, configureOptions); + /// + /// Adds certificate authentication. + /// + /// 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. + /// The . + /// + /// The . + public static AuthenticationBuilder AddCertificate(this AuthenticationBuilder builder, Action configureOptions) where TService : class + => builder.AddCertificate(CertificateAuthenticationDefaults.AuthenticationScheme, configureOptions); + /// /// Adds certificate authentication. /// @@ -50,6 +60,32 @@ namespace Microsoft.Extensions.DependencyInjection this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) - => builder.AddScheme(authenticationScheme, configureOptions); + { + Action configureOptionsWithServices; + if (configureOptions == null) + { + configureOptionsWithServices = null; + } + else + { + configureOptionsWithServices = (options, _) => configureOptions(options); + } + + return builder.AddCertificate(authenticationScheme, configureOptionsWithServices); + } + + /// + /// Adds certificate authentication. + /// + /// 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. + /// The . + /// + /// + /// The . + public static AuthenticationBuilder AddCertificate( + this AuthenticationBuilder builder, + string authenticationScheme, + Action configureOptions) where TService : class + => builder.AddScheme(authenticationScheme, configureOptions); } } diff --git a/src/Security/Authentication/Cookies/ref/Microsoft.AspNetCore.Authentication.Cookies.netcoreapp.cs b/src/Security/Authentication/Cookies/ref/Microsoft.AspNetCore.Authentication.Cookies.netcoreapp.cs index f3d730a1f9..714bdd2ebd 100644 --- a/src/Security/Authentication/Cookies/ref/Microsoft.AspNetCore.Authentication.Cookies.netcoreapp.cs +++ b/src/Security/Authentication/Cookies/ref/Microsoft.AspNetCore.Authentication.Cookies.netcoreapp.cs @@ -126,5 +126,8 @@ namespace Microsoft.Extensions.DependencyInjection public static Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddCookie(this Microsoft.AspNetCore.Authentication.AuthenticationBuilder builder, string authenticationScheme) { throw null; } public static Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddCookie(this Microsoft.AspNetCore.Authentication.AuthenticationBuilder builder, string authenticationScheme, System.Action configureOptions) { throw null; } public static Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddCookie(this Microsoft.AspNetCore.Authentication.AuthenticationBuilder builder, string authenticationScheme, string displayName, System.Action configureOptions) { throw null; } + public static Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddCookie(this Microsoft.AspNetCore.Authentication.AuthenticationBuilder builder, System.Action configureOptions) where TService : class { throw null; } + public static Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddCookie(this Microsoft.AspNetCore.Authentication.AuthenticationBuilder builder, string authenticationScheme, System.Action configureOptions) where TService : class { throw null; } + public static Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddCookie(this Microsoft.AspNetCore.Authentication.AuthenticationBuilder builder, string authenticationScheme, string displayName, System.Action configureOptions) where TService : class { throw null; } } } diff --git a/src/Security/Authentication/Cookies/samples/CookieSessionSample/Startup.cs b/src/Security/Authentication/Cookies/samples/CookieSessionSample/Startup.cs index f7b8f2bb88..c538866d7e 100644 --- a/src/Security/Authentication/Cookies/samples/CookieSessionSample/Startup.cs +++ b/src/Security/Authentication/Cookies/samples/CookieSessionSample/Startup.cs @@ -14,12 +14,14 @@ namespace CookieSessionSample { public void ConfigureServices(IServiceCollection services) { + services.AddSingleton(); + // This can be removed after https://github.com/aspnet/IISIntegration/issues/371 services.AddAuthentication(options => { options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme; - }).AddCookie(o => o.SessionStore = new MemoryCacheTicketStore()); + }).AddCookie((o, ticketStore) => o.SessionStore = ticketStore); } public void Configure(IApplicationBuilder app) diff --git a/src/Security/Authentication/Cookies/src/CookieExtensions.cs b/src/Security/Authentication/Cookies/src/CookieExtensions.cs index 7763e6a624..a67a708149 100644 --- a/src/Security/Authentication/Cookies/src/CookieExtensions.cs +++ b/src/Security/Authentication/Cookies/src/CookieExtensions.cs @@ -15,19 +15,40 @@ namespace Microsoft.Extensions.DependencyInjection => builder.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme); public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, string authenticationScheme) - => builder.AddCookie(authenticationScheme, configureOptions: null); + => builder.AddCookie(authenticationScheme, configureOptions: (Action)null); - public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, Action configureOptions) + public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, Action configureOptions) + => builder.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, configureOptions); + + public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, Action configureOptions) where TService : class => builder.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, configureOptions); public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) => builder.AddCookie(authenticationScheme, displayName: null, configureOptions: configureOptions); + public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) where TService : class + => builder.AddCookie(authenticationScheme, displayName: null, configureOptions: configureOptions); + public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) + { + Action configureOptionsWithServices; + if (configureOptions == null) + { + configureOptionsWithServices = null; + } + else + { + configureOptionsWithServices = (options, _) => configureOptions(options); + } + + return builder.AddCookie(authenticationScheme, displayName, configureOptionsWithServices); + } + + public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) where TService : class { builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton, PostConfigureCookieAuthenticationOptions>()); builder.Services.AddOptions(authenticationScheme).Validate(o => o.Cookie.Expiration == null, "Cookie.Expiration is ignored, use ExpireTimeSpan instead."); - return builder.AddScheme(authenticationScheme, displayName, configureOptions); + return builder.AddScheme(authenticationScheme, displayName, configureOptions); } } } diff --git a/src/Security/Authentication/Core/ref/Microsoft.AspNetCore.Authentication.netcoreapp.cs b/src/Security/Authentication/Core/ref/Microsoft.AspNetCore.Authentication.netcoreapp.cs index 1a1d21a774..1cf17c4f5f 100644 --- a/src/Security/Authentication/Core/ref/Microsoft.AspNetCore.Authentication.netcoreapp.cs +++ b/src/Security/Authentication/Core/ref/Microsoft.AspNetCore.Authentication.netcoreapp.cs @@ -16,9 +16,13 @@ namespace Microsoft.AspNetCore.Authentication public AuthenticationBuilder(Microsoft.Extensions.DependencyInjection.IServiceCollection services) { } public virtual Microsoft.Extensions.DependencyInjection.IServiceCollection Services { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } public virtual Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddPolicyScheme(string authenticationScheme, string displayName, System.Action configureOptions) { throw null; } + public virtual Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddPolicyScheme(string authenticationScheme, string displayName, System.Action configureOptions) where TService : class { throw null; } public virtual Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddRemoteScheme(string authenticationScheme, string displayName, System.Action configureOptions) where TOptions : Microsoft.AspNetCore.Authentication.RemoteAuthenticationOptions, new() where THandler : Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler { throw null; } + public virtual Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddRemoteScheme(string authenticationScheme, string displayName, System.Action configureOptions) where TOptions : Microsoft.AspNetCore.Authentication.RemoteAuthenticationOptions, new() where THandler : Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler where TService : class { throw null; } public virtual Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddScheme(string authenticationScheme, System.Action configureOptions) where TOptions : Microsoft.AspNetCore.Authentication.AuthenticationSchemeOptions, new() where THandler : Microsoft.AspNetCore.Authentication.AuthenticationHandler { throw null; } public virtual Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddScheme(string authenticationScheme, string displayName, System.Action configureOptions) where TOptions : Microsoft.AspNetCore.Authentication.AuthenticationSchemeOptions, new() where THandler : Microsoft.AspNetCore.Authentication.AuthenticationHandler { throw null; } + public virtual Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddScheme(string authenticationScheme, System.Action configureOptions) where TOptions : Microsoft.AspNetCore.Authentication.AuthenticationSchemeOptions, new() where THandler : Microsoft.AspNetCore.Authentication.AuthenticationHandler where TService : class { throw null; } + public virtual Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddScheme(string authenticationScheme, string displayName, System.Action configureOptions) where TOptions : Microsoft.AspNetCore.Authentication.AuthenticationSchemeOptions, new() where THandler : Microsoft.AspNetCore.Authentication.AuthenticationHandler where TService : class { throw null; } } public abstract partial class AuthenticationHandler : Microsoft.AspNetCore.Authentication.IAuthenticationHandler where TOptions : Microsoft.AspNetCore.Authentication.AuthenticationSchemeOptions, new() { diff --git a/src/Security/Authentication/Core/src/AuthenticationBuilder.cs b/src/Security/Authentication/Core/src/AuthenticationBuilder.cs index d4efd0c847..829fe007d7 100644 --- a/src/Security/Authentication/Core/src/AuthenticationBuilder.cs +++ b/src/Security/Authentication/Core/src/AuthenticationBuilder.cs @@ -25,25 +25,31 @@ namespace Microsoft.AspNetCore.Authentication /// public virtual IServiceCollection Services { get; } - private AuthenticationBuilder AddSchemeHelper(string authenticationScheme, string displayName, Action configureOptions) + private AuthenticationBuilder AddSchemeHelper(string authenticationScheme, string displayName, Action configureOptions) where TService : class where TOptions : AuthenticationSchemeOptions, new() where THandler : class, IAuthenticationHandler { Services.Configure(o => { - o.AddScheme(authenticationScheme, scheme => { + o.AddScheme(authenticationScheme, scheme => + { scheme.HandlerType = typeof(THandler); scheme.DisplayName = displayName; }); }); + + var optionsBuilder = Services.AddOptions(authenticationScheme) + .Validate(o => + { + o.Validate(authenticationScheme); + return true; + }); + if (configureOptions != null) { - Services.Configure(authenticationScheme, configureOptions); + optionsBuilder.Configure(configureOptions); } - Services.AddOptions(authenticationScheme).Validate(o => { - o.Validate(authenticationScheme); - return true; - }); + Services.AddTransient(); return this; } @@ -60,7 +66,22 @@ namespace Microsoft.AspNetCore.Authentication public virtual AuthenticationBuilder AddScheme(string authenticationScheme, string displayName, Action configureOptions) where TOptions : AuthenticationSchemeOptions, new() where THandler : AuthenticationHandler - => AddSchemeHelper(authenticationScheme, displayName, configureOptions); + => AddSchemeHelper(authenticationScheme, displayName, MapConfiguration(configureOptions)); + + /// + /// Adds a which can be used by . + /// + /// The type to configure the handler."/>. + /// The used to handle this scheme. + /// 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. + /// The name of this scheme. + /// The display name of this scheme. + /// Used to configure the scheme options. + /// The builder. + public virtual AuthenticationBuilder AddScheme(string authenticationScheme, string displayName, Action configureOptions) where TService : class + where TOptions : AuthenticationSchemeOptions, new() + where THandler : AuthenticationHandler + => AddSchemeHelper(authenticationScheme, displayName, configureOptions); /// /// Adds a which can be used by . @@ -75,6 +96,20 @@ namespace Microsoft.AspNetCore.Authentication where THandler : AuthenticationHandler => AddScheme(authenticationScheme, displayName: null, configureOptions: configureOptions); + /// + /// Adds a which can be used by . + /// + /// The type to configure the handler."/>. + /// The used to handle this scheme. + /// 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. + /// The name of this scheme. + /// Used to configure the scheme options. + /// The builder. + public virtual AuthenticationBuilder AddScheme(string authenticationScheme, Action configureOptions) where TService : class + where TOptions : AuthenticationSchemeOptions, new() + where THandler : AuthenticationHandler + => AddScheme(authenticationScheme, displayName: null, configureOptions: configureOptions); + /// /// Adds a based that supports remote authentication /// which can be used by . @@ -93,6 +128,25 @@ namespace Microsoft.AspNetCore.Authentication return AddScheme(authenticationScheme, displayName, configureOptions: configureOptions); } + /// + /// Adds a based that supports remote authentication + /// which can be used by . + /// + /// The type to configure the handler."/>. + /// The used to handle this scheme. + /// 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. + /// The name of this scheme. + /// The display name of this scheme. + /// Used to configure the scheme options. + /// The builder. + public virtual AuthenticationBuilder AddRemoteScheme(string authenticationScheme, string displayName, Action configureOptions) where TService : class + where TOptions : RemoteAuthenticationOptions, new() + where THandler : RemoteAuthenticationHandler + { + Services.TryAddEnumerable(ServiceDescriptor.Singleton, EnsureSignInScheme>()); + return AddScheme(authenticationScheme, displayName, configureOptions: configureOptions); + } + /// /// Adds a based authentication handler which can be used to /// redirect to other authentication schemes. @@ -102,7 +156,30 @@ namespace Microsoft.AspNetCore.Authentication /// Used to configure the scheme options. /// The builder. public virtual AuthenticationBuilder AddPolicyScheme(string authenticationScheme, string displayName, Action configureOptions) - => AddSchemeHelper(authenticationScheme, displayName, configureOptions); + => AddSchemeHelper(authenticationScheme, displayName, MapConfiguration(configureOptions)); + + /// + /// Adds a based authentication handler which can be used to + /// redirect to other authentication schemes. + /// + /// The name of this scheme. + /// The display name of this scheme. + /// Used to configure the scheme options. + /// The builder. + public virtual AuthenticationBuilder AddPolicyScheme(string authenticationScheme, string displayName, Action configureOptions) where TService : class + => AddSchemeHelper(authenticationScheme, displayName, configureOptions); + + private Action MapConfiguration(Action configureOptions) + { + if (configureOptions == null) + { + return null; + } + else + { + return (options, _) => configureOptions(options); + } + } // Used to ensure that there's always a default sign in scheme that's not itself private class EnsureSignInScheme : IPostConfigureOptions where TOptions : RemoteAuthenticationOptions diff --git a/src/Security/Authentication/Facebook/src/FacebookExtensions.cs b/src/Security/Authentication/Facebook/src/FacebookExtensions.cs index 2273724a42..b32b35c6c1 100644 --- a/src/Security/Authentication/Facebook/src/FacebookExtensions.cs +++ b/src/Security/Authentication/Facebook/src/FacebookExtensions.cs @@ -15,10 +15,31 @@ namespace Microsoft.Extensions.DependencyInjection public static AuthenticationBuilder AddFacebook(this AuthenticationBuilder builder, Action configureOptions) => builder.AddFacebook(FacebookDefaults.AuthenticationScheme, configureOptions); + public static AuthenticationBuilder AddFacebook(this AuthenticationBuilder builder, Action configureOptions) where TService : class + => builder.AddFacebook(FacebookDefaults.AuthenticationScheme, configureOptions); + public static AuthenticationBuilder AddFacebook(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) => builder.AddFacebook(authenticationScheme, FacebookDefaults.DisplayName, configureOptions); + public static AuthenticationBuilder AddFacebook(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) where TService : class + => builder.AddFacebook(authenticationScheme, FacebookDefaults.DisplayName, configureOptions); + public static AuthenticationBuilder AddFacebook(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) - => builder.AddOAuth(authenticationScheme, displayName, configureOptions); + { + Action configureOptionsWithServices; + if (configureOptions == null) + { + configureOptionsWithServices = null; + } + else + { + configureOptionsWithServices = (options, _) => configureOptions(options); + } + + return builder.AddFacebook(authenticationScheme, displayName, configureOptionsWithServices); + } + + public static AuthenticationBuilder AddFacebook(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) where TService : class + => builder.AddOAuth(authenticationScheme, displayName, configureOptions); } } diff --git a/src/Security/Authentication/Google/src/GoogleExtensions.cs b/src/Security/Authentication/Google/src/GoogleExtensions.cs index 95547014ca..88add9610d 100644 --- a/src/Security/Authentication/Google/src/GoogleExtensions.cs +++ b/src/Security/Authentication/Google/src/GoogleExtensions.cs @@ -15,10 +15,31 @@ namespace Microsoft.Extensions.DependencyInjection public static AuthenticationBuilder AddGoogle(this AuthenticationBuilder builder, Action configureOptions) => builder.AddGoogle(GoogleDefaults.AuthenticationScheme, configureOptions); + public static AuthenticationBuilder AddGoogle(this AuthenticationBuilder builder, Action configureOptions) where TService : class + => builder.AddGoogle(GoogleDefaults.AuthenticationScheme, configureOptions); + public static AuthenticationBuilder AddGoogle(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) => builder.AddGoogle(authenticationScheme, GoogleDefaults.DisplayName, configureOptions); + public static AuthenticationBuilder AddGoogle(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) where TService : class + => builder.AddGoogle(authenticationScheme, GoogleDefaults.DisplayName, configureOptions); + public static AuthenticationBuilder AddGoogle(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) - => builder.AddOAuth(authenticationScheme, displayName, configureOptions); + { + Action configureOptionsWithServices; + if (configureOptions == null) + { + configureOptionsWithServices = null; + } + else + { + configureOptionsWithServices = (options, _) => configureOptions(options); + } + + return builder.AddGoogle(authenticationScheme, displayName, configureOptionsWithServices); + } + + public static AuthenticationBuilder AddGoogle(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) where TService : class + => builder.AddOAuth(authenticationScheme, displayName, configureOptions); } } diff --git a/src/Security/Authentication/JwtBearer/src/JwtBearerExtensions.cs b/src/Security/Authentication/JwtBearer/src/JwtBearerExtensions.cs index 334407c0da..a5aa46a451 100644 --- a/src/Security/Authentication/JwtBearer/src/JwtBearerExtensions.cs +++ b/src/Security/Authentication/JwtBearer/src/JwtBearerExtensions.cs @@ -17,13 +17,34 @@ namespace Microsoft.Extensions.DependencyInjection public static AuthenticationBuilder AddJwtBearer(this AuthenticationBuilder builder, Action configureOptions) => builder.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, configureOptions); + public static AuthenticationBuilder AddJwtBearer(this AuthenticationBuilder builder, Action configureOptions) where TService : class + => builder.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, configureOptions); + public static AuthenticationBuilder AddJwtBearer(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) => builder.AddJwtBearer(authenticationScheme, displayName: null, configureOptions: configureOptions); + public static AuthenticationBuilder AddJwtBearer(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) where TService : class + => builder.AddJwtBearer(authenticationScheme, displayName: null, configureOptions: configureOptions); + public static AuthenticationBuilder AddJwtBearer(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) + { + Action configureOptionsWithServices; + if (configureOptions == null) + { + configureOptionsWithServices = null; + } + else + { + configureOptionsWithServices = (options, _) => configureOptions(options); + } + + return builder.AddJwtBearer(authenticationScheme, displayName, configureOptionsWithServices); + } + + public static AuthenticationBuilder AddJwtBearer(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) where TService : class { builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton, JwtBearerPostConfigureOptions>()); - return builder.AddScheme(authenticationScheme, displayName, configureOptions); + return builder.AddScheme(authenticationScheme, displayName, configureOptions); } } } diff --git a/src/Security/Authentication/MicrosoftAccount/src/MicrosoftAccountExtensions.cs b/src/Security/Authentication/MicrosoftAccount/src/MicrosoftAccountExtensions.cs index 7f24e5af77..0c59ce3504 100644 --- a/src/Security/Authentication/MicrosoftAccount/src/MicrosoftAccountExtensions.cs +++ b/src/Security/Authentication/MicrosoftAccount/src/MicrosoftAccountExtensions.cs @@ -15,10 +15,31 @@ namespace Microsoft.Extensions.DependencyInjection public static AuthenticationBuilder AddMicrosoftAccount(this AuthenticationBuilder builder, Action configureOptions) => builder.AddMicrosoftAccount(MicrosoftAccountDefaults.AuthenticationScheme, configureOptions); + public static AuthenticationBuilder AddMicrosoftAccount(this AuthenticationBuilder builder, Action configureOptions) where TService : class + => builder.AddMicrosoftAccount(MicrosoftAccountDefaults.AuthenticationScheme, configureOptions); + public static AuthenticationBuilder AddMicrosoftAccount(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) => builder.AddMicrosoftAccount(authenticationScheme, MicrosoftAccountDefaults.DisplayName, configureOptions); + public static AuthenticationBuilder AddMicrosoftAccount(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) where TService : class + => builder.AddMicrosoftAccount(authenticationScheme, MicrosoftAccountDefaults.DisplayName, configureOptions); + public static AuthenticationBuilder AddMicrosoftAccount(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) - => builder.AddOAuth(authenticationScheme, displayName, configureOptions); + { + Action configureOptionsWithServices; + if (configureOptions == null) + { + configureOptionsWithServices = null; + } + else + { + configureOptionsWithServices = (options, _) => configureOptions(options); + } + + return builder.AddMicrosoftAccount(authenticationScheme, displayName, configureOptionsWithServices); + } + + public static AuthenticationBuilder AddMicrosoftAccount(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) where TService : class + => builder.AddOAuth(authenticationScheme, displayName, configureOptions); } -} \ No newline at end of file +} diff --git a/src/Security/Authentication/Negotiate/src/NegotiateExtensions.cs b/src/Security/Authentication/Negotiate/src/NegotiateExtensions.cs index f5bbf8cbc8..401c3dc839 100644 --- a/src/Security/Authentication/Negotiate/src/NegotiateExtensions.cs +++ b/src/Security/Authentication/Negotiate/src/NegotiateExtensions.cs @@ -31,6 +31,16 @@ namespace Microsoft.Extensions.DependencyInjection public static AuthenticationBuilder AddNegotiate(this AuthenticationBuilder builder, Action configureOptions) => builder.AddNegotiate(NegotiateDefaults.AuthenticationScheme, configureOptions); + /// + /// Adds and configures Negotiate authentication. + /// + /// 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. + /// The . + /// Allows for configuring the authentication handler. + /// The original builder. + public static AuthenticationBuilder AddNegotiate(this AuthenticationBuilder builder, Action configureOptions) where TService : class + => builder.AddNegotiate(NegotiateDefaults.AuthenticationScheme, configureOptions); + /// /// Adds and configures Negotiate authentication. /// @@ -41,6 +51,17 @@ namespace Microsoft.Extensions.DependencyInjection public static AuthenticationBuilder AddNegotiate(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) => builder.AddNegotiate(authenticationScheme, displayName: null, configureOptions: configureOptions); + /// + /// Adds and configures Negotiate authentication. + /// + /// 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. + /// The . + /// The scheme name used to identify the authentication handler internally. + /// Allows for configuring the authentication handler. + /// The original builder. + public static AuthenticationBuilder AddNegotiate(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) where TService : class + => builder.AddNegotiate(authenticationScheme, displayName: null, configureOptions: configureOptions); + /// /// Adds and configures Negotiate authentication. /// @@ -50,9 +71,33 @@ namespace Microsoft.Extensions.DependencyInjection /// Allows for configuring the authentication handler. /// The original builder. public static AuthenticationBuilder AddNegotiate(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) + { + Action configureOptionsWithServices; + if (configureOptions == null) + { + configureOptionsWithServices = null; + } + else + { + configureOptionsWithServices = (options, _) => configureOptions(options); + } + + return builder.AddNegotiate(authenticationScheme, displayName, configureOptionsWithServices); + } + + /// + /// Adds and configures Negotiate authentication. + /// + /// 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. + /// The . + /// The scheme name used to identify the authentication handler internally. + /// The name displayed to users when selecting an authentication handler. The default is null to prevent this from displaying. + /// Allows for configuring the authentication handler. + /// The original builder. + public static AuthenticationBuilder AddNegotiate(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) where TService : class { builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton, PostConfigureNegotiateOptions>()); - return builder.AddScheme(authenticationScheme, displayName, configureOptions); + return builder.AddScheme(authenticationScheme, displayName, configureOptions); } } } diff --git a/src/Security/Authentication/OAuth/ref/Microsoft.AspNetCore.Authentication.OAuth.netcoreapp.cs b/src/Security/Authentication/OAuth/ref/Microsoft.AspNetCore.Authentication.OAuth.netcoreapp.cs index 2301daf1ad..94d2fc4d37 100644 --- a/src/Security/Authentication/OAuth/ref/Microsoft.AspNetCore.Authentication.OAuth.netcoreapp.cs +++ b/src/Security/Authentication/OAuth/ref/Microsoft.AspNetCore.Authentication.OAuth.netcoreapp.cs @@ -168,8 +168,12 @@ namespace Microsoft.Extensions.DependencyInjection { public static Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddOAuth(this Microsoft.AspNetCore.Authentication.AuthenticationBuilder builder, string authenticationScheme, System.Action configureOptions) { throw null; } public static Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddOAuth(this Microsoft.AspNetCore.Authentication.AuthenticationBuilder builder, string authenticationScheme, string displayName, System.Action configureOptions) { throw null; } + public static Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddOAuth(this Microsoft.AspNetCore.Authentication.AuthenticationBuilder builder, string authenticationScheme, System.Action configureOptions) where TService : class { throw null; } + public static Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddOAuth(this Microsoft.AspNetCore.Authentication.AuthenticationBuilder builder, string authenticationScheme, string displayName, System.Action configureOptions) where TService : class { throw null; } public static Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddOAuth(this Microsoft.AspNetCore.Authentication.AuthenticationBuilder builder, string authenticationScheme, System.Action configureOptions) where TOptions : Microsoft.AspNetCore.Authentication.OAuth.OAuthOptions, new() where THandler : Microsoft.AspNetCore.Authentication.OAuth.OAuthHandler { throw null; } public static Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddOAuth(this Microsoft.AspNetCore.Authentication.AuthenticationBuilder builder, string authenticationScheme, string displayName, System.Action configureOptions) where TOptions : Microsoft.AspNetCore.Authentication.OAuth.OAuthOptions, new() where THandler : Microsoft.AspNetCore.Authentication.OAuth.OAuthHandler { throw null; } + public static Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddOAuth(this Microsoft.AspNetCore.Authentication.AuthenticationBuilder builder, string authenticationScheme, System.Action configureOptions) where TOptions : Microsoft.AspNetCore.Authentication.OAuth.OAuthOptions, new() where THandler : Microsoft.AspNetCore.Authentication.OAuth.OAuthHandler where TService : class { throw null; } + public static Microsoft.AspNetCore.Authentication.AuthenticationBuilder AddOAuth(this Microsoft.AspNetCore.Authentication.AuthenticationBuilder builder, string authenticationScheme, string displayName, System.Action configureOptions) where TOptions : Microsoft.AspNetCore.Authentication.OAuth.OAuthOptions, new() where THandler : Microsoft.AspNetCore.Authentication.OAuth.OAuthHandler where TService : class { throw null; } } public partial class OAuthPostConfigureOptions : Microsoft.Extensions.Options.IPostConfigureOptions where TOptions : Microsoft.AspNetCore.Authentication.OAuth.OAuthOptions, new() where THandler : Microsoft.AspNetCore.Authentication.OAuth.OAuthHandler { diff --git a/src/Security/Authentication/OAuth/src/OAuthExtensions.cs b/src/Security/Authentication/OAuth/src/OAuthExtensions.cs index 22c541a0ac..69bb2d73d3 100644 --- a/src/Security/Authentication/OAuth/src/OAuthExtensions.cs +++ b/src/Security/Authentication/OAuth/src/OAuthExtensions.cs @@ -14,20 +14,50 @@ namespace Microsoft.Extensions.DependencyInjection public static AuthenticationBuilder AddOAuth(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) => builder.AddOAuth>(authenticationScheme, configureOptions); + public static AuthenticationBuilder AddOAuth(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) where TService : class + => builder.AddOAuth, TService>(authenticationScheme, configureOptions); + 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, string displayName, Action configureOptions) where TService : class + => builder.AddOAuth, TService>(authenticationScheme, displayName, 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, Action configureOptions) + where TOptions : OAuthOptions, new() + where THandler : OAuthHandler + where TService : class + => builder.AddOAuth(authenticationScheme, OAuthDefaults.DisplayName, configureOptions); + public static AuthenticationBuilder AddOAuth(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) where TOptions : OAuthOptions, new() where THandler : OAuthHandler + { + Action configureOptionsWithServices; + if (configureOptions == null) + { + configureOptionsWithServices = null; + } + else + { + configureOptionsWithServices = (options, _) => configureOptions(options); + } + + return builder.AddOAuth(authenticationScheme, displayName, configureOptionsWithServices); + } + + public static AuthenticationBuilder AddOAuth(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) + where TOptions : OAuthOptions, new() + where THandler : OAuthHandler + where TService : class { builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton, OAuthPostConfigureOptions>()); - return builder.AddRemoteScheme(authenticationScheme, displayName, configureOptions); + return builder.AddRemoteScheme(authenticationScheme, displayName, configureOptions); } } } diff --git a/src/Security/Authentication/OpenIdConnect/src/OpenIdConnectExtensions.cs b/src/Security/Authentication/OpenIdConnect/src/OpenIdConnectExtensions.cs index f427bebaff..482452bca2 100644 --- a/src/Security/Authentication/OpenIdConnect/src/OpenIdConnectExtensions.cs +++ b/src/Security/Authentication/OpenIdConnect/src/OpenIdConnectExtensions.cs @@ -17,13 +17,34 @@ namespace Microsoft.Extensions.DependencyInjection public static AuthenticationBuilder AddOpenIdConnect(this AuthenticationBuilder builder, Action configureOptions) => builder.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, configureOptions); + public static AuthenticationBuilder AddOpenIdConnect(this AuthenticationBuilder builder, Action configureOptions) where TService : class + => builder.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, configureOptions); + public static AuthenticationBuilder AddOpenIdConnect(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) => builder.AddOpenIdConnect(authenticationScheme, OpenIdConnectDefaults.DisplayName, configureOptions); + public static AuthenticationBuilder AddOpenIdConnect(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) where TService : class + => builder.AddOpenIdConnect(authenticationScheme, OpenIdConnectDefaults.DisplayName, configureOptions); + public static AuthenticationBuilder AddOpenIdConnect(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) + { + Action configureOptionsWithServices; + if (configureOptions == null) + { + configureOptionsWithServices = null; + } + else + { + configureOptionsWithServices = (options, _) => configureOptions(options); + } + + return builder.AddOpenIdConnect(authenticationScheme, displayName, configureOptionsWithServices); + } + + public static AuthenticationBuilder AddOpenIdConnect(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) where TService : class { builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton, OpenIdConnectPostConfigureOptions>()); - return builder.AddRemoteScheme(authenticationScheme, displayName, configureOptions); + return builder.AddRemoteScheme(authenticationScheme, displayName, configureOptions); } } } diff --git a/src/Security/Authentication/Twitter/src/TwitterExtensions.cs b/src/Security/Authentication/Twitter/src/TwitterExtensions.cs index 7243805692..f6f6b93a9e 100644 --- a/src/Security/Authentication/Twitter/src/TwitterExtensions.cs +++ b/src/Security/Authentication/Twitter/src/TwitterExtensions.cs @@ -17,13 +17,34 @@ namespace Microsoft.Extensions.DependencyInjection public static AuthenticationBuilder AddTwitter(this AuthenticationBuilder builder, Action configureOptions) => builder.AddTwitter(TwitterDefaults.AuthenticationScheme, configureOptions); + public static AuthenticationBuilder AddTwitter(this AuthenticationBuilder builder, Action configureOptions) where TService : class + => builder.AddTwitter(TwitterDefaults.AuthenticationScheme, configureOptions); + public static AuthenticationBuilder AddTwitter(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) => builder.AddTwitter(authenticationScheme, TwitterDefaults.DisplayName, configureOptions); + public static AuthenticationBuilder AddTwitter(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) where TService : class + => builder.AddTwitter(authenticationScheme, TwitterDefaults.DisplayName, configureOptions); + public static AuthenticationBuilder AddTwitter(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) + { + Action configureOptionsWithServices; + if (configureOptions == null) + { + configureOptionsWithServices = null; + } + else + { + configureOptionsWithServices = (options, _) => configureOptions(options); + } + + return builder.AddTwitter(authenticationScheme, displayName, configureOptionsWithServices); + } + + public static AuthenticationBuilder AddTwitter(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) where TService : class { builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton, TwitterPostConfigureOptions>()); - return builder.AddRemoteScheme(authenticationScheme, displayName, configureOptions); + return builder.AddRemoteScheme(authenticationScheme, displayName, configureOptions); } } } diff --git a/src/Security/Authentication/WsFederation/src/WsFederationExtensions.cs b/src/Security/Authentication/WsFederation/src/WsFederationExtensions.cs index 47091d58d5..6a9ffad239 100644 --- a/src/Security/Authentication/WsFederation/src/WsFederationExtensions.cs +++ b/src/Security/Authentication/WsFederation/src/WsFederationExtensions.cs @@ -31,6 +31,16 @@ namespace Microsoft.Extensions.DependencyInjection public static AuthenticationBuilder AddWsFederation(this AuthenticationBuilder builder, Action configureOptions) => builder.AddWsFederation(WsFederationDefaults.AuthenticationScheme, configureOptions); + /// + /// Registers the using the default authentication scheme, display name, and the given options configuration. + /// + /// 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. + /// + /// A delegate that configures the . + /// + public static AuthenticationBuilder AddWsFederation(this AuthenticationBuilder builder, Action configureOptions) where TService : class + => builder.AddWsFederation(WsFederationDefaults.AuthenticationScheme, configureOptions); + /// /// Registers the using the given authentication scheme, default display name, and the given options configuration. /// @@ -41,6 +51,17 @@ namespace Microsoft.Extensions.DependencyInjection public static AuthenticationBuilder AddWsFederation(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) => builder.AddWsFederation(authenticationScheme, WsFederationDefaults.DisplayName, configureOptions); + /// + /// Registers the using the given authentication scheme, default display name, and the given options configuration. + /// + /// 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. + /// + /// + /// A delegate that configures the . + /// + public static AuthenticationBuilder AddWsFederation(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) where TService : class + => builder.AddWsFederation(authenticationScheme, WsFederationDefaults.DisplayName, configureOptions); + /// /// Registers the using the given authentication scheme, display name, and options configuration. /// @@ -50,9 +71,33 @@ namespace Microsoft.Extensions.DependencyInjection /// A delegate that configures the . /// public static AuthenticationBuilder AddWsFederation(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) + { + Action configureOptionsWithServices; + if (configureOptions == null) + { + configureOptionsWithServices = null; + } + else + { + configureOptionsWithServices = (options, _) => configureOptions(options); + } + + return builder.AddWsFederation(authenticationScheme, displayName, configureOptionsWithServices); + } + + /// + /// Registers the using the given authentication scheme, display name, and options configuration. + /// + /// 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. + /// + /// + /// + /// A delegate that configures the . + /// + public static AuthenticationBuilder AddWsFederation(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) where TService : class { builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton, WsFederationPostConfigureOptions>()); - return builder.AddRemoteScheme(authenticationScheme, displayName, configureOptions); + return builder.AddRemoteScheme(authenticationScheme, displayName, configureOptions); } } }