Add TryAddScheme (#14448)
This commit is contained in:
parent
c1cc168441
commit
a0eb923ab5
|
|
@ -149,6 +149,7 @@ namespace Microsoft.AspNetCore.Authentication
|
||||||
System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Authentication.AuthenticationScheme>> GetRequestHandlerSchemesAsync();
|
System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Authentication.AuthenticationScheme>> GetRequestHandlerSchemesAsync();
|
||||||
System.Threading.Tasks.Task<Microsoft.AspNetCore.Authentication.AuthenticationScheme> GetSchemeAsync(string name);
|
System.Threading.Tasks.Task<Microsoft.AspNetCore.Authentication.AuthenticationScheme> GetSchemeAsync(string name);
|
||||||
void RemoveScheme(string name);
|
void RemoveScheme(string name);
|
||||||
|
bool TryAddScheme(Microsoft.AspNetCore.Authentication.AuthenticationScheme scheme) { throw null; }
|
||||||
}
|
}
|
||||||
public partial interface IAuthenticationService
|
public partial interface IAuthenticationService
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -71,6 +71,23 @@ namespace Microsoft.AspNetCore.Authentication
|
||||||
/// <param name="scheme">The scheme.</param>
|
/// <param name="scheme">The scheme.</param>
|
||||||
void AddScheme(AuthenticationScheme scheme);
|
void AddScheme(AuthenticationScheme scheme);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Registers a scheme for use by <see cref="IAuthenticationService"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="scheme">The scheme.</param>
|
||||||
|
/// <returns>true if the scheme was added successfully.</returns>
|
||||||
|
bool TryAddScheme(AuthenticationScheme scheme)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
AddScheme(scheme);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Removes a scheme, preventing it from being used by <see cref="IAuthenticationService"/>.
|
/// Removes a scheme, preventing it from being used by <see cref="IAuthenticationService"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ namespace Microsoft.AspNetCore.Authentication
|
||||||
public virtual System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Authentication.AuthenticationScheme>> GetRequestHandlerSchemesAsync() { throw null; }
|
public virtual System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Authentication.AuthenticationScheme>> GetRequestHandlerSchemesAsync() { throw null; }
|
||||||
public virtual System.Threading.Tasks.Task<Microsoft.AspNetCore.Authentication.AuthenticationScheme> GetSchemeAsync(string name) { throw null; }
|
public virtual System.Threading.Tasks.Task<Microsoft.AspNetCore.Authentication.AuthenticationScheme> GetSchemeAsync(string name) { throw null; }
|
||||||
public virtual void RemoveScheme(string name) { }
|
public virtual void RemoveScheme(string name) { }
|
||||||
|
public virtual bool TryAddScheme(Microsoft.AspNetCore.Authentication.AuthenticationScheme scheme) { throw null; }
|
||||||
}
|
}
|
||||||
public partial class AuthenticationService : Microsoft.AspNetCore.Authentication.IAuthenticationService
|
public partial class AuthenticationService : Microsoft.AspNetCore.Authentication.IAuthenticationService
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -133,17 +133,18 @@ namespace Microsoft.AspNetCore.Authentication
|
||||||
/// Registers a scheme for use by <see cref="IAuthenticationService"/>.
|
/// Registers a scheme for use by <see cref="IAuthenticationService"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="scheme">The scheme.</param>
|
/// <param name="scheme">The scheme.</param>
|
||||||
public virtual void AddScheme(AuthenticationScheme scheme)
|
/// <returns>true if the scheme was added successfully.</returns>
|
||||||
|
public virtual bool TryAddScheme(AuthenticationScheme scheme)
|
||||||
{
|
{
|
||||||
if (_schemes.ContainsKey(scheme.Name))
|
if (_schemes.ContainsKey(scheme.Name))
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException("Scheme already exists: " + scheme.Name);
|
return false;
|
||||||
}
|
}
|
||||||
lock (_lock)
|
lock (_lock)
|
||||||
{
|
{
|
||||||
if (_schemes.ContainsKey(scheme.Name))
|
if (_schemes.ContainsKey(scheme.Name))
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException("Scheme already exists: " + scheme.Name);
|
return false;
|
||||||
}
|
}
|
||||||
if (typeof(IAuthenticationRequestHandler).IsAssignableFrom(scheme.HandlerType))
|
if (typeof(IAuthenticationRequestHandler).IsAssignableFrom(scheme.HandlerType))
|
||||||
{
|
{
|
||||||
|
|
@ -152,6 +153,26 @@ namespace Microsoft.AspNetCore.Authentication
|
||||||
}
|
}
|
||||||
_schemes[scheme.Name] = scheme;
|
_schemes[scheme.Name] = scheme;
|
||||||
_schemesCopy = _schemes.Values.ToArray();
|
_schemesCopy = _schemes.Values.ToArray();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Registers a scheme for use by <see cref="IAuthenticationService"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="scheme">The scheme.</param>
|
||||||
|
public virtual void AddScheme(AuthenticationScheme scheme)
|
||||||
|
{
|
||||||
|
if (_schemes.ContainsKey(scheme.Name))
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Scheme already exists: " + scheme.Name);
|
||||||
|
}
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
if (!TryAddScheme(scheme))
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Scheme already exists: " + scheme.Name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -133,6 +133,23 @@ namespace Microsoft.AspNetCore.Authentication
|
||||||
Assert.Contains("Scheme already exists: signin", error.Message);
|
Assert.Contains("Scheme already exists: signin", error.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void CanSafelyTryAddSchemes()
|
||||||
|
{
|
||||||
|
var services = new ServiceCollection().AddOptions().AddAuthenticationCore(o =>
|
||||||
|
{
|
||||||
|
}).BuildServiceProvider();
|
||||||
|
|
||||||
|
var o = services.GetRequiredService<IAuthenticationSchemeProvider>();
|
||||||
|
Assert.True(o.TryAddScheme(new AuthenticationScheme("signin", "whatever", typeof(Handler))));
|
||||||
|
Assert.True(o.TryAddScheme(new AuthenticationScheme("signin2", "whatever", typeof(Handler))));
|
||||||
|
Assert.False(o.TryAddScheme(new AuthenticationScheme("signin", "whatever", typeof(Handler))));
|
||||||
|
Assert.True(o.TryAddScheme(new AuthenticationScheme("signin3", "whatever", typeof(Handler))));
|
||||||
|
Assert.False(o.TryAddScheme(new AuthenticationScheme("signin2", "whatever", typeof(Handler))));
|
||||||
|
o.RemoveScheme("signin2");
|
||||||
|
Assert.True(o.TryAddScheme(new AuthenticationScheme("signin2", "whatever", typeof(Handler))));
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task LookupUsesProvidedStringComparer()
|
public async Task LookupUsesProvidedStringComparer()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue