// 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 Microsoft.AspNet.Antiforgery; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.OptionsModel; namespace Microsoft.Extensions.DependencyInjection { public static class ServiceCollectionExtensions { public static IServiceCollection AddAntiforgery(this IServiceCollection services) { if (services == null) { throw new ArgumentNullException(nameof(services)); } services.AddDataProtection(); // Don't overwrite any options setups that a user may have added. services.TryAddEnumerable( ServiceDescriptor.Transient, AntiforgeryOptionsSetup>()); services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddScoped(); services.TryAddSingleton(); return services; } public static IServiceCollection ConfigureAntiforgery( this IServiceCollection services, Action setupAction) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (setupAction == null) { throw new ArgumentNullException(nameof(setupAction)); } services.Configure(setupAction); return services; } } }