// 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);
}
}