Set DisplayName for auth

This commit is contained in:
Hao Kung 2017-04-20 14:19:32 -07:00
parent 7fd15a2ae6
commit 4f20655850
16 changed files with 100 additions and 10 deletions

View File

@ -238,7 +238,7 @@ namespace SocialSample
foreach (var provider in await schemeProvider.GetAllSchemesAsync()) foreach (var provider in await schemeProvider.GetAllSchemesAsync())
{ {
// REVIEW: we lost access to display name (which is buried in the handler options) // REVIEW: we lost access to display name (which is buried in the handler options)
await context.Response.WriteAsync("<a href=\"?authscheme=" + provider.Name + "\">" + (provider.Name ?? "(suppressed)") + "</a><br>"); await context.Response.WriteAsync("<a href=\"?authscheme=" + provider.Name + "\">" + (provider.DisplayName ?? "(suppressed)") + "</a><br>");
} }
await context.Response.WriteAsync("</body></html>"); await context.Response.WriteAsync("</body></html>");
}); });

View File

@ -26,7 +26,7 @@ namespace Microsoft.Extensions.DependencyInjection
public static IServiceCollection AddFacebookAuthentication(this IServiceCollection services, string authenticationScheme, Action<FacebookOptions> configureOptions) public static IServiceCollection AddFacebookAuthentication(this IServiceCollection services, string authenticationScheme, Action<FacebookOptions> configureOptions)
{ {
return services.AddScheme<FacebookOptions, FacebookHandler>(authenticationScheme, configureOptions); return services.AddScheme<FacebookOptions, FacebookHandler>(authenticationScheme, authenticationScheme, configureOptions);
} }
} }
} }

View File

@ -26,7 +26,7 @@ namespace Microsoft.Extensions.DependencyInjection
public static IServiceCollection AddGoogleAuthentication(this IServiceCollection services, string authenticationScheme, Action<GoogleOptions> configureOptions) public static IServiceCollection AddGoogleAuthentication(this IServiceCollection services, string authenticationScheme, Action<GoogleOptions> configureOptions)
{ {
return services.AddScheme<GoogleOptions, GoogleHandler>(authenticationScheme, configureOptions); return services.AddScheme<GoogleOptions, GoogleHandler>(authenticationScheme, authenticationScheme, configureOptions);
} }
} }
} }

View File

@ -26,7 +26,7 @@ namespace Microsoft.Extensions.DependencyInjection
public static IServiceCollection AddMicrosoftAccountAuthentication(this IServiceCollection services, string authenticationScheme, Action<MicrosoftAccountOptions> configureOptions) public static IServiceCollection AddMicrosoftAccountAuthentication(this IServiceCollection services, string authenticationScheme, Action<MicrosoftAccountOptions> configureOptions)
{ {
return services.AddScheme<MicrosoftAccountOptions, MicrosoftAccountHandler>(authenticationScheme, configureOptions); return services.AddScheme<MicrosoftAccountOptions, MicrosoftAccountHandler>(authenticationScheme, authenticationScheme, configureOptions);
} }
} }
} }

View File

@ -10,6 +10,6 @@ namespace Microsoft.AspNetCore.Builder
public static class OAuthExtensions public static class OAuthExtensions
{ {
public static IServiceCollection AddOAuthAuthentication(this IServiceCollection services, string authenticationScheme, Action<OAuthOptions> configureOptions) => public static IServiceCollection AddOAuthAuthentication(this IServiceCollection services, string authenticationScheme, Action<OAuthOptions> configureOptions) =>
services.AddScheme<OAuthOptions, OAuthHandler<OAuthOptions>>(authenticationScheme, configureOptions); services.AddScheme<OAuthOptions, OAuthHandler<OAuthOptions>>(authenticationScheme, authenticationScheme, configureOptions);
} }
} }

View File

@ -26,7 +26,7 @@ namespace Microsoft.Extensions.DependencyInjection
public static IServiceCollection AddOpenIdConnectAuthentication(this IServiceCollection services, string authenticationScheme, Action<OpenIdConnectOptions> configureOptions) public static IServiceCollection AddOpenIdConnectAuthentication(this IServiceCollection services, string authenticationScheme, Action<OpenIdConnectOptions> configureOptions)
{ {
return services.AddScheme<OpenIdConnectOptions, OpenIdConnectHandler>(authenticationScheme, configureOptions); return services.AddScheme<OpenIdConnectOptions, OpenIdConnectHandler>(authenticationScheme, authenticationScheme, configureOptions);
} }
} }
} }

View File

@ -26,7 +26,7 @@ namespace Microsoft.Extensions.DependencyInjection
public static IServiceCollection AddTwitterAuthentication(this IServiceCollection services, string authenticationScheme, Action<TwitterOptions> configureOptions) public static IServiceCollection AddTwitterAuthentication(this IServiceCollection services, string authenticationScheme, Action<TwitterOptions> configureOptions)
{ {
return services.AddScheme<TwitterOptions, TwitterHandler>(authenticationScheme, configureOptions); return services.AddScheme<TwitterOptions, TwitterHandler>(authenticationScheme, authenticationScheme, configureOptions);
} }
} }
} }

