// 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 System.IdentityModel.Tokens.Jwt; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.Service; using Microsoft.AspNetCore.Identity.Service.Claims; using Microsoft.AspNetCore.Identity.Service.Configuration; using Microsoft.AspNetCore.Identity.Service.Core; using Microsoft.AspNetCore.Identity.Service.Core.Claims; using Microsoft.AspNetCore.Identity.Service.Metadata; using Microsoft.AspNetCore.Identity.Service.Serialization; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; namespace Microsoft.Extensions.DependencyInjection { public static class IdentityServiceServiceCollectionExtensions { public static IIdentityServiceBuilder AddApplications( this IdentityBuilder builder, Action configure) where TUser : class where TApplication : class { if (builder == null) { throw new NullReferenceException(nameof(builder)); } if (configure == null) { throw new NullReferenceException(nameof(configure)); } var services = builder.Services; services.AddOptions(); services.AddWebEncoders(); services.AddDataProtection(); services.AddAuthentication(); services.TryAdd(CreateServices()); // Configuration services.AddTransient, IdentityServiceOptionsDefaultSetup>(); services.AddTransient, IdentityServiceOptionsSetup>(); services.AddAuthentication().AddCookie(IdentityServiceOptions.CookieAuthenticationScheme, options => { options.Cookie.HttpOnly = true; options.Cookie.SecurePolicy = CookieSecurePolicy.Always; options.Cookie.Path = "/tfp"; options.Cookie.Name = IdentityServiceOptions.AuthenticationCookieName; }); services.ConfigureApplicationCookie(options => { options.LoginPath = "/tfp/Identity/Account/Login"; options.AccessDeniedPath = "/tfp/Identity/Account/Denied"; options.Cookie.Path = "/tfp"; }); services.ConfigureExternalCookie(options => options.Cookie.Path = "/tfp"); services.Configure(IdentityConstants.TwoFactorRememberMeScheme, options => options.Cookie.Path = "/tfp"); services.Configure(IdentityConstants.TwoFactorUserIdScheme, options => options.Cookie.Path = "/tfp"); services.AddTransient, IdentityServiceAuthorizationOptionsSetup>(); // Other stuff services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton, PasswordHasher>(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton, ApplicationValidator>(); services.AddSingleton(); // Session services.AddTransient>(); services.AddTransient>(); services.AddTransient>(); services.AddHttpContextAccessor(); services.Configure(configure); return new IdentityServiceBuilder(builder); } private static IEnumerable CreateServices() where TUser : class where TApplication : class { yield return ServiceDescriptor.Scoped, ApplicationManager>(); // Protocol services yield return ServiceDescriptor.Transient(); yield return ServiceDescriptor.Transient(); yield return ServiceDescriptor.Transient(); yield return ServiceDescriptor.Transient(); yield return ServiceDescriptor.Transient(); yield return ServiceDescriptor.Transient(); // Infrastructure services yield return ServiceDescriptor.Singleton(); yield return ServiceDescriptor.Transient(); yield return ServiceDescriptor.Singleton(); yield return ServiceDescriptor.Singleton(); yield return ServiceDescriptor.Singleton(); yield return ServiceDescriptor.Transient, SecureDataFormat>(); yield return ServiceDescriptor.Transient, SecureDataFormat>(); yield return ServiceDescriptor.Singleton(sp => sp.GetDataProtectionProvider().CreateProtector("IdentityProvider")); yield return ServiceDescriptor.Transient(); yield return ServiceDescriptor.Transient, TokenDataSerializer>(); yield return ServiceDescriptor.Transient, TokenDataSerializer>(); yield return ServiceDescriptor.Transient, ApplicationClaimsPrincipalFactory>(); // Validation yield return ServiceDescriptor.Transient(); yield return ServiceDescriptor.Transient(); yield return ServiceDescriptor.Transient(); yield return ServiceDescriptor.Transient>(); yield return ServiceDescriptor.Transient>(); // Metadata yield return ServiceDescriptor.Singleton(); yield return ServiceDescriptor.Singleton(); } } }