Inject ICorsPolicyProvider instance through Invoke
This allows for scoped instances of `ICorsPolicyProvider` to be injected in the CORS middleware and prevents turning them into singletons. Resolves #105
This commit is contained in:
parent
808bf234b9
commit
e790a9bb10
|
|
@ -17,7 +17,6 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure
|
||||||
{
|
{
|
||||||
private readonly Func<object, Task> OnResponseStartingDelegate = OnResponseStarting;
|
private readonly Func<object, Task> OnResponseStartingDelegate = OnResponseStarting;
|
||||||
private readonly RequestDelegate _next;
|
private readonly RequestDelegate _next;
|
||||||
private readonly ICorsPolicyProvider _corsPolicyProvider;
|
|
||||||
private readonly CorsPolicy _policy;
|
private readonly CorsPolicy _policy;
|
||||||
private readonly string _corsPolicyName;
|
private readonly string _corsPolicyName;
|
||||||
|
|
||||||
|
|
@ -26,61 +25,12 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="next">The next middleware in the pipeline.</param>
|
/// <param name="next">The next middleware in the pipeline.</param>
|
||||||
/// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
|
/// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
|
||||||
/// <param name="policyProvider">A policy provider which can get an <see cref="CorsPolicy"/>.</param>
|
|
||||||
[Obsolete("This constructor has been replaced with an equivalent constructor which requires an ILoggerFactory")]
|
|
||||||
public CorsMiddleware(
|
|
||||||
RequestDelegate next,
|
|
||||||
ICorsService corsService,
|
|
||||||
ICorsPolicyProvider policyProvider)
|
|
||||||
: this(next, corsService, policyProvider, NullLoggerFactory.Instance, policyName: null)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Instantiates a new <see cref="CorsMiddleware"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="next">The next middleware in the pipeline.</param>
|
|
||||||
/// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
|
|
||||||
/// <param name="policyProvider">A policy provider which can get an <see cref="CorsPolicy"/>.</param>
|
|
||||||
/// <param name="policyName">An optional name of the policy to be fetched.</param>
|
|
||||||
[Obsolete("This constructor has been replaced with an equivalent constructor which requires an ILoggerFactory")]
|
|
||||||
public CorsMiddleware(
|
|
||||||
RequestDelegate next,
|
|
||||||
ICorsService corsService,
|
|
||||||
ICorsPolicyProvider policyProvider,
|
|
||||||
string policyName)
|
|
||||||
: this(next, corsService, policyProvider, NullLoggerFactory.Instance, policyName)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Instantiates a new <see cref="CorsMiddleware"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="next">The next middleware in the pipeline.</param>
|
|
||||||
/// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
|
|
||||||
/// <param name="policy">An instance of the <see cref="CorsPolicy"/> which can be applied.</param>
|
|
||||||
[Obsolete("This constructor has been replaced with an equivalent constructor which requires an ILoggerFactory")]
|
|
||||||
public CorsMiddleware(
|
|
||||||
RequestDelegate next,
|
|
||||||
ICorsService corsService,
|
|
||||||
CorsPolicy policy)
|
|
||||||
: this(next, corsService, policy, NullLoggerFactory.Instance)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Instantiates a new <see cref="CorsMiddleware"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="next">The next middleware in the pipeline.</param>
|
|
||||||
/// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
|
|
||||||
/// <param name="policyProvider">A policy provider which can get an <see cref="CorsPolicy"/>.</param>
|
|
||||||
/// <param name="loggerFactory">An instance of <see cref="ILoggerFactory"/>.</param>
|
/// <param name="loggerFactory">An instance of <see cref="ILoggerFactory"/>.</param>
|
||||||
public CorsMiddleware(
|
public CorsMiddleware(
|
||||||
RequestDelegate next,
|
RequestDelegate next,
|
||||||
ICorsService corsService,
|
ICorsService corsService,
|
||||||
ICorsPolicyProvider policyProvider,
|
|
||||||
ILoggerFactory loggerFactory)
|
ILoggerFactory loggerFactory)
|
||||||
: this(next, corsService, policyProvider, loggerFactory, policyName: null)
|
: this(next, corsService, loggerFactory, policyName: null)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -89,13 +39,11 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="next">The next middleware in the pipeline.</param>
|
/// <param name="next">The next middleware in the pipeline.</param>
|
||||||
/// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
|
/// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
|
||||||
/// <param name="policyProvider">A policy provider which can get an <see cref="CorsPolicy"/>.</param>
|
|
||||||
/// <param name="loggerFactory">An instance of <see cref="ILoggerFactory"/>.</param>
|
/// <param name="loggerFactory">An instance of <see cref="ILoggerFactory"/>.</param>
|
||||||
/// <param name="policyName">An optional name of the policy to be fetched.</param>
|
/// <param name="policyName">An optional name of the policy to be fetched.</param>
|
||||||
public CorsMiddleware(
|
public CorsMiddleware(
|
||||||
RequestDelegate next,
|
RequestDelegate next,
|
||||||
ICorsService corsService,
|
ICorsService corsService,
|
||||||
ICorsPolicyProvider policyProvider,
|
|
||||||
ILoggerFactory loggerFactory,
|
ILoggerFactory loggerFactory,
|
||||||
string policyName)
|
string policyName)
|
||||||
{
|
{
|
||||||
|
|
@ -109,11 +57,6 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure
|
||||||
throw new ArgumentNullException(nameof(corsService));
|
throw new ArgumentNullException(nameof(corsService));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (policyProvider == null)
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(policyProvider));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (loggerFactory == null)
|
if (loggerFactory == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(loggerFactory));
|
throw new ArgumentNullException(nameof(loggerFactory));
|
||||||
|
|
@ -121,7 +64,6 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure
|
||||||
|
|
||||||
_next = next;
|
_next = next;
|
||||||
CorsService = corsService;
|
CorsService = corsService;
|
||||||
_corsPolicyProvider = policyProvider;
|
|
||||||
_corsPolicyName = policyName;
|
_corsPolicyName = policyName;
|
||||||
Logger = loggerFactory.CreateLogger<CorsMiddleware>();
|
Logger = loggerFactory.CreateLogger<CorsMiddleware>();
|
||||||
}
|
}
|
||||||
|
|
@ -170,19 +112,19 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure
|
||||||
private ILogger Logger { get; }
|
private ILogger Logger { get; }
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public Task Invoke(HttpContext context)
|
public Task Invoke(HttpContext context, ICorsPolicyProvider corsPolicyProvider)
|
||||||
{
|
{
|
||||||
if (!context.Request.Headers.ContainsKey(CorsConstants.Origin))
|
if (!context.Request.Headers.ContainsKey(CorsConstants.Origin))
|
||||||
{
|
{
|
||||||
return _next(context);
|
return _next(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
return InvokeCore(context);
|
return InvokeCore(context, corsPolicyProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task InvokeCore(HttpContext context)
|
private async Task InvokeCore(HttpContext context, ICorsPolicyProvider corsPolicyProvider)
|
||||||
{
|
{
|
||||||
var corsPolicy = _policy ?? await _corsPolicyProvider?.GetPolicyAsync(context, _corsPolicyName);
|
var corsPolicy = _policy ?? await corsPolicyProvider.GetPolicyAsync(context, _corsPolicyName);
|
||||||
if (corsPolicy == null)
|
if (corsPolicy == null)
|
||||||
{
|
{
|
||||||
Logger?.NoCorsPolicyFound();
|
Logger?.NoCorsPolicyFound();
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"TypeId": "public class Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware",
|
||||||
|
"MemberId": "public .ctor(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.AspNetCore.Cors.Infrastructure.ICorsService corsService, Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicy policy)",
|
||||||
|
"Kind": "Removal"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"TypeId": "public class Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware",
|
||||||
|
"MemberId": "public .ctor(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.AspNetCore.Cors.Infrastructure.ICorsService corsService, Microsoft.AspNetCore.Cors.Infrastructure.ICorsPolicyProvider policyProvider)",
|
||||||
|
"Kind": "Removal"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"TypeId": "public class Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware",
|
||||||
|
"MemberId": "public .ctor(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.AspNetCore.Cors.Infrastructure.ICorsService corsService, Microsoft.AspNetCore.Cors.Infrastructure.ICorsPolicyProvider policyProvider, System.String policyName)",
|
||||||
|
"Kind": "Removal"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"TypeId": "public class Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware",
|
||||||
|
"MemberId": "public System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext context)",
|
||||||
|
"Kind": "Removal"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
|
@ -336,7 +336,6 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure
|
||||||
var middleware = new CorsMiddleware(
|
var middleware = new CorsMiddleware(
|
||||||
Mock.Of<RequestDelegate>(),
|
Mock.Of<RequestDelegate>(),
|
||||||
corsService,
|
corsService,
|
||||||
mockProvider.Object,
|
|
||||||
loggerFactory,
|
loggerFactory,
|
||||||
policyName: null);
|
policyName: null);
|
||||||
|
|
||||||
|
|
@ -344,7 +343,7 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure
|
||||||
httpContext.Request.Headers.Add(CorsConstants.Origin, new[] { "http://example.com" });
|
httpContext.Request.Headers.Add(CorsConstants.Origin, new[] { "http://example.com" });
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await middleware.Invoke(httpContext);
|
await middleware.Invoke(httpContext, mockProvider.Object);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
mockProvider.Verify(
|
mockProvider.Verify(
|
||||||
|
|
@ -366,7 +365,6 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure
|
||||||
var middleware = new CorsMiddleware(
|
var middleware = new CorsMiddleware(
|
||||||
Mock.Of<RequestDelegate>(),
|
Mock.Of<RequestDelegate>(),
|
||||||
corsService,
|
corsService,
|
||||||
mockProvider.Object,
|
|
||||||
loggerFactory,
|
loggerFactory,
|
||||||
policyName: null);
|
policyName: null);
|
||||||
|
|
||||||
|
|
@ -374,7 +372,7 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure
|
||||||
httpContext.Request.Headers.Add(CorsConstants.Origin, new[] { "http://example.com" });
|
httpContext.Request.Headers.Add(CorsConstants.Origin, new[] { "http://example.com" });
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await middleware.Invoke(httpContext);
|
await middleware.Invoke(httpContext, mockProvider.Object);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(200, httpContext.Response.StatusCode);
|
Assert.Equal(200, httpContext.Response.StatusCode);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue