Bind AuthenticationOptions to config + PathString type converter (#851)
This commit is contained in:
parent
ed4db47869
commit
a55b818a07
|
|
@ -4,6 +4,8 @@
|
|||
using System;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Options.Infrastructure;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyInjection
|
||||
{
|
||||
|
|
@ -28,6 +30,7 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||
services.TryAddSingleton<IClaimsTransformation, NoopClaimsTransformation>(); // Can be replaced with scoped ones that use DbContext
|
||||
services.TryAddScoped<IAuthenticationHandlerProvider, AuthenticationHandlerProvider>();
|
||||
services.TryAddSingleton<IAuthenticationSchemeProvider, AuthenticationSchemeProvider>();
|
||||
services.AddTransient<ConfigureDefaultOptions<AuthenticationOptions>, DefaultConfigureOptions>();
|
||||
return services;
|
||||
}
|
||||
|
||||
|
|
@ -52,5 +55,13 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||
services.Configure(configureOptions);
|
||||
return services;
|
||||
}
|
||||
|
||||
private class DefaultConfigureOptions : ConfigureDefaultOptions<AuthenticationOptions>
|
||||
{
|
||||
public DefaultConfigureOptions(IConfiguration config) :
|
||||
base(options => config.GetSection("Microsoft:AspNetCore:Authentication").Bind(options))
|
||||
{ }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@
|
|||
<ProjectReference Include="..\Microsoft.AspNetCore.Authentication.Abstractions\Microsoft.AspNetCore.Authentication.Abstractions.csproj" />
|
||||
<ProjectReference Include="..\Microsoft.AspNetCore.Http\Microsoft.AspNetCore.Http.csproj" />
|
||||
<ProjectReference Include="..\Microsoft.AspNetCore.Http.Extensions\Microsoft.AspNetCore.Http.Extensions.csproj" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="$(AspNetCoreVersion)" />
|
||||
<PackageReference Include="Microsoft.Extensions.TaskCache.Sources" Version="$(AspNetCoreVersion)" PrivateAssets="All" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Http.Abstractions;
|
||||
using Microsoft.AspNetCore.Http.Internal;
|
||||
|
|
@ -11,6 +13,7 @@ namespace Microsoft.AspNetCore.Http
|
|||
/// <summary>
|
||||
/// Provides correct escaping for Path and PathBase values when needed to reconstruct a request or redirect URI string
|
||||
/// </summary>
|
||||
[TypeConverter(typeof(PathStringConverter))]
|
||||
public struct PathString : IEquatable<PathString>
|
||||
{
|
||||
private static readonly char[] splitChar = { '/' };
|
||||
|
|
@ -453,4 +456,12 @@ namespace Microsoft.AspNetCore.Http
|
|||
return path.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
internal class PathStringConverter : TypeConverter
|
||||
{
|
||||
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
|
||||
{
|
||||
return new PathString((string)value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,40 @@
|
|||
// 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.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.Extensions.Options.Infrastructure;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNetCore.Authentication
|
||||
{
|
||||
public class ConfigTests
|
||||
{
|
||||
[Fact]
|
||||
public void AddCanBindAgainstDefaultConfig()
|
||||
{
|
||||
var dic = new Dictionary<string, string>
|
||||
{
|
||||
{"Microsoft:AspNetCore:Authentication:DefaultSignInScheme", "<signin>"},
|
||||
{"Microsoft:AspNetCore:Authentication:DefaultAuthenticateScheme", "<auth>"},
|
||||
{"Microsoft:AspNetCore:Authentication:DefaultChallengeScheme", "<challenge>"}
|
||||
};
|
||||
var configurationBuilder = new ConfigurationBuilder();
|
||||
configurationBuilder.AddInMemoryCollection(dic);
|
||||
var config = configurationBuilder.Build();
|
||||
var services = new ServiceCollection()
|
||||
.AddOptions()
|
||||
.AddSingleton<IConfigureOptions<AuthenticationOptions>, ConfigureDefaults<AuthenticationOptions>>()
|
||||
.AddAuthenticationCore()
|
||||
.AddSingleton<IConfiguration>(config);
|
||||
var sp = services.BuildServiceProvider();
|
||||
|
||||
var options = sp.GetRequiredService<IOptions<AuthenticationOptions>>().Value;
|
||||
Assert.Equal("<auth>", options.DefaultAuthenticateScheme);
|
||||
Assert.Equal("<challenge>", options.DefaultChallengeScheme);
|
||||
Assert.Equal("<signin>", options.DefaultSignInScheme);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using Microsoft.AspNetCore.Testing;
|
||||
using Xunit;
|
||||
|
||||
|
|
@ -205,5 +206,13 @@ namespace Microsoft.AspNetCore.Http
|
|||
|
||||
Assert.Equal(expected, path.ToUriComponent());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PathStringConvertsFromString()
|
||||
{
|
||||
var converter = TypeDescriptor.GetConverter(typeof(PathString));
|
||||
PathString result = (PathString)converter.ConvertFromInvariantString("/foo");
|
||||
Assert.Equal("/foo", result.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue