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

Bind AuthenticationOptions to config + PathString type converter #851

Merged
merged 2 commits into from
Jun 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -28,6 +30,7 @@ public static IServiceCollection AddAuthenticationCore(this IServiceCollection s
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;
}

Expand All @@ -52,5 +55,13 @@ public static IServiceCollection AddAuthenticationCore(this IServiceCollection s
services.Configure(configureOptions);
return services;
}

private class DefaultConfigureOptions : ConfigureDefaultOptions<AuthenticationOptions>
{
public DefaultConfigureOptions(IConfiguration config) :
base(options => config.GetSection("Microsoft:AspNetCore:Authentication").Bind(options))
{ }
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
11 changes: 11 additions & 0 deletions src/Microsoft.AspNetCore.Http.Abstractions/PathString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = { '/' };
Expand Down Expand Up @@ -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);
}
}
}
40 changes: 40 additions & 0 deletions test/Microsoft.AspNetCore.Authentication.Core.Test/ConfigTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
}
}