diff --git a/README.md b/README.md index 296ef3e..c55f6db 100644 --- a/README.md +++ b/README.md @@ -89,13 +89,7 @@ public class Startup { services.AddTransient(); services.AddAuthentication() - .AddSimpleTokenAuthentication(options => - { - options.SecurityTokenValidatorsFactory = () => - { - return services.BuildServiceProvider().GetServices(); - }; - }); + .AddSimpleTokenAuthentication(); } public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory) { diff --git a/src/MakingSense.AspNetCore.Authentication.SimpleToken/MakingSense.AspNetCore.Authentication.SimpleToken.csproj b/src/MakingSense.AspNetCore.Authentication.SimpleToken/MakingSense.AspNetCore.Authentication.SimpleToken.csproj index 6d171d0..9017b90 100644 --- a/src/MakingSense.AspNetCore.Authentication.SimpleToken/MakingSense.AspNetCore.Authentication.SimpleToken.csproj +++ b/src/MakingSense.AspNetCore.Authentication.SimpleToken/MakingSense.AspNetCore.Authentication.SimpleToken.csproj @@ -18,6 +18,7 @@ + diff --git a/src/MakingSense.AspNetCore.Authentication.SimpleToken/README.md b/src/MakingSense.AspNetCore.Authentication.SimpleToken/README.md index 296ef3e..c55f6db 100644 --- a/src/MakingSense.AspNetCore.Authentication.SimpleToken/README.md +++ b/src/MakingSense.AspNetCore.Authentication.SimpleToken/README.md @@ -89,13 +89,7 @@ public class Startup { services.AddTransient(); services.AddAuthentication() - .AddSimpleTokenAuthentication(options => - { - options.SecurityTokenValidatorsFactory = () => - { - return services.BuildServiceProvider().GetServices(); - }; - }); + .AddSimpleTokenAuthentication(); } public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory) { diff --git a/src/MakingSense.AspNetCore.Authentication.SimpleToken/SimpleTokenAuthenticationExtensions.cs b/src/MakingSense.AspNetCore.Authentication.SimpleToken/SimpleTokenAuthenticationExtensions.cs index c19def3..e7aeccd 100644 --- a/src/MakingSense.AspNetCore.Authentication.SimpleToken/SimpleTokenAuthenticationExtensions.cs +++ b/src/MakingSense.AspNetCore.Authentication.SimpleToken/SimpleTokenAuthenticationExtensions.cs @@ -1,4 +1,6 @@ using Microsoft.AspNetCore.Authentication; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.IdentityModel.Tokens; using System; namespace MakingSense.AspNetCore.Authentication.SimpleToken @@ -6,7 +8,7 @@ namespace MakingSense.AspNetCore.Authentication.SimpleToken public static class SimpleTokenAuthenticationExtensions { public static AuthenticationBuilder AddSimpleTokenAuthentication(this AuthenticationBuilder builder) - => builder.AddSimpleTokenAuthentication(SimpleTokenDefaults.AuthenticationScheme, _ => { }); + => builder.AddSimpleTokenAuthentication(SimpleTokenDefaults.AuthenticationScheme, null); public static AuthenticationBuilder AddSimpleTokenAuthentication(this AuthenticationBuilder builder, Action configureOptions) @@ -22,7 +24,28 @@ public static AuthenticationBuilder AddSimpleTokenAuthentication(this Authentica string displayName, Action configureOptions) { - return builder.AddScheme(authenticationScheme, displayName, configureOptions); + return builder.AddScheme(authenticationScheme, displayName, + (SimpleTokenAuthenticationOptions options) => { + configureOptions?.Invoke(options); + + if (options.SecurityTokenValidatorsFactory == null) + { + options.SecurityTokenValidatorsFactory = () => + { + // TODO: fix it because it is using app services, and it should use scope services, + // a work around could be: + // ``` + // SecurityTokenValidatorsFactory = () => + // { + // var context = builder.Services.BuildServiceProvider().GetService().HttpContext; + // return context.RequestServices.GetServices(); + // } + // ``` + var serviceProvider = builder.Services.BuildServiceProvider(); + return serviceProvider.GetServices(); + }; + } + }); } } }