// 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.Hosting; using Microsoft.AspNet.Http; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Authentication; using Microsoft.AspNet.Authentication.Cookies; using Microsoft.Framework.Configuration; using Microsoft.Framework.DependencyInjection.Extensions; namespace Microsoft.Framework.DependencyInjection { /// /// Contains extension methods to for configuring identity services. /// public static class IdentityServiceCollectionExtensions { /// /// Configures a set of for the application /// /// The services available in the application. /// An action to configure the . /// The instance this method extends. public static IServiceCollection ConfigureIdentity(this IServiceCollection services, Action setupAction) { return services.Configure(setupAction); } /// /// Configures a set of for the application /// /// The services available in the application. /// The configuration for the . /// The instance this method extends. public static IServiceCollection ConfigureIdentity(this IServiceCollection services, IConfiguration config) { return services.Configure(config); } /// /// Configures a set of for the application /// /// The services available in the application. /// An action to configure the . /// The instance this method extends. public static IServiceCollection ConfigureIdentityApplicationCookie(this IServiceCollection services, Action setupAction) { return services.ConfigureCookieAuthentication(setupAction, IdentityOptions.ApplicationCookieAuthenticationScheme); } /// /// Adds the default identity system configuration for the specified User and Role types. /// /// The type representing a User in the system. /// The type representing a Role in the system. /// The services available in the application. /// An for creating and configuring the identity system. public static IdentityBuilder AddIdentity( this IServiceCollection services) where TUser : class where TRole : class { return services.AddIdentity(setupAction: null); } /// /// Adds and configures the identity system for the specified User and Role types. /// /// The type representing a User in the system. /// The type representing a Role in the system. /// The services available in the application. /// An action to configure the . /// An for creating and configuring the identity system. public static IdentityBuilder AddIdentity( this IServiceCollection services, Action setupAction) where TUser : class where TRole : class { // Services used by identity services.AddOptions(); services.AddAuthentication(); // Identity services services.TryAdd(ServiceDescriptor.Scoped, UserValidator>()); services.TryAdd(ServiceDescriptor.Scoped, PasswordValidator>()); services.TryAdd(ServiceDescriptor.Scoped, PasswordHasher>()); services.TryAdd(ServiceDescriptor.Scoped()); services.TryAdd(ServiceDescriptor.Scoped, RoleValidator>()); // No interface for the error describer so we can add errors without rev'ing the interface services.TryAdd(ServiceDescriptor.Scoped()); services.TryAdd(ServiceDescriptor.Scoped>()); services.TryAdd(ServiceDescriptor.Scoped, UserClaimsPrincipalFactory>()); services.TryAdd(ServiceDescriptor.Scoped, UserManager>()); services.TryAdd(ServiceDescriptor.Scoped, SignInManager>()); services.TryAdd(ServiceDescriptor.Scoped, RoleManager>()); if (setupAction != null) { services.ConfigureIdentity(setupAction); } services.Configure(options => { options.SignInScheme = IdentityOptions.ExternalCookieAuthenticationScheme; }); // Configure all of the cookie middlewares services.ConfigureIdentityApplicationCookie(options => { options.AuthenticationScheme = IdentityOptions.ApplicationCookieAuthenticationScheme; options.AutomaticAuthentication = true; options.LoginPath = new PathString("/Account/Login"); options.Notifications = new CookieAuthenticationNotifications { OnValidatePrincipal = SecurityStampValidator.ValidatePrincipalAsync }; }); services.ConfigureCookieAuthentication(options => { options.AuthenticationScheme = IdentityOptions.ExternalCookieAuthenticationScheme; options.CookieName = IdentityOptions.ExternalCookieAuthenticationScheme; options.ExpireTimeSpan = TimeSpan.FromMinutes(5); }, IdentityOptions.ExternalCookieAuthenticationScheme); services.ConfigureCookieAuthentication(options => { options.AuthenticationScheme = IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme; options.CookieName = IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme; }, IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme); services.ConfigureCookieAuthentication(options => { options.AuthenticationScheme = IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme; options.CookieName = IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme; options.ExpireTimeSpan = TimeSpan.FromMinutes(5); }, IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme); return new IdentityBuilder(typeof(TUser), typeof(TRole), services); } } }