Skip to content

Commit 6ee2ba3

Browse files
Set options.SecurityTokenValidatorsFactory if not set
1 parent c5be417 commit 6ee2ba3

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,7 @@ public class Startup
8989
{
9090
services.AddTransient<ISecurityTokenValidator, MyCustomTokenValidator>();
9191
services.AddAuthentication()
92-
.AddSimpleTokenAuthentication(options =>
93-
{
94-
options.SecurityTokenValidatorsFactory = () =>
95-
{
96-
return services.BuildServiceProvider().GetServices<ISecurityTokenValidator>();
97-
};
98-
});
92+
.AddSimpleTokenAuthentication();
9993
}
10094
10195
public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory) {

src/MakingSense.AspNetCore.Authentication.SimpleToken/MakingSense.AspNetCore.Authentication.SimpleToken.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<PackageReference Include="MakingSense.AspNetCore.Abstractions" Version="2.0.0-alpha-173" />
1919
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.0.0" />
2020
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
21+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
2122
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="5.4.0" />
2223
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
2324
<PackageReference Include="System.Linq" Version="4.3.0" />

src/MakingSense.AspNetCore.Authentication.SimpleToken/README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,7 @@ public class Startup
8989
{
9090
services.AddTransient<ISecurityTokenValidator, MyCustomTokenValidator>();
9191
services.AddAuthentication()
92-
.AddSimpleTokenAuthentication(options =>
93-
{
94-
options.SecurityTokenValidatorsFactory = () =>
95-
{
96-
return services.BuildServiceProvider().GetServices<ISecurityTokenValidator>();
97-
};
98-
});
92+
.AddSimpleTokenAuthentication();
9993
}
10094
10195
public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory) {

src/MakingSense.AspNetCore.Authentication.SimpleToken/SimpleTokenAuthenticationExtensions.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using Microsoft.AspNetCore.Authentication;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.IdentityModel.Tokens;
24
using System;
35

46
namespace MakingSense.AspNetCore.Authentication.SimpleToken
57
{
68
public static class SimpleTokenAuthenticationExtensions
79
{
810
public static AuthenticationBuilder AddSimpleTokenAuthentication(this AuthenticationBuilder builder)
9-
=> builder.AddSimpleTokenAuthentication(SimpleTokenDefaults.AuthenticationScheme, _ => { });
11+
=> builder.AddSimpleTokenAuthentication(SimpleTokenDefaults.AuthenticationScheme, null);
1012

1113
public static AuthenticationBuilder AddSimpleTokenAuthentication(this AuthenticationBuilder builder,
1214
Action<SimpleTokenAuthenticationOptions> configureOptions)
@@ -22,7 +24,28 @@ public static AuthenticationBuilder AddSimpleTokenAuthentication(this Authentica
2224
string displayName,
2325
Action<SimpleTokenAuthenticationOptions> configureOptions)
2426
{
25-
return builder.AddScheme<SimpleTokenAuthenticationOptions, SimpleTokenAuthenticationHandler>(authenticationScheme, displayName, configureOptions);
27+
return builder.AddScheme<SimpleTokenAuthenticationOptions, SimpleTokenAuthenticationHandler>(authenticationScheme, displayName,
28+
(SimpleTokenAuthenticationOptions options) => {
29+
configureOptions?.Invoke(options);
30+
31+
if (options.SecurityTokenValidatorsFactory == null)
32+
{
33+
options.SecurityTokenValidatorsFactory = () =>
34+
{
35+
// TODO: fix it because it is using app services, and it should use scope services,
36+
// a work around could be:
37+
// ```
38+
// SecurityTokenValidatorsFactory = () =>
39+
// {
40+
// var context = builder.Services.BuildServiceProvider().GetService<IHttpContextAccessor>().HttpContext;
41+
// return context.RequestServices.GetServices<ISecurityTokenValidator>();
42+
// }
43+
// ```
44+
var serviceProvider = builder.Services.BuildServiceProvider();
45+
return serviceProvider.GetServices<ISecurityTokenValidator>();
46+
};
47+
}
48+
});
2649
}
2750
}
2851
}

0 commit comments

Comments
 (0)