Skip to content

Commit 6de2392

Browse files
authored
Add wrapper for SetDataProtectionProvider (#630)
1 parent af62eb5 commit 6de2392

File tree

7 files changed

+48
-1
lines changed

7 files changed

+48
-1
lines changed

samples/AuthRemoteIdentity/AuthRemoteIdentityCore/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Microsoft.Owin;
2121
using Microsoft.Owin.Extensions;
2222
using Microsoft.Owin.Security.Cookies;
23+
using Microsoft.Owin.Security.DataProtection;
2324
using Microsoft.Owin.Security.Interop;
2425
using MvcApp;
2526
using MvcApp.Models;

samples/AuthRemoteIdentity/AuthRemoteIdentityFramework/Web.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@
241241
<bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
242242
</dependentAssembly>
243243
</assemblyBinding>
244+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
245+
<dependentAssembly>
246+
<assemblyIdentity name="System.Security.Cryptography.Xml" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
247+
<bindingRedirect oldVersion="0.0.0.0-8.0.0.2" newVersion="8.0.0.2" />
248+
</dependentAssembly>
249+
</assemblyBinding>
244250
</runtime>
245251
<system.codedom>
246252
<compilers>

samples/Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
<PackageVersion Include="Microsoft.jQuery.Unobtrusive.Validation" Version="3.2.11" />
4646
<PackageVersion Include="Microsoft.Owin.Host.SystemWeb" Version="4.2.2" />
4747
<PackageVersion Include="Microsoft.Owin.Security.Cookies" Version="4.2.2" />
48-
<PackageVersion Include="Microsoft.Owin.Security.Interop" Version="2.1.38" />
48+
<PackageVersion Include="Microsoft.Owin.Security.Interop" Version="2.3.0" />
4949
<PackageVersion Include="Microsoft.Owin.Security.OAuth" Version="4.2.2" />
5050
<PackageVersion Include="Microsoft.Web.Infrastructure" Version="2.0.1" />
5151
<PackageVersion Include="Microsoft.Win32.Registry" Version="5.0.0" />

src/Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<PackageVersion Include="Yarp.ReverseProxy" Version="2.3.0" />
2121
<PackageVersion Include="Microsoft.Owin" Version="4.2.3" />
2222
<PackageVersion Include="Microsoft.Owin.Security" Version="4.2.3" />
23+
<PackageVersion Include="Microsoft.Owin.Security.Interop" Version="2.3.0" />
2324
<PackageVersion Include="Owin" Version="1.0.0" />
2425
</ItemGroup>
2526

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.AspNetCore.DataProtection;
5+
using Microsoft.Owin.Security.Interop;
6+
using Owin;
7+
8+
using AspNetCoreDataProtectionProvider = Microsoft.AspNetCore.DataProtection.IDataProtectionProvider;
9+
10+
namespace Microsoft.Owin.Security.DataProtection;
11+
12+
public static class AppBuilderDataProtectionExtensions
13+
{
14+
public static void SetDataProtectionProvider(this IAppBuilder app, AspNetCoreDataProtectionProvider dataProtectionProvider)
15+
{
16+
ArgumentNullException.ThrowIfNull(app);
17+
ArgumentNullException.ThrowIfNull(dataProtectionProvider);
18+
19+
app.SetDataProtectionProvider(new DataProtectionProviderShim(dataProtectionProvider));
20+
}
21+
22+
private sealed class DataProtectionProviderShim(AspNetCoreDataProtectionProvider other) : IDataProtectionProvider
23+
{
24+
public IDataProtector Create(params string[] purposes) => new DataProtectorShim(other.CreateProtector(purposes));
25+
}
26+
}

src/Microsoft.AspNetCore.SystemWebAdapters.Owin/Microsoft.AspNetCore.SystemWebAdapters.Owin.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.Owin" />
1212
<PackageReference Include="Microsoft.Owin.Security" />
13+
<PackageReference Include="Microsoft.Owin.Security.Interop" />
1314
<PackageReference Include="Microsoft.AspNetCore.Owin" />
1415
</ItemGroup>
1516

src/Microsoft.AspNetCore.SystemWebAdapters.Owin/OwinBuilder.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Microsoft.AspNetCore.DataProtection;
45
using Microsoft.AspNetCore.Hosting;
56
using Microsoft.Extensions.DependencyInjection;
67
using Microsoft.Extensions.Hosting;
78
using Microsoft.Owin.Builder;
89
using Microsoft.Owin.BuilderProperties;
10+
using Microsoft.Owin.Security.DataProtection;
911
using Owin;
1012

1113
namespace Microsoft.AspNetCore.SystemWebAdapters;
@@ -25,8 +27,18 @@ public static AppFunc Build(AppFunc defaultApp, Action<IAppBuilder, IServiceProv
2527

2628
owinAppProperties.AppName = env.ApplicationName;
2729

30+
AddAspNetCoreDefaults(owinAppBuilder, services);
31+
2832
configure(owinAppBuilder, services);
2933

3034
return owinAppBuilder.Build();
3135
}
36+
37+
private static void AddAspNetCoreDefaults(AppBuilder app, IServiceProvider services)
38+
{
39+
if (!app.Properties.ContainsKey("security.DataProtectionProvider") && services.GetService<DataProtection.IDataProtectionProvider>() is { } dataProtectionProvider)
40+
{
41+
app.SetDataProtectionProvider(dataProtectionProvider);
42+
}
43+
}
3244
}

0 commit comments

Comments
 (0)