// 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;
using System.Collections.Generic;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
namespace Microsoft.Extensions.DependencyInjection
{
///
/// Extension methods for setting up authentication services in an .
///
public static class AuthenticationServiceCollectionExtensions
{
public static IServiceCollection AddAuthentication(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
services.AddAuthenticationCore();
services.AddDataProtection();
services.AddWebEncoders();
services.TryAddSingleton();
return services;
}
public static IServiceCollection AddAuthentication(this IServiceCollection services, Action configureOptions) {
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
services.AddAuthentication();
services.Configure(configureOptions);
return services;
}
public static IServiceCollection AddScheme(this IServiceCollection services, string authenticationScheme, string displayName, Action configureScheme, Action configureOptions)
where TOptions : AuthenticationSchemeOptions, new()
where THandler : AuthenticationHandler
{
services.AddAuthentication(o =>
{
o.AddScheme(authenticationScheme, scheme => {
scheme.HandlerType = typeof(THandler);
scheme.DisplayName = displayName;
configureScheme?.Invoke(scheme);
});
});
if (configureOptions != null)
{
services.Configure(authenticationScheme, configureOptions);
}
services.AddTransient();
return services;
}
public static IServiceCollection AddScheme(this IServiceCollection services, string authenticationScheme, Action configureOptions)
where TOptions : AuthenticationSchemeOptions, new()
where THandler : AuthenticationHandler
=> services.AddScheme(authenticationScheme, displayName: null, configureScheme: null, configureOptions: configureOptions);
public static IServiceCollection AddScheme(this IServiceCollection services, string authenticationScheme, string displayName, Action configureOptions)
where TOptions : AuthenticationSchemeOptions, new()
where THandler : AuthenticationHandler
=> services.AddScheme(authenticationScheme, displayName, configureScheme: null, configureOptions: configureOptions);
public static IServiceCollection AddRemoteScheme(this IServiceCollection services, string authenticationScheme, string displayName, Action configureOptions)
where TOptions : RemoteAuthenticationOptions, new()
where THandler : RemoteAuthenticationHandler
{
services.TryAddEnumerable(ServiceDescriptor.Singleton, EnsureSignInScheme>());
return services.AddScheme(authenticationScheme, displayName, configureScheme: null, configureOptions: configureOptions);
}
// Used to ensure that there's always a default data protection provider
private class EnsureSignInScheme : IInitializeOptions where TOptions : RemoteAuthenticationOptions
{
private readonly AuthenticationOptions _authOptions;
public EnsureSignInScheme(IOptions authOptions)
{
_authOptions = authOptions.Value;
}
public void Initialize(string name, TOptions options)
{
options.SignInScheme = options.SignInScheme ?? _authOptions.DefaultSignInScheme;
}
}
}
}