// Copyright (c) Microsoft Open Technologies, Inc. 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.ConfigurationModel; namespace Microsoft.Framework.DependencyInjection { public static class IdentityServiceCollectionExtensions { public static IServiceCollection ConfigureIdentity(this IServiceCollection services, Action configure) { return services.Configure(configure); } public static IdentityBuilder AddIdentity(this IServiceCollection services) { return services.AddIdentity(); } public static IdentityBuilder AddIdentity( this IServiceCollection services, IConfiguration identityConfig = null, Action configureOptions = null, bool useDefaultSubKey = true) { return services.AddIdentity(identityConfig, configureOptions, useDefaultSubKey); } public static IdentityBuilder AddIdentity( this IServiceCollection services, IConfiguration identityConfig = null, Action configureOptions = null, bool useDefaultSubKey = true) where TUser : class where TRole : class { if (identityConfig != null) { if (useDefaultSubKey) { identityConfig = identityConfig.GetSubKey("identity"); } services.Configure(identityConfig); } // Services used by identity services.AddOptions(); services.AddDataProtection(); services.AddLogging(); services.TryAdd(ServiceDescriptor.Singleton()); // Identity services services.TryAdd(ServiceDescriptor.Transient, UserValidator>()); services.TryAdd(ServiceDescriptor.Transient, PasswordValidator>()); services.TryAdd(ServiceDescriptor.Transient, PasswordHasher>()); services.TryAdd(ServiceDescriptor.Transient()); services.TryAdd(ServiceDescriptor.Transient, RoleValidator>()); // No interface for the error describer so we can add errors without rev'ing the interface services.TryAdd(ServiceDescriptor.Transient()); 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 (configureOptions != null) { services.ConfigureIdentity(configureOptions); } services.Configure(options => { options.SignInScheme = IdentityOptions.ExternalCookieAuthenticationScheme; }); // Configure all of the cookie middlewares services.Configure(options => { options.AuthenticationScheme = IdentityOptions.ApplicationCookieAuthenticationScheme; options.LoginPath = new PathString("/Account/Login"); options.Notifications = new CookieAuthenticationNotifications { OnValidatePrincipal = SecurityStampValidator.ValidatePrincipalAsync }; }, IdentityOptions.ApplicationCookieAuthenticationScheme); services.Configure(options => { options.AuthenticationScheme = IdentityOptions.ExternalCookieAuthenticationScheme; options.AutomaticAuthentication = false; options.CookieName = IdentityOptions.ExternalCookieAuthenticationScheme; options.ExpireTimeSpan = TimeSpan.FromMinutes(5); }, IdentityOptions.ExternalCookieAuthenticationScheme); services.Configure(options => { options.AuthenticationScheme = IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme; options.AutomaticAuthentication = false; options.CookieName = IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme; }, IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme); services.Configure(options => { options.AuthenticationScheme = IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme; options.AutomaticAuthentication = false; options.CookieName = IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme; options.ExpireTimeSpan = TimeSpan.FromMinutes(5); }, IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme); return new IdentityBuilder(typeof(TUser), typeof(TRole), services); } } }