// 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 IServiceCollection ConfigureIdentity(this IServiceCollection services, IConfiguration config) { return services.Configure(config); } public static IServiceCollection ConfigureIdentityApplicationCookie(this IServiceCollection services, Action configureOptions) { return services.Configure(configureOptions, IdentityOptions.ApplicationCookieAuthenticationScheme); } public static IdentityBuilder AddIdentity(this IServiceCollection services) { return services.AddIdentity(configureOptions: null); } public static IdentityBuilder AddIdentity( this IServiceCollection services) where TUser : class where TRole : class { return services.AddIdentity(configureOptions: null); } public static IdentityBuilder AddIdentity( this IServiceCollection services, Action configureOptions) where TUser : class where TRole : class { // 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.ConfigureIdentityApplicationCookie(options => { options.AuthenticationScheme = IdentityOptions.ApplicationCookieAuthenticationScheme; options.AutomaticAuthentication = true; options.LoginPath = new PathString("/Account/Login"); options.Notifications = new CookieAuthenticationNotifications { OnValidatePrincipal = SecurityStampValidator.ValidatePrincipalAsync }; }); 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); } } }