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

Commit ec176b2

Browse files
authored
Merge release/2.2 (#1055)
* Add safe copy for enumeration (#1052)
1 parent d17d915 commit ec176b2

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/Microsoft.AspNetCore.Authentication.Core/AuthenticationSchemeProvider.cs

+13-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Linq;
67
using System.Threading.Tasks;
78
using Microsoft.AspNetCore.Http;
89
using Microsoft.Extensions.Options;
@@ -49,6 +50,9 @@ protected AuthenticationSchemeProvider(IOptions<AuthenticationOptions> options,
4950

5051
private readonly IDictionary<string, AuthenticationScheme> _schemes;
5152
private readonly List<AuthenticationScheme> _requestHandlers;
53+
// Used as a safe return value for enumeration apis
54+
private IEnumerable<AuthenticationScheme> _schemesCopy = Array.Empty<AuthenticationScheme>();
55+
private IEnumerable<AuthenticationScheme> _requestHandlersCopy = Array.Empty<AuthenticationScheme>();
5256

5357
private Task<AuthenticationScheme> GetDefaultSchemeAsync()
5458
=> _options.DefaultScheme != null
@@ -123,7 +127,7 @@ public virtual Task<AuthenticationScheme> GetSchemeAsync(string name)
123127
/// </summary>
124128
/// <returns>The schemes in priority order for request handling</returns>
125129
public virtual Task<IEnumerable<AuthenticationScheme>> GetRequestHandlerSchemesAsync()
126-
=> Task.FromResult<IEnumerable<AuthenticationScheme>>(_requestHandlers);
130+
=> Task.FromResult(_requestHandlersCopy);
127131

128132
/// <summary>
129133
/// Registers a scheme for use by <see cref="IAuthenticationService"/>.
@@ -144,8 +148,10 @@ public virtual void AddScheme(AuthenticationScheme scheme)
144148
if (typeof(IAuthenticationRequestHandler).IsAssignableFrom(scheme.HandlerType))
145149
{
146150
_requestHandlers.Add(scheme);
151+
_requestHandlersCopy = _requestHandlers.ToArray();
147152
}
148153
_schemes[scheme.Name] = scheme;
154+
_schemesCopy = _schemes.Values.ToArray();
149155
}
150156
}
151157

@@ -164,13 +170,17 @@ public virtual void RemoveScheme(string name)
164170
if (_schemes.ContainsKey(name))
165171
{
166172
var scheme = _schemes[name];
167-
_requestHandlers.Remove(scheme);
173+
if (_requestHandlers.Remove(scheme))
174+
{
175+
_requestHandlersCopy = _requestHandlers.ToArray();
176+
}
168177
_schemes.Remove(name);
178+
_schemesCopy = _schemes.Values.ToArray();
169179
}
170180
}
171181
}
172182

173183
public virtual Task<IEnumerable<AuthenticationScheme>> GetAllSchemesAsync()
174-
=> Task.FromResult<IEnumerable<AuthenticationScheme>>(_schemes.Values);
184+
=> Task.FromResult(_schemesCopy);
175185
}
176186
}

0 commit comments

Comments
 (0)