// 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.Http; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Security; using Microsoft.AspNet.Security.Cookies; using Microsoft.AspNet.Security.DataProtection; 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, IConfiguration identityConfig = null, Action configureOptions = null) { return services.AddIdentity(identityConfig, configureOptions); } public static IdentityBuilder AddIdentity(this IServiceCollection services) { return services.AddIdentity(); } public static IdentityBuilder AddIdentity(this IServiceCollection services, IConfiguration identityConfig = null, Action configureOptions = null) where TUser : class where TRole : class { if (identityConfig != null) { services.Configure(identityConfig); } if (configureOptions != null) { services.ConfigureIdentity(configureOptions); } services.Add(IdentityServices.GetDefaultServices(identityConfig)); services.AddScoped>(); services.AddScoped>(); services.AddScoped>(); services.AddScoped>(); services.AddScoped, ClaimsIdentityFactory>(); services.Configure(options => { options.SignInAsAuthenticationType = IdentityOptions.ExternalCookieAuthenticationType; }); services.Configure(options => { options.AuthenticationType = IdentityOptions.ApplicationCookieAuthenticationType; //CookieName = ".AspNet.Identity." + ClaimsIdentityOptions.DefaultAuthenticationType, options.LoginPath = new PathString("/Account/Login"); options.Notifications = new CookieAuthenticationNotifications { OnValidateIdentity = SecurityStampValidator.ValidateIdentityAsync }; }, IdentityOptions.ApplicationCookieAuthenticationType); services.Configure(options => { options.AuthenticationType = IdentityOptions.ExternalCookieAuthenticationType; options.AuthenticationMode = AuthenticationMode.Passive; options.CookieName = IdentityOptions.ExternalCookieAuthenticationType; options.ExpireTimeSpan = TimeSpan.FromMinutes(5); }, IdentityOptions.ExternalCookieAuthenticationType); services.Configure(options => { options.AuthenticationType = IdentityOptions.TwoFactorRememberMeCookieAuthenticationType; options.AuthenticationMode = AuthenticationMode.Passive; options.CookieName = IdentityOptions.TwoFactorRememberMeCookieAuthenticationType; }, IdentityOptions.TwoFactorRememberMeCookieAuthenticationType); services.Configure(options => { options.AuthenticationType = IdentityOptions.TwoFactorUserIdCookieAuthenticationType; options.AuthenticationMode = AuthenticationMode.Passive; options.CookieName = IdentityOptions.TwoFactorUserIdCookieAuthenticationType; options.ExpireTimeSpan = TimeSpan.FromMinutes(5); }, IdentityOptions.TwoFactorUserIdCookieAuthenticationType); return new IdentityBuilder(services); } public static IdentityBuilder AddDefaultIdentity(this IServiceCollection services, IConfiguration config = null, Action configureOptions = null) where TUser : class where TRole : class { services.Configure(options => { options.Name = Resources.DefaultTokenProvider; }); return services.AddIdentity(config) .AddTokenProvider>() .AddTokenProvider>() .AddTokenProvider>(); } public static IdentityBuilder AddIdentity(this IServiceCollection services) where TUser : class { return services.AddIdentity(); } public static IdentityBuilder AddIdentity(this IServiceCollection services, IConfiguration identityConfig) where TUser : class { return services.AddIdentity(identityConfig); } } }