Skip to content
This repository was archived by the owner on Dec 24, 2020. It is now read-only.

Commit f28adaa

Browse files
committed
Use DataProtectionProvider.Create instead of manually creating a new data protection provider
1 parent cb31e87 commit f28adaa

File tree

2 files changed

+18
-42
lines changed

2 files changed

+18
-42
lines changed

src/Owin.Security.OAuth.Introspection/OAuthIntrospectionMiddleware.cs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Microsoft.AspNetCore.DataProtection;
1111
using Microsoft.Extensions.Caching.Distributed;
1212
using Microsoft.Extensions.Caching.Memory;
13-
using Microsoft.Extensions.DependencyInjection;
1413
using Microsoft.Extensions.Logging;
1514
using Microsoft.Owin;
1615
using Microsoft.Owin.BuilderProperties;
@@ -39,28 +38,17 @@ public OAuthIntrospectionMiddleware(
3938
}
4039

4140
if (options.DataProtectionProvider == null) {
42-
// Create a new DI container and register
43-
// the data protection services.
44-
var services = new ServiceCollection();
41+
// Try to use the application name provided by
42+
// the OWIN host as the application discriminator.
43+
var discriminator = new AppProperties(app.Properties).AppName;
4544

46-
services.AddDataProtection(configuration => {
47-
// Try to use the application name provided by
48-
// the OWIN host as the application discriminator.
49-
var discriminator = new AppProperties(app.Properties).AppName;
45+
// When an application discriminator cannot be resolved from
46+
// the OWIN host properties, generate a temporary identifier.
47+
if (string.IsNullOrEmpty(discriminator)) {
48+
discriminator = Guid.NewGuid().ToString();
49+
}
5050

51-
// When an application discriminator cannot be resolved from
52-
// the OWIN host properties, generate a temporary identifier.
53-
if (string.IsNullOrEmpty(discriminator)) {
54-
discriminator = Guid.NewGuid().ToString();
55-
}
56-
57-
configuration.ApplicationDiscriminator = discriminator;
58-
});
59-
60-
var container = services.BuildServiceProvider();
61-
62-
// Resolve a data protection provider from the services container.
63-
options.DataProtectionProvider = container.GetRequiredService<IDataProtectionProvider>();
51+
options.DataProtectionProvider = DataProtectionProvider.Create(discriminator);
6452
}
6553

6654
if (options.AccessTokenFormat == null) {

src/Owin.Security.OAuth.Validation/OAuthValidationMiddleware.cs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System;
88
using JetBrains.Annotations;
99
using Microsoft.AspNetCore.DataProtection;
10-
using Microsoft.Extensions.DependencyInjection;
1110
using Microsoft.Extensions.Logging;
1211
using Microsoft.Owin;
1312
using Microsoft.Owin.BuilderProperties;
@@ -26,28 +25,17 @@ public OAuthValidationMiddleware(
2625
}
2726

2827
if (options.DataProtectionProvider == null) {
29-
// Create a new DI container and register
30-
// the data protection services.
31-
var services = new ServiceCollection();
28+
// Try to use the application name provided by
29+
// the OWIN host as the application discriminator.
30+
var discriminator = new AppProperties(app.Properties).AppName;
3231

33-
services.AddDataProtection(configuration => {
34-
// Try to use the application name provided by
35-
// the OWIN host as the application discriminator.
36-
var discriminator = new AppProperties(app.Properties).AppName;
32+
// When an application discriminator cannot be resolved from
33+
// the OWIN host properties, generate a temporary identifier.
34+
if (string.IsNullOrEmpty(discriminator)) {
35+
discriminator = Guid.NewGuid().ToString();
36+
}
3737

38-
// When an application discriminator cannot be resolved from
39-
// the OWIN host properties, generate a temporary identifier.
40-
if (string.IsNullOrEmpty(discriminator)) {
41-
discriminator = Guid.NewGuid().ToString();
42-
}
43-
44-
configuration.ApplicationDiscriminator = discriminator;
45-
});
46-
47-
var container = services.BuildServiceProvider();
48-
49-
// Resolve a data protection provider from the services container.
50-
options.DataProtectionProvider = container.GetRequiredService<IDataProtectionProvider>();
38+
options.DataProtectionProvider = DataProtectionProvider.Create(discriminator);
5139
}
5240

5341
if (options.AccessTokenFormat == null) {

0 commit comments

Comments
 (0)