diff --git a/src/Microsoft.AspNetCore.Authentication.Core/AuthenticationCoreServiceCollectionExtensions.cs b/src/Microsoft.AspNetCore.Authentication.Core/AuthenticationCoreServiceCollectionExtensions.cs index fdf85a9b..9089761d 100644 --- a/src/Microsoft.AspNetCore.Authentication.Core/AuthenticationCoreServiceCollectionExtensions.cs +++ b/src/Microsoft.AspNetCore.Authentication.Core/AuthenticationCoreServiceCollectionExtensions.cs @@ -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 @@ public static IServiceCollection AddAuthenticationCore(this IServiceCollection s services.TryAddSingleton(); // Can be replaced with scoped ones that use DbContext services.TryAddScoped(); services.TryAddSingleton(); + services.AddTransient, DefaultConfigureOptions>(); return services; } @@ -52,5 +55,13 @@ public static IServiceCollection AddAuthenticationCore(this IServiceCollection s services.Configure(configureOptions); return services; } + + private class DefaultConfigureOptions : ConfigureDefaultOptions + { + public DefaultConfigureOptions(IConfiguration config) : + base(options => config.GetSection("Microsoft:AspNetCore:Authentication").Bind(options)) + { } + } + } } diff --git a/src/Microsoft.AspNetCore.Authentication.Core/Microsoft.AspNetCore.Authentication.Core.csproj b/src/Microsoft.AspNetCore.Authentication.Core/Microsoft.AspNetCore.Authentication.Core.csproj index 0ceaa4d3..94550732 100644 --- a/src/Microsoft.AspNetCore.Authentication.Core/Microsoft.AspNetCore.Authentication.Core.csproj +++ b/src/Microsoft.AspNetCore.Authentication.Core/Microsoft.AspNetCore.Authentication.Core.csproj @@ -15,6 +15,7 @@ + diff --git a/src/Microsoft.AspNetCore.Http.Abstractions/PathString.cs b/src/Microsoft.AspNetCore.Http.Abstractions/PathString.cs index 50124e5e..b82d4442 100644 --- a/src/Microsoft.AspNetCore.Http.Abstractions/PathString.cs +++ b/src/Microsoft.AspNetCore.Http.Abstractions/PathString.cs @@ -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 /// /// Provides correct escaping for Path and PathBase values when needed to reconstruct a request or redirect URI string /// + [TypeConverter(typeof(PathStringConverter))] public struct PathString : IEquatable { private static readonly char[] splitChar = { '/' }; @@ -453,4 +456,12 @@ public static implicit operator string(PathString path) return path.ToString(); } } + + internal class PathStringConverter : TypeConverter + { + public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) + { + return new PathString((string)value); + } + } } diff --git a/test/Microsoft.AspNetCore.Authentication.Core.Test/ConfigTests.cs b/test/Microsoft.AspNetCore.Authentication.Core.Test/ConfigTests.cs new file mode 100644 index 00000000..31c3ee50 --- /dev/null +++ b/test/Microsoft.AspNetCore.Authentication.Core.Test/ConfigTests.cs @@ -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 + { + {"Microsoft:AspNetCore:Authentication:DefaultSignInScheme", ""}, + {"Microsoft:AspNetCore:Authentication:DefaultAuthenticateScheme", ""}, + {"Microsoft:AspNetCore:Authentication:DefaultChallengeScheme", ""} + }; + var configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.AddInMemoryCollection(dic); + var config = configurationBuilder.Build(); + var services = new ServiceCollection() + .AddOptions() + .AddSingleton, ConfigureDefaults>() + .AddAuthenticationCore() + .AddSingleton(config); + var sp = services.BuildServiceProvider(); + + var options = sp.GetRequiredService>().Value; + Assert.Equal("", options.DefaultAuthenticateScheme); + Assert.Equal("", options.DefaultChallengeScheme); + Assert.Equal("", options.DefaultSignInScheme); + } + } +} diff --git a/test/Microsoft.AspNetCore.Http.Abstractions.Tests/PathStringTests.cs b/test/Microsoft.AspNetCore.Http.Abstractions.Tests/PathStringTests.cs index 60a1a9b0..365a9de3 100644 --- a/test/Microsoft.AspNetCore.Http.Abstractions.Tests/PathStringTests.cs +++ b/test/Microsoft.AspNetCore.Http.Abstractions.Tests/PathStringTests.cs @@ -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 @@ public void ToUriComponentEscapeCorrectly(string category, string input, string 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()); + } } }