Skip to content
This repository was archived by the owner on Nov 22, 2018. It is now read-only.

Commit e790a9b

Browse files
henkmollemapranavkm
authored andcommitted
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
1 parent 808bf23 commit e790a9b

File tree

3 files changed

+30
-68
lines changed

3 files changed

+30
-68
lines changed

src/Microsoft.AspNetCore.Cors/Infrastructure/CorsMiddleware.cs

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public class CorsMiddleware
1717
{
1818
private readonly Func<object, Task> OnResponseStartingDelegate = OnResponseStarting;
1919
private readonly RequestDelegate _next;
20-
private readonly ICorsPolicyProvider _corsPolicyProvider;
2120
private readonly CorsPolicy _policy;
2221
private readonly string _corsPolicyName;
2322

@@ -26,61 +25,12 @@ public class CorsMiddleware
2625
/// </summary>
2726
/// <param name="next">The next middleware in the pipeline.</param>
2827
/// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
29-
/// <param name="policyProvider">A policy provider which can get an <see cref="CorsPolicy"/>.</param>
30-
[Obsolete("This constructor has been replaced with an equivalent constructor which requires an ILoggerFactory")]
31-
public CorsMiddleware(
32-
RequestDelegate next,
33-
ICorsService corsService,
34-
ICorsPolicyProvider policyProvider)
35-
: this(next, corsService, policyProvider, NullLoggerFactory.Instance, policyName: null)
36-
{
37-
}
38-
39-
/// <summary>
40-
/// Instantiates a new <see cref="CorsMiddleware"/>.
41-
/// </summary>
42-
/// <param name="next">The next middleware in the pipeline.</param>
43-
/// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
44-
/// <param name="policyProvider">A policy provider which can get an <see cref="CorsPolicy"/>.</param>
45-
/// <param name="policyName">An optional name of the policy to be fetched.</param>
46-
[Obsolete("This constructor has been replaced with an equivalent constructor which requires an ILoggerFactory")]
47-
public CorsMiddleware(
48-
RequestDelegate next,
49-
ICorsService corsService,
50-
ICorsPolicyProvider policyProvider,
51-
string policyName)
52-
: this(next, corsService, policyProvider, NullLoggerFactory.Instance, policyName)
53-
{
54-
}
55-
56-
/// <summary>
57-
/// Instantiates a new <see cref="CorsMiddleware"/>.
58-
/// </summary>
59-
/// <param name="next">The next middleware in the pipeline.</param>
60-
/// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
61-
/// <param name="policy">An instance of the <see cref="CorsPolicy"/> which can be applied.</param>
62-
[Obsolete("This constructor has been replaced with an equivalent constructor which requires an ILoggerFactory")]
63-
public CorsMiddleware(
64-
RequestDelegate next,
65-
ICorsService corsService,
66-
CorsPolicy policy)
67-
: this(next, corsService, policy, NullLoggerFactory.Instance)
68-
{
69-
}
70-
71-
/// <summary>
72-
/// Instantiates a new <see cref="CorsMiddleware"/>.
73-
/// </summary>
74-
/// <param name="next">The next middleware in the pipeline.</param>
75-
/// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
76-
/// <param name="policyProvider">A policy provider which can get an <see cref="CorsPolicy"/>.</param>
7728
/// <param name="loggerFactory">An instance of <see cref="ILoggerFactory"/>.</param>
7829
public CorsMiddleware(
7930
RequestDelegate next,
8031
ICorsService corsService,
81-
ICorsPolicyProvider policyProvider,
8232
ILoggerFactory loggerFactory)
83-
: this(next, corsService, policyProvider, loggerFactory, policyName: null)
33+
: this(next, corsService, loggerFactory, policyName: null)
8434
{
8535
}
8636

@@ -89,13 +39,11 @@ public CorsMiddleware(
8939
/// </summary>
9040
/// <param name="next">The next middleware in the pipeline.</param>
9141
/// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
92-
/// <param name="policyProvider">A policy provider which can get an <see cref="CorsPolicy"/>.</param>
9342
/// <param name="loggerFactory">An instance of <see cref="ILoggerFactory"/>.</param>
9443
/// <param name="policyName">An optional name of the policy to be fetched.</param>
9544
public CorsMiddleware(
9645
RequestDelegate next,
9746
ICorsService corsService,
98-
ICorsPolicyProvider policyProvider,
9947
ILoggerFactory loggerFactory,
10048
string policyName)
10149
{
@@ -109,19 +57,13 @@ public CorsMiddleware(
10957
throw new ArgumentNullException(nameof(corsService));
11058
}
11159

112-
if (policyProvider == null)
113-
{
114-
throw new ArgumentNullException(nameof(policyProvider));
115-
}
116-
11760
if (loggerFactory == null)
11861
{
11962
throw new ArgumentNullException(nameof(loggerFactory));
12063
}
12164

12265
_next = next;
12366
CorsService = corsService;
124-
_corsPolicyProvider = policyProvider;
12567
_corsPolicyName = policyName;
12668
Logger = loggerFactory.CreateLogger<CorsMiddleware>();
12769
}
@@ -170,19 +112,19 @@ public CorsMiddleware(
170112
private ILogger Logger { get; }
171113

172114
/// <inheritdoc />
173-
public Task Invoke(HttpContext context)
115+
public Task Invoke(HttpContext context, ICorsPolicyProvider corsPolicyProvider)
174116
{
175117
if (!context.Request.Headers.ContainsKey(CorsConstants.Origin))
176118
{
177119
return _next(context);
178120
}
179121

180-
return InvokeCore(context);
122+
return InvokeCore(context, corsPolicyProvider);
181123
}
182124

183-
private async Task InvokeCore(HttpContext context)
125+
private async Task InvokeCore(HttpContext context, ICorsPolicyProvider corsPolicyProvider)
184126
{
185-
var corsPolicy = _policy ?? await _corsPolicyProvider?.GetPolicyAsync(context, _corsPolicyName);
127+
var corsPolicy = _policy ?? await corsPolicyProvider.GetPolicyAsync(context, _corsPolicyName);
186128
if (corsPolicy == null)
187129
{
188130
Logger?.NoCorsPolicyFound();
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[
2+
{
3+
"TypeId": "public class Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware",
4+
"MemberId": "public .ctor(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.AspNetCore.Cors.Infrastructure.ICorsService corsService, Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicy policy)",
5+
"Kind": "Removal"
6+
},
7+
{
8+
"TypeId": "public class Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware",
9+
"MemberId": "public .ctor(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.AspNetCore.Cors.Infrastructure.ICorsService corsService, Microsoft.AspNetCore.Cors.Infrastructure.ICorsPolicyProvider policyProvider)",
10+
"Kind": "Removal"
11+
},
12+
{
13+
"TypeId": "public class Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware",
14+
"MemberId": "public .ctor(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.AspNetCore.Cors.Infrastructure.ICorsService corsService, Microsoft.AspNetCore.Cors.Infrastructure.ICorsPolicyProvider policyProvider, System.String policyName)",
15+
"Kind": "Removal"
16+
},
17+
{
18+
"TypeId": "public class Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware",
19+
"MemberId": "public System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext context)",
20+
"Kind": "Removal"
21+
}
22+
]

test/Microsoft.AspNetCore.Cors.Test/CorsMiddlewareTests.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -336,15 +336,14 @@ public async Task Uses_PolicyProvider_AsFallback()
336336
var middleware = new CorsMiddleware(
337337
Mock.Of<RequestDelegate>(),
338338
corsService,
339-
mockProvider.Object,
340339
loggerFactory,
341340
policyName: null);
342341

343342
var httpContext = new DefaultHttpContext();
344343
httpContext.Request.Headers.Add(CorsConstants.Origin, new[] { "http://example.com" });
345344

346345
// Act
347-
await middleware.Invoke(httpContext);
346+
await middleware.Invoke(httpContext, mockProvider.Object);
348347

349348
// Assert
350349
mockProvider.Verify(
@@ -366,15 +365,14 @@ public async Task DoesNotSetHeaders_ForNoPolicy()
366365
var middleware = new CorsMiddleware(
367366
Mock.Of<RequestDelegate>(),
368367
corsService,
369-
mockProvider.Object,
370368
loggerFactory,
371369
policyName: null);
372370

373371
var httpContext = new DefaultHttpContext();
374372
httpContext.Request.Headers.Add(CorsConstants.Origin, new[] { "http://example.com" });
375373

376374
// Act
377-
await middleware.Invoke(httpContext);
375+
await middleware.Invoke(httpContext, mockProvider.Object);
378376

379377
// Assert
380378
Assert.Equal(200, httpContext.Response.StatusCode);

0 commit comments

Comments
 (0)