Skip to content

Commit

Permalink
Expanded Async support + HttpContext fixes (#728)
Browse files Browse the repository at this point in the history
* ### Major Fixes
- Added support for proper Async naming convention on method names
- Replaced UseClaimsPrincipal with industrial-strength middleware

### Minor fixes
- Fixed a bug in ConventionBasedOperationFilter & ConventionBasedChangeSetItemFilterthat incorrectly dropped into an error message state.
- Update .NET SDK
- Fixed error message text
  • Loading branch information
robertmclaws authored Jun 5, 2023
1 parent 8b29d6b commit eff96a3
Show file tree
Hide file tree
Showing 18 changed files with 318 additions and 156 deletions.
4 changes: 1 addition & 3 deletions NuGet.Config
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
<add key="disableSourceControlIntegration" value="true" />
</solution>
<packageSources>
<add key="nuget.org" value="https://www.nuget.org/api/v2/" />
<add key="OData Feed" value="https://www.myget.org/F/odata/" />
<add key="WebApi Nightly" value="https://www.myget.org/F/webapinetcore" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>
222 changes: 110 additions & 112 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,118 +1,116 @@
<Project>
<!-- Folder layout -->
<PropertyGroup>
<IsBenchmarkProject Condition="$(MSBuildProjectName.EndsWith('.Performance'))">true</IsBenchmarkProject>
<IsTestProject Condition="$(MSBuildProjectName.ToLower().Contains('.tests.'))">true</IsTestProject>
<IsTestAssetProject Condition="$(RepoRelativeProjectDir.ToLower().Contains('testassets'))">true</IsTestAssetProject>
<IsSampleProject Condition="$(MSBuildProjectName.ToLower().Contains('.samples.'))">true</IsSampleProject>
<IsTemplateProject Condition="$(MSBuildProjectName.ToLower().Contains('.templates.'))">true</IsTemplateProject>
<IsTemplatePackageProject Condition="$(MSBuildProjectName.ToLower().Contains('.templates.package'))">true</IsTemplatePackageProject>
<IsNetCore Condition=" '$(TargetFramework)' == 'netcoreapp3.1' OR '$(TargetFramework)' == 'net6.0' OR '$(TargetFramework)' == 'net7.0' ">true</IsNetCore>
<IsPrimaryProject Condition=" '$(IsBenchmarkProject)' != 'true' And '$(IsTestProject)' != 'true' And '$(IsTestAssetProject)' != 'true' And '$(IsSampleProject)' != 'true' ">true</IsPrimaryProject>

<IncludeSource>false</IncludeSource>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>

<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)restier.snk</AssemblyOriginatorKeyFile>
<StrongNamePublicKey>PublicKey=0024000004800000940000000602000000240000525341310004000001000100f1fcc0061e7ed7476808f86fdecd6a9585915f329fc6312d08331c971da86230330e42ed8ffbd528c523fa023d92c3db1123b5d044b3aad9e04fd3877ffca8e4ba836a740ec3074f0fcbef770ff14d2fa1b5a8403ab16f398ac14998bd011c35505b6bc555326037a7ab7dcca118000a19a5475377358e9dea38df76452b01c9</StrongNamePublicKey>

<LangVersion>preview</LangVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
<DefineConstants>TRACE;RELEASE</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>

<PropertyGroup>
<Product>Microsoft Restier</Product>
<Authors>Microsoft</Authors>
<Company>Microsoft Corporation</Company>
<RpmPackageVendor>.NET Foundation</RpmPackageVendor>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<NeutralLanguage>en-US</NeutralLanguage>
<PackageLicenseUrl>https://raw.githubusercontent.com/OData/RESTier/master/License.txt</PackageLicenseUrl>
<!-- The SPDX name for the source license. See https://spdx.org/licenses/. -->
<PackageLicenseType>MIT</PackageLicenseType>
<!--
<!-- Folder layout -->
<PropertyGroup>
<IsBenchmarkProject Condition="$(MSBuildProjectName.EndsWith('.Performance'))">true</IsBenchmarkProject>
<IsTestProject Condition="$(MSBuildProjectName.ToLower().Contains('.tests.'))">true</IsTestProject>
<IsTestAssetProject Condition="$(RepoRelativeProjectDir.ToLower().Contains('testassets'))">true</IsTestAssetProject>
<IsSampleProject Condition="$(MSBuildProjectName.ToLower().Contains('.samples.'))">true</IsSampleProject>
<IsTemplateProject Condition="$(MSBuildProjectName.ToLower().Contains('.templates.'))">true</IsTemplateProject>
<IsTemplatePackageProject Condition="$(MSBuildProjectName.ToLower().Contains('.templates.package'))">true</IsTemplatePackageProject>
<IsNetCore Condition=" '$(TargetFramework)' == 'netcoreapp3.1' OR '$(TargetFramework)' == 'net6.0' OR '$(TargetFramework)' == 'net7.0' ">true</IsNetCore>
<IsPrimaryProject Condition=" '$(IsBenchmarkProject)' != 'true' And '$(IsTestProject)' != 'true' And '$(IsTestAssetProject)' != 'true' And '$(IsSampleProject)' != 'true' ">true</IsPrimaryProject>

<IncludeSource>false</IncludeSource>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>

<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)restier.snk</AssemblyOriginatorKeyFile>
<StrongNamePublicKey>PublicKey=0024000004800000940000000602000000240000525341310004000001000100f1fcc0061e7ed7476808f86fdecd6a9585915f329fc6312d08331c971da86230330e42ed8ffbd528c523fa023d92c3db1123b5d044b3aad9e04fd3877ffca8e4ba836a740ec3074f0fcbef770ff14d2fa1b5a8403ab16f398ac14998bd011c35505b6bc555326037a7ab7dcca118000a19a5475377358e9dea38df76452b01c9</StrongNamePublicKey>

<LangVersion>preview</LangVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
<DefineConstants>TRACE;RELEASE</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>

<PropertyGroup>
<Product>Microsoft Restier</Product>
<Authors>Microsoft</Authors>
<Company>Microsoft Corporation</Company>
<RpmPackageVendor>.NET Foundation</RpmPackageVendor>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<NeutralLanguage>en-US</NeutralLanguage>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<!--
Suppress a warning about upcoming deprecation of PackageLicenseUrl. When embedding licenses are supported,
replace PackageLicenseUrl with PackageLicenseExpression.
-->
<NoWarn>$(NoWarn);NU5125</NoWarn>
<!-- Suppress warnings about using SemVer 2.0. -->
<NoWarn>$(NoWarn);NU5105</NoWarn>

<!-- Contact email address for NuGet packages and Linux installers. -->
<MaintainerEmail>odata@nimbleapps.cloud</MaintainerEmail>

<PackageIcon>dotnet-logo.png</PackageIcon>
<PackageProjectUrl>https://restier.readthedocs.io/en/latest/</PackageProjectUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageTags>odata;wcf data services;</PackageTags>
<Serviceable>true</Serviceable>

<RepositoryRoot>$(MSBuildThisFileDirectory)</RepositoryRoot>
<RepositoryUrl>https://github.com/OData/RESTier.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup>

<!-- Compilation options -->
<PropertyGroup>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>

<!-- Instructs the compiler to use SHA256 instead of SHA1 when adding file hashes to PDBs. -->
<ChecksumAlgorithm>SHA256</ChecksumAlgorithm>

<!-- Fixes a common error in targets implementing a NoBuild mode. -->
<BuildProjectReferences Condition=" '$(NoBuild)' == 'true' ">false</BuildProjectReferences>

<!-- Suppress warnings about uninstantiated classes. -->
<NoWarn>$(NoWarn);CA1812;CS1570</NoWarn>
</PropertyGroup>

<PropertyGroup Condition=" $(IsTestProject) == 'true' ">
<NoWarn>$(NoWarn);CA1001;CA1031;CA1062;CA1301;CA1303;AC1307;CA1707;CA1716;CA1801;CA1806;CA1819;CA1822;CA1825;CA2000;CA2007;CA2227;CA2234</NoWarn>
</PropertyGroup>

<PropertyGroup Condition=" $(IsSampleProject) == 'true' ">
<NoWarn>$(NoWarn);CA1001;CA1707;CA1716;CA1801;CA1822</NoWarn>
</PropertyGroup>

<PropertyGroup Condition=" '$(IsPrimaryProject)' == 'true' ">
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)</DocumentationFile>
</PropertyGroup>

<PropertyGroup>
<StandardTestTfms>net472</StandardTestTfms>
</PropertyGroup>

<ItemGroup Condition=" $(IsTestProject) != 'true' and $(IsSampleProject) != 'true'">
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.*" PrivateAssets="All" />

<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>$(AssemblyName.Replace("Microsoft.Restier.", "Microsoft.Restier.Tests.")), $(StrongNamePublicKey)</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

<ItemGroup Condition=" $(IsTestProject) == 'true' and $(IsSampleProject) != 'true'">
<PackageReference Include="FluentAssertions" Version="6.*" PrivateAssets="All" />
<PackageReference Include="FluentAssertions.Analyzers" Version="0.17.*" PrivateAssets="All" />
<PackageReference Include="MSTest" Version="3.*" />
</ItemGroup>

<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)dotnet-logo.png" Pack="true" PackagePath="\" Condition="'$(IsTestProject)' != 'true'" />
</ItemGroup>
<NoWarn>$(NoWarn);NU5125</NoWarn>
<!-- Suppress warnings about using SemVer 2.0. -->
<NoWarn>$(NoWarn);NU5105</NoWarn>

<!-- Contact email address for NuGet packages and Linux installers. -->
<MaintainerEmail>odata@nimbleapps.cloud</MaintainerEmail>

<PackageIcon>dotnet-logo.png</PackageIcon>
<PackageProjectUrl>https://restier.readthedocs.io/en/latest/</PackageProjectUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageTags>odata;wcf data services;</PackageTags>
<Serviceable>true</Serviceable>

<RepositoryRoot>$(MSBuildThisFileDirectory)</RepositoryRoot>
<RepositoryUrl>https://github.com/OData/RESTier.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup>

<!-- Compilation options -->
<PropertyGroup>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>

<!-- Instructs the compiler to use SHA256 instead of SHA1 when adding file hashes to PDBs. -->
<ChecksumAlgorithm>SHA256</ChecksumAlgorithm>

<!-- Fixes a common error in targets implementing a NoBuild mode. -->
<BuildProjectReferences Condition=" '$(NoBuild)' == 'true' ">false</BuildProjectReferences>

<!-- Suppress warnings about uninstantiated classes. -->
<NoWarn>$(NoWarn);CA1812;CS1570</NoWarn>
</PropertyGroup>

<PropertyGroup Condition=" $(IsTestProject) == 'true' ">
<NoWarn>$(NoWarn);CA1001;CA1031;CA1062;CA1301;CA1303;AC1307;CA1707;CA1716;CA1801;CA1806;CA1819;CA1822;CA1825;CA2000;CA2007;CA2227;CA2234</NoWarn>
</PropertyGroup>

<PropertyGroup Condition=" $(IsSampleProject) == 'true' ">
<NoWarn>$(NoWarn);CA1001;CA1707;CA1716;CA1801;CA1822</NoWarn>
</PropertyGroup>

<PropertyGroup Condition=" '$(IsPrimaryProject)' == 'true' ">
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)</DocumentationFile>
</PropertyGroup>

<PropertyGroup>
<StandardTestTfms>net472</StandardTestTfms>
</PropertyGroup>

<ItemGroup Condition=" $(IsTestProject) != 'true' and $(IsSampleProject) != 'true'">
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.*" PrivateAssets="All" />

<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>$(AssemblyName.Replace("Microsoft.Restier.", "Microsoft.Restier.Tests.")), $(StrongNamePublicKey)</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

<ItemGroup Condition=" $(IsTestProject) == 'true' and $(IsSampleProject) != 'true'">
<PackageReference Include="FluentAssertions" Version="6.*" PrivateAssets="All" />
<PackageReference Include="FluentAssertions.Analyzers" Version="0.17.*" PrivateAssets="All" />
<PackageReference Include="MSTest" Version="3.*" />
</ItemGroup>

<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)dotnet-logo.png" Pack="true" PackagePath="\" Condition="'$(IsTestProject)' != 'true'" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.AspNet.OData.Extensions;
using Microsoft.Restier.AspNetCore.Batch;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Restier.AspNetCore.Middleware;
using System.Security.Claims;

namespace Microsoft.AspNetCore.Builder
Expand All @@ -19,11 +18,12 @@ public static class IApplicationBuilderExtensions
/// <returns></returns>
public static IApplicationBuilder UseClaimsPrincipals(this IApplicationBuilder app)
{
app.Use(async (context, next) =>
{
ClaimsPrincipal.ClaimsPrincipalSelector = () => context.User;
await next();
});
//app.Use((context, next) =>
//{
// ClaimsPrincipal.ClaimsPrincipalSelector = () => context.User;
// return next(context);
//});
app.UseMiddleware<RestierClaimsPrincipalMiddleware>();
return app;
}

Expand Down Expand Up @@ -52,6 +52,7 @@ public static IApplicationBuilder UseRestierBatching(this IApplicationBuilder ap
// await next();
// });
//#endif

app.UseODataBatching();
// RWM: This call fixes issues where the batch processor irresponsibly disposes of the HttpContext before it should.
app.UseMiddleware<ODataBatchHttpContextFixerMiddleware>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;

namespace Microsoft.Restier.AspNetCore.Batch
namespace Microsoft.Restier.AspNetCore.Middleware
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Microsoft.AspNetCore.Http;
using System.Security.Claims;
using System.Threading.Tasks;

namespace Microsoft.Restier.AspNetCore.Middleware
{

/// <summary>
/// Fixes the issue outlined in https://github.com/OData/WebApi/issues/2294
/// </summary>
/// <remarks>
/// Solution adapted from https://stackoverflow.com/questions/71338662/ihttpcontextaccessor-httpcontext-is-null-after-execution-falls-out-of-the-useoda
/// </remarks>
public class RestierClaimsPrincipalMiddleware
{

#region Private Members

private readonly RequestDelegate requestDelegate;

#endregion

#region Constructor

/// <summary>
/// The default constructor for the middleware.
/// </summary>
/// <param name="requestDelegate"></param>
public RestierClaimsPrincipalMiddleware(RequestDelegate requestDelegate)
{
this.requestDelegate = requestDelegate;
}

#endregion

#region Middleware

/// <summary>
///
/// </summary>
/// <param name="httpContext"></param>
/// <param name="contextAccessor">The <see cref="IHttpContextAccessor"/> injected from DI for the current request,</param>
/// <returns></returns>
public async Task InvokeAsync(HttpContext httpContext, IHttpContextAccessor contextAccessor)
{
contextAccessor.HttpContext ??= httpContext;
ClaimsPrincipal.ClaimsPrincipalSelector = () => contextAccessor.HttpContext.User;
await requestDelegate(httpContext);
}

#endregion

}

}

Loading

0 comments on commit eff96a3

Please sign in to comment.