View File

@ -45,7 +45,7 @@ namespace Microsoft.Extensions.DependencyInjection
return services; return services;
} }
public static IServiceCollection AddScheme<TOptions, THandler>(this IServiceCollection services, string authenticationScheme, Action<AuthenticationSchemeBuilder> configureScheme, Action<TOptions> configureOptions) public static IServiceCollection AddScheme<TOptions, THandler>(this IServiceCollection services, string authenticationScheme, string displayName, Action<AuthenticationSchemeBuilder> configureScheme, Action<TOptions> configureOptions)
where TOptions : AuthenticationSchemeOptions, new() where TOptions : AuthenticationSchemeOptions, new()
where THandler : AuthenticationHandler<TOptions> where THandler : AuthenticationHandler<TOptions>
{ {
@ -53,6 +53,7 @@ namespace Microsoft.Extensions.DependencyInjection
{ {
o.AddScheme(authenticationScheme, scheme => { o.AddScheme(authenticationScheme, scheme => {
scheme.HandlerType = typeof(THandler); scheme.HandlerType = typeof(THandler);
scheme.DisplayName = displayName;
configureScheme?.Invoke(scheme); configureScheme?.Invoke(scheme);
}); });
}); });
@ -67,6 +68,11 @@ namespace Microsoft.Extensions.DependencyInjection
public static IServiceCollection AddScheme<TOptions, THandler>(this IServiceCollection services, string authenticationScheme, Action<TOptions> configureOptions) public static IServiceCollection AddScheme<TOptions, THandler>(this IServiceCollection services, string authenticationScheme, Action<TOptions> configureOptions)
where TOptions : AuthenticationSchemeOptions, new() where TOptions : AuthenticationSchemeOptions, new()
where THandler : AuthenticationHandler<TOptions> where THandler : AuthenticationHandler<TOptions>
=> services.AddScheme<TOptions, THandler>(authenticationScheme, configureScheme: null, configureOptions: configureOptions); => services.AddScheme<TOptions, THandler>(authenticationScheme, displayName: null, configureScheme: null, configureOptions: configureOptions);
public static IServiceCollection AddScheme<TOptions, THandler>(this IServiceCollection services, string authenticationScheme, string displayName, Action<TOptions> configureOptions)
where TOptions : AuthenticationSchemeOptions, new()
where THandler : AuthenticationHandler<TOptions>
=> services.AddScheme<TOptions, THandler>(authenticationScheme, displayName, configureScheme: null, configureOptions: configureOptions);
} }
} }

View File

@ -26,6 +26,18 @@ namespace Microsoft.AspNetCore.Authentication.Cookies
{ {
private TestClock _clock = new TestClock(); private TestClock _clock = new TestClock();
[Fact]
public async Task VerifySchemeDefaults()
{
var services = new ServiceCollection().AddCookieAuthentication();
var sp = services.BuildServiceProvider();
var schemeProvider = sp.GetRequiredService<IAuthenticationSchemeProvider>();
var scheme = await schemeProvider.GetSchemeAsync(CookieAuthenticationDefaults.AuthenticationScheme);
Assert.NotNull(scheme);
Assert.Equal("CookieAuthenticationHandler", scheme.HandlerType.Name);
Assert.Null(scheme.DisplayName);
}
[Fact] [Fact]
public async Task NormalRequestPassesThrough() public async Task NormalRequestPassesThrough()
{ {

View File

@ -99,7 +99,7 @@ namespace Microsoft.AspNetCore.Authentication
{ {
var name = remainder.Value.Substring(1); var name = remainder.Value.Substring(1);
var auth = context.RequestServices.GetRequiredService<IAuthenticationSchemeProvider>(); var auth = context.RequestServices.GetRequiredService<IAuthenticationSchemeProvider>();
var scheme = new AuthenticationScheme(name, typeof(TestHandler)); var scheme = new AuthenticationScheme(name, name, typeof(TestHandler));
auth.AddScheme(scheme); auth.AddScheme(scheme);
} }
else if (req.Path.StartsWithSegments(new PathString("/auth"), out remainder)) else if (req.Path.StartsWithSegments(new PathString("/auth"), out remainder))

View File

@ -27,6 +27,18 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
{ {
public class FacebookTests public class FacebookTests
{ {
[Fact]
public async Task VerifySchemeDefaults()
{
var services = new ServiceCollection().AddFacebookAuthentication().AddSingleton<IConfiguration>(new ConfigurationBuilder().Build());
var sp = services.BuildServiceProvider();
var schemeProvider = sp.GetRequiredService<IAuthenticationSchemeProvider>();
var scheme = await schemeProvider.GetSchemeAsync(FacebookDefaults.AuthenticationScheme);
Assert.NotNull(scheme);
Assert.Equal("FacebookHandler", scheme.HandlerType.Name);
Assert.Equal(FacebookDefaults.AuthenticationScheme, scheme.DisplayName);
}
[Fact] [Fact]
public void AddCanBindAgainstDefaultConfig() public void AddCanBindAgainstDefaultConfig()
{ {

View File

@ -26,6 +26,18 @@ namespace Microsoft.AspNetCore.Authentication.Google
{ {
public class GoogleTests public class GoogleTests
{ {
[Fact]
public async Task VerifySchemeDefaults()
{
var services = new ServiceCollection().AddGoogleAuthentication().AddSingleton<IConfiguration>(new ConfigurationBuilder().Build());
var sp = services.BuildServiceProvider();
var schemeProvider = sp.GetRequiredService<IAuthenticationSchemeProvider>();
var scheme = await schemeProvider.GetSchemeAsync(GoogleDefaults.AuthenticationScheme);
Assert.NotNull(scheme);
Assert.Equal("GoogleHandler", scheme.HandlerType.Name);
Assert.Equal(GoogleDefaults.AuthenticationScheme, scheme.DisplayName);
}
[Fact] [Fact]
public void AddCanBindAgainstDefaultConfig() public void AddCanBindAgainstDefaultConfig()
{ {

View File

@ -26,6 +26,18 @@ namespace Microsoft.AspNetCore.Authentication.JwtBearer
{ {
public class JwtBearerTests public class JwtBearerTests
{ {
[Fact]
public async Task VerifySchemeDefaults()
{
var services = new ServiceCollection().AddJwtBearerAuthentication().AddSingleton<IConfiguration>(new ConfigurationBuilder().Build());
var sp = services.BuildServiceProvider();
var schemeProvider = sp.GetRequiredService<IAuthenticationSchemeProvider>();
var scheme = await schemeProvider.GetSchemeAsync(JwtBearerDefaults.AuthenticationScheme);
Assert.NotNull(scheme);
Assert.Equal("JwtBearerHandler", scheme.HandlerType.Name);
Assert.Null(scheme.DisplayName);
}
[Fact] [Fact]
public void AddCanBindAgainstDefaultConfig() public void AddCanBindAgainstDefaultConfig()
{ {

View File

@ -27,6 +27,18 @@ namespace Microsoft.AspNetCore.Authentication.Tests.MicrosoftAccount
{ {
public class MicrosoftAccountTests public class MicrosoftAccountTests
{ {
[Fact]
public async Task VerifySchemeDefaults()
{
var services = new ServiceCollection().AddMicrosoftAccountAuthentication().AddSingleton<IConfiguration>(new ConfigurationBuilder().Build());
var sp = services.BuildServiceProvider();
var schemeProvider = sp.GetRequiredService<IAuthenticationSchemeProvider>();
var scheme = await schemeProvider.GetSchemeAsync(MicrosoftAccountDefaults.AuthenticationScheme);
Assert.NotNull(scheme);
Assert.Equal("MicrosoftAccountHandler", scheme.HandlerType.Name);
Assert.Equal(MicrosoftAccountDefaults.AuthenticationScheme, scheme.DisplayName);
}
[Fact] [Fact]
public void AddCanBindAgainstDefaultConfig() public void AddCanBindAgainstDefaultConfig()
{ {

View File

@ -15,6 +15,18 @@ namespace Microsoft.AspNetCore.Authentication.OAuth
{ {
public class OAuthTests public class OAuthTests
{ {
[Fact]
public async Task VerifySchemeDefaults()
{
var services = new ServiceCollection().AddOAuthAuthentication("oauth", o => { });
var sp = services.BuildServiceProvider();
var schemeProvider = sp.GetRequiredService<IAuthenticationSchemeProvider>();
var scheme = await schemeProvider.GetSchemeAsync("oauth");
Assert.NotNull(scheme);
Assert.Equal("OAuthHandler`1", scheme.HandlerType.Name);
Assert.Equal("oauth", scheme.DisplayName);
}
[Fact] [Fact]
public async Task ThrowsIfClientIdMissing() public async Task ThrowsIfClientIdMissing()
{ {

View File

@ -20,6 +20,18 @@ namespace Microsoft.AspNetCore.Authentication.Twitter
{ {
public class TwitterTests public class TwitterTests
{ {
[Fact]
public async Task VerifySchemeDefaults()
{
var services = new ServiceCollection().AddTwitterAuthentication().AddSingleton<IConfiguration>(new ConfigurationBuilder().Build());
var sp = services.BuildServiceProvider();
var schemeProvider = sp.GetRequiredService<IAuthenticationSchemeProvider>();
var scheme = await schemeProvider.GetSchemeAsync(TwitterDefaults.AuthenticationScheme);
Assert.NotNull(scheme);
Assert.Equal("TwitterHandler", scheme.HandlerType.Name);
Assert.Equal(TwitterDefaults.AuthenticationScheme, scheme.DisplayName);
}
[Fact] [Fact]
public void AddCanBindAgainstDefaultConfig() public void AddCanBindAgainstDefaultConfig()
{ {