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

Commit ec36c3d

Browse files
authored
Bind AuthenticationOptions to config + PathString type converter (#851)
1 parent 7de909a commit ec36c3d

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System;
55
using Microsoft.AspNetCore.Authentication;
66
using Microsoft.Extensions.DependencyInjection.Extensions;
7+
using Microsoft.Extensions.Configuration;
8+
using Microsoft.Extensions.Options.Infrastructure;
79

810
namespace Microsoft.Extensions.DependencyInjection
911
{
@@ -28,6 +30,7 @@ public static IServiceCollection AddAuthenticationCore(this IServiceCollection s
2830
services.TryAddSingleton<IClaimsTransformation, NoopClaimsTransformation>(); // Can be replaced with scoped ones that use DbContext
2931
services.TryAddScoped<IAuthenticationHandlerProvider, AuthenticationHandlerProvider>();
3032
services.TryAddSingleton<IAuthenticationSchemeProvider, AuthenticationSchemeProvider>();
33+
services.AddTransient<ConfigureDefaultOptions<AuthenticationOptions>, DefaultConfigureOptions>();
3134
return services;
3235
}
3336

@@ -52,5 +55,13 @@ public static IServiceCollection AddAuthenticationCore(this IServiceCollection s
5255
services.Configure(configureOptions);
5356
return services;
5457
}
58+
59+
private class DefaultConfigureOptions : ConfigureDefaultOptions<AuthenticationOptions>
60+
{
61+
public DefaultConfigureOptions(IConfiguration config) :
62+
base(options => config.GetSection("Microsoft:AspNetCore:Authentication").Bind(options))
63+
{ }
64+
}
65+
5566
}
5667
}

src/Microsoft.AspNetCore.Authentication.Core/Microsoft.AspNetCore.Authentication.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<ProjectReference Include="..\Microsoft.AspNetCore.Authentication.Abstractions\Microsoft.AspNetCore.Authentication.Abstractions.csproj" />
1616
<ProjectReference Include="..\Microsoft.AspNetCore.Http\Microsoft.AspNetCore.Http.csproj" />
1717
<ProjectReference Include="..\Microsoft.AspNetCore.Http.Extensions\Microsoft.AspNetCore.Http.Extensions.csproj" />
18+
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="$(AspNetCoreVersion)" />
1819
<PackageReference Include="Microsoft.Extensions.TaskCache.Sources" Version="$(AspNetCoreVersion)" PrivateAssets="All" />
1920
</ItemGroup>
2021

src/Microsoft.AspNetCore.Http.Abstractions/PathString.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.ComponentModel;
6+
using System.Globalization;
57
using System.Text;
68
using Microsoft.AspNetCore.Http.Abstractions;
79
using Microsoft.AspNetCore.Http.Internal;
@@ -11,6 +13,7 @@ namespace Microsoft.AspNetCore.Http
1113
/// <summary>
1214
/// Provides correct escaping for Path and PathBase values when needed to reconstruct a request or redirect URI string
1315
/// </summary>
16+
[TypeConverter(typeof(PathStringConverter))]
1417
public struct PathString : IEquatable<PathString>
1518
{
1619
private static readonly char[] splitChar = { '/' };
@@ -453,4 +456,12 @@ public static implicit operator string(PathString path)
453456
return path.ToString();
454457
}
455458
}
459+
460+
internal class PathStringConverter : TypeConverter
461+
{
462+
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
463+
{
464+
return new PathString((string)value);
465+
}
466+
}
456467
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Collections.Generic;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Options;
8+
using Microsoft.Extensions.Options.Infrastructure;
9+
using Xunit;
10+
11+
namespace Microsoft.AspNetCore.Authentication
12+
{
13+
public class ConfigTests
14+
{
15+
[Fact]
16+
public void AddCanBindAgainstDefaultConfig()
17+
{
18+
var dic = new Dictionary<string, string>
19+
{
20+
{"Microsoft:AspNetCore:Authentication:DefaultSignInScheme", "<signin>"},
21+
{"Microsoft:AspNetCore:Authentication:DefaultAuthenticateScheme", "<auth>"},
22+
{"Microsoft:AspNetCore:Authentication:DefaultChallengeScheme", "<challenge>"}
23+
};
24+
var configurationBuilder = new ConfigurationBuilder();
25+
configurationBuilder.AddInMemoryCollection(dic);
26+
var config = configurationBuilder.Build();
27+
var services = new ServiceCollection()
28+
.AddOptions()
29+
.AddSingleton<IConfigureOptions<AuthenticationOptions>, ConfigureDefaults<AuthenticationOptions>>()
30+
.AddAuthenticationCore()
31+
.AddSingleton<IConfiguration>(config);
32+
var sp = services.BuildServiceProvider();
33+
34+
var options = sp.GetRequiredService<IOptions<AuthenticationOptions>>().Value;
35+
Assert.Equal("<auth>", options.DefaultAuthenticateScheme);
36+
Assert.Equal("<challenge>", options.DefaultChallengeScheme);
37+
Assert.Equal("<signin>", options.DefaultSignInScheme);
38+
}
39+
}
40+
}

test/Microsoft.AspNetCore.Http.Abstractions.Tests/PathStringTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.ComponentModel;
56
using Microsoft.AspNetCore.Testing;
67
using Xunit;
78

@@ -205,5 +206,13 @@ public void ToUriComponentEscapeCorrectly(string category, string input, string
205206

206207
Assert.Equal(expected, path.ToUriComponent());
207208
}
209+
210+
[Fact]
211+
public void PathStringConvertsFromString()
212+
{
213+
var converter = TypeDescriptor.GetConverter(typeof(PathString));
214+
PathString result = (PathString)converter.ConvertFromInvariantString("/foo");
215+
Assert.Equal("/foo", result.ToString());
216+
}
208217
}
209218
}

0 commit comments

Comments
 (0)