Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@
<PackageVersion Include="Microsoft.AspNet.Web.Optimization" Version="1.1.3" />
<PackageVersion Include="Microsoft.AspNet.Web.Optimization.WebForms" Version="1.1.3" />
<PackageVersion Include="Microsoft.AspNet.WebApi" Version="5.3.0" />
<PackageVersion Include="Microsoft.AspNetCore.DataProtection.SystemWeb" Version="2.3.11" />
<PackageVersion Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" Version="4.1.0" />
<PackageVersion Include="Microsoft.Extensions.ServiceDiscovery" Version="10.7.0" />
<PackageVersion Include="Microsoft.jQuery.Unobtrusive.Validation" Version="3.3.0" />
Expand Down
3 changes: 1 addition & 2 deletions samples/AppConfig/AppConfigFramework/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
</system.webServer>
<system.web>
<httpRuntime targetFramework="4.8.1" />
<machineKey compatibilityMode="Framework45" dataProtectorType="Microsoft.AspNetCore.DataProtection.SystemWeb.CompatibilityDataProtector, Microsoft.AspNetCore.DataProtection.SystemWeb" />
<compilation debug="true" />
</system.web>
<runtime>
Expand Down Expand Up @@ -154,4 +153,4 @@
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008,40000,40008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</compilers>
</system.codedom>
</configuration>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
<add assembly="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
</assemblies>
</compilation>
<machineKey compatibilityMode="Framework45" dataProtectorType="Microsoft.AspNetCore.DataProtection.SystemWeb.CompatibilityDataProtector, Microsoft.AspNetCore.DataProtection.SystemWeb" />
<httpRuntime targetFramework="4.8.1" />
</system.web>
<system.webServer>
Expand Down
1 change: 1 addition & 0 deletions samples/MachineKey/MachineKeyFramework/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ protected void Application_Start()
{
HttpApplicationHost.RegisterHost(builder =>
{
builder.AddSystemWebDependencyInjection();
builder.AddServiceDefaults();
builder.AddDataProtection()
.SetApplicationName(MachineKeyExampleHandler.AppName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
<PropertyGroup>
<TargetFramework>net481</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.DataProtection.SystemWeb" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MachineKeyShared\MachineKeyShared.csproj" />
<ProjectReference Include="..\..\ServiceDefaults\Samples.ServiceDefaults.csproj" />
Expand Down
1 change: 0 additions & 1 deletion samples/MachineKey/MachineKeyFramework/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
</system.webServer>
<system.web>
<httpRuntime targetFramework="4.8.1" />
<machineKey compatibilityMode="Framework45" dataProtectorType="Microsoft.AspNetCore.DataProtection.SystemWeb.CompatibilityDataProtector, Microsoft.AspNetCore.DataProtection.SystemWeb" />
<compilation debug="true" />
</system.web>
<runtime>
Expand Down
3 changes: 1 addition & 2 deletions samples/SessionRemote/SessionRemoteFramework/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
</system.webServer>
<system.web>
<httpRuntime targetFramework="4.8.1" />
<machineKey compatibilityMode="Framework45" dataProtectorType="Microsoft.AspNetCore.DataProtection.SystemWeb.CompatibilityDataProtector, Microsoft.AspNetCore.DataProtection.SystemWeb" />
<compilation debug="true" />
</system.web>
<runtime>
Expand Down Expand Up @@ -146,4 +145,4 @@
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008,40000,40008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</compilers>
</system.codedom>
</configuration>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel;
using System.Configuration;
using System.Security.Cryptography;
using System.Web;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.SystemWebAdapters.Hosting;

namespace Microsoft.AspNetCore.DataProtection.SystemWeb;

[EditorBrowsable(EditorBrowsableState.Never)]
public class CompatibilityDataProtector : DataProtector
{
[ThreadStatic]
private static bool _suppressPrimaryPurpose;

private readonly Lazy<IDataProtector> _lazyProtector;
private readonly Lazy<IDataProtector> _lazyProtectorSuppressedPrimaryPurpose;

public CompatibilityDataProtector(string applicationName, string primaryPurpose, string[] specificPurposes)
: base("application-name", "primary-purpose", null) // we feed dummy values to the base ctor
{
// We don't want to evaluate the IDataProtectionProvider factory quite yet,
// as we'd rather defer failures to the call to Protect so that we can bubble
// up a good error message to the developer.

_lazyProtector = new Lazy<IDataProtector>(() => GetDataProtectionProvider().CreateProtector(primaryPurpose, specificPurposes));

// System.Web always provides "User.MachineKey.Protect" as the primary purpose for calls
// to MachineKey.Protect. Only in this case should we allow suppressing the primary
// purpose, as then we can easily map calls to MachineKey.Protect(userData, purposes)
// into calls to provider.GetProtector(purposes).Protect(userData).
if (primaryPurpose == "User.MachineKey.Protect")
{
_lazyProtectorSuppressedPrimaryPurpose = new Lazy<IDataProtector>(() => GetDataProtectionProvider().CreateProtector(specificPurposes));
}
else
{
_lazyProtectorSuppressedPrimaryPurpose = _lazyProtector;
}
}

// We take care of flowing purposes ourselves.
protected override bool PrependHashedPurposeToPlaintext => false;

// Retrieves the appropriate protector (potentially with a suppressed primary purpose) for this operation.
private IDataProtector Protector => ((_suppressPrimaryPurpose) ? _lazyProtectorSuppressedPrimaryPurpose : _lazyProtector).Value;

protected virtual IDataProtectionProvider GetDataProtectionProvider()
=> HttpApplicationHost.Current.Services.GetDataProtectionProvider();

public override bool IsReprotectRequired(byte[] encryptedData)
{
// Nobody ever calls this.
return false;
}

protected override byte[] ProviderProtect(byte[] userData)
{
try
{
return Protector.Protect(userData);
}
catch (Exception ex)
{
// System.Web special-cases ConfigurationException errors and allows them to bubble
// up to the developer without being homogenized. Since a call to Protect should
// never fail, any exceptions here really do imply a misconfiguration.

#pragma warning disable CS0618 // Type or member is obsolete
throw new ConfigurationException("DataProtection failed to protect", ex);
#pragma warning restore CS0618 // Type or member is obsolete
}
}

protected override byte[] ProviderUnprotect(byte[] encryptedData)
{
return Protector.Unprotect(encryptedData);
}

/// <summary>
/// Invokes a delegate where calls to <see cref="ProviderProtect(byte[])"/>
/// and <see cref="ProviderUnprotect(byte[])"/> will ignore the primary
/// purpose and instead use only the sub-purposes.
/// </summary>
public static byte[] RunWithSuppressedPrimaryPurpose(Func<object, byte[], byte[]> callback, object state, byte[] input)
{
if (callback is null)
{
throw new ArgumentNullException(nameof(callback));
}

if (_suppressPrimaryPurpose)
{
return callback(state, input); // already suppressed - just forward call
}

try
{
try
{
_suppressPrimaryPurpose = true;
return callback(state, input);
}
finally
{
_suppressPrimaryPurpose = false;
}
}
catch
{
// defeat exception filters
throw;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
<PackageReference Include="Microsoft.Extensions.Options.DataAnnotations" />
<PackageReference Include="System.Text.Json" />
<PackageReference Include="Microsoft.Extensions.Hosting" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.SystemWeb" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection" />
</ItemGroup>
<ItemGroup>
Expand Down
Loading
Loading