From 85530742ef456a9da08acbfd66dadb47d5bb4047 Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Wed, 12 Nov 2014 15:49:54 -0800 Subject: [PATCH] Replace AddDefaultIdentity Break it into AddIdentity<>.AddEntityFrameworkStores.AddDefaultTokenProviders() --- samples/IdentitySample.Mvc/Startup.cs | 6 +- ...dentityEntityFrameworkBuilderExtensions.cs | 34 ++-------- ...ityFrameworkServiceCollectionExtensions.cs | 66 ------------------- .../IdentityEntityFrameworkServices.cs | 49 ++++++++++++++ .../IdentityBuilder.cs | 39 +++++++++-- .../IdentityServiceCollectionExtensions.cs | 64 ++++++------------ .../IdentityServices.cs | 6 +- ...InMemoryTestServiceCollectionExtensions.cs | 3 +- .../TestIdentityFactory.cs | 2 +- .../DefaultPocoTest.cs | 2 +- .../SqlStoreTestBase.cs | 7 +- .../UserStoreTest.cs | 6 +- .../IdentityBuilderTest.cs | 9 ++- .../IdentityOptionsTest.cs | 21 ++++-- test/Shared/UserManagerTestBase.cs | 3 +- 15 files changed, 145 insertions(+), 172 deletions(-) delete mode 100644 src/Microsoft.AspNet.Identity.EntityFramework/IdentityEntityFrameworkServiceCollectionExtensions.cs create mode 100644 src/Microsoft.AspNet.Identity.EntityFramework/IdentityEntityFrameworkServices.cs diff --git a/samples/IdentitySample.Mvc/Startup.cs b/samples/IdentitySample.Mvc/Startup.cs index c797c10071..80be907d58 100644 --- a/samples/IdentitySample.Mvc/Startup.cs +++ b/samples/IdentitySample.Mvc/Startup.cs @@ -36,10 +36,12 @@ namespace IdentitySamples options.DefaultAdminPassword = Configuration.Get("DefaultAdminPassword"); }); - services.AddDefaultIdentity(Configuration.GetSubKey("Identity"), options => + services.AddIdentity(Configuration, options => { options.SecurityStampValidationInterval = TimeSpan.FromMinutes(20); - }); + }) + .AddEntityFrameworkStores() + .AddDefaultTokenProviders(); services.ConfigureFacebookAuthentication(options => { diff --git a/src/Microsoft.AspNet.Identity.EntityFramework/IdentityEntityFrameworkBuilderExtensions.cs b/src/Microsoft.AspNet.Identity.EntityFramework/IdentityEntityFrameworkBuilderExtensions.cs index 3290097270..787ce65d85 100644 --- a/src/Microsoft.AspNet.Identity.EntityFramework/IdentityEntityFrameworkBuilderExtensions.cs +++ b/src/Microsoft.AspNet.Identity.EntityFramework/IdentityEntityFrameworkBuilderExtensions.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Microsoft.AspNet.Identity; -using Microsoft.AspNet.Identity.EntityFramework; using Microsoft.Data.Entity; using System; @@ -10,43 +9,18 @@ namespace Microsoft.Framework.DependencyInjection { public static class IdentityEntityFrameworkBuilderExtensions { - public static IdentityBuilder AddEntityFramework(this IdentityBuilder builder) - { - return builder.AddEntityFramework(); - } - - public static IdentityBuilder AddEntityFramework(this IdentityBuilder builder) + public static IdentityBuilder AddEntityFrameworkStores(this IdentityBuilder builder) where TContext : DbContext { - return builder.AddEntityFramework(); - } - - public static IdentityBuilder AddEntityFramework(this IdentityBuilder builder) - where TUser : IdentityUser, new() - where TContext : DbContext - { - return builder.AddEntityFramework(); - } - - public static IdentityBuilder AddEntityFramework(this IdentityBuilder builder) - where TUser : IdentityUser, new() - where TRole : IdentityRole, new() - where TContext : DbContext - { - builder.Services.AddScoped, UserStore>(); - builder.Services.AddScoped, RoleStore>(); + builder.Services.Add(IdentityEntityFrameworkServices.GetDefaultServices(builder.UserType, builder.RoleType, typeof(TContext))); return builder; } - public static IdentityBuilder AddEntityFramework(this IdentityBuilder builder) - where TUser : IdentityUser, new() - where TRole : IdentityRole, new() + public static IdentityBuilder AddEntityFrameworkStores(this IdentityBuilder builder) where TContext : DbContext where TKey : IEquatable { - builder.Services.AddScoped, UserStore>(); - builder.Services.AddScoped, RoleStore>(); - builder.Services.AddScoped(); + builder.Services.Add(IdentityEntityFrameworkServices.GetDefaultServices(builder.UserType, builder.RoleType, typeof(TContext), typeof(TKey))); return builder; } } diff --git a/src/Microsoft.AspNet.Identity.EntityFramework/IdentityEntityFrameworkServiceCollectionExtensions.cs b/src/Microsoft.AspNet.Identity.EntityFramework/IdentityEntityFrameworkServiceCollectionExtensions.cs deleted file mode 100644 index fe15737b48..0000000000 --- a/src/Microsoft.AspNet.Identity.EntityFramework/IdentityEntityFrameworkServiceCollectionExtensions.cs +++ /dev/null @@ -1,66 +0,0 @@ -// 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.Identity; -using Microsoft.AspNet.Identity.EntityFramework; -using Microsoft.Data.Entity; -using Microsoft.Framework.ConfigurationModel; - -namespace Microsoft.Framework.DependencyInjection -{ - public static class IdentityEntityFrameworkServiceCollectionExtensions - { - // MOVE to builder extension - public static IdentityBuilder AddIdentityEntityFramework(this IServiceCollection services) - { - return services.AddIdentityEntityFramework(); - } - - public static IdentityBuilder AddIdentityEntityFramework(this IServiceCollection services) - where TContext : DbContext - { - return services.AddIdentityEntityFramework(); - } - - public static IdentityBuilder AddDefaultIdentity(this IServiceCollection services, IConfiguration config = null, - Action configureOptions = null) - where TUser : IdentityUser, new() - where TRole : IdentityRole, new() - where TContext : DbContext - { - return services.AddDefaultIdentity(config, configureOptions) - .AddEntityFramework(); - } - - public static IdentityBuilder AddIdentityEntityFramework(this IServiceCollection services, Action configureOptions = null) - where TUser : IdentityUser, new() - where TContext : DbContext - { - return services.AddIdentityEntityFramework(null, configureOptions); - } - - public static IdentityBuilder AddIdentityEntityFramework(this IServiceCollection services, IConfiguration config = null, Action configureOptions = null) - where TUser : IdentityUser, new() - where TRole : IdentityRole, new() - where TContext : DbContext - { - var builder = services.AddIdentity(config, configureOptions); - services.AddScoped, UserStore>(); - services.AddScoped, RoleStore>(); - return builder; - } - - public static IdentityBuilder AddIdentityEntityFramework(this IServiceCollection services, IConfiguration config = null, Action configureOptions = null) - where TUser : IdentityUser, new() - where TRole : IdentityRole, new() - where TContext : DbContext - where TKey : IEquatable - { - var builder = services.AddIdentity(config, configureOptions); - services.AddScoped, UserStore>(); - services.AddScoped, RoleStore>(); - return builder; - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Identity.EntityFramework/IdentityEntityFrameworkServices.cs b/src/Microsoft.AspNet.Identity.EntityFramework/IdentityEntityFrameworkServices.cs new file mode 100644 index 0000000000..c01ab1c1cb --- /dev/null +++ b/src/Microsoft.AspNet.Identity.EntityFramework/IdentityEntityFrameworkServices.cs @@ -0,0 +1,49 @@ +// 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 System.Collections.Generic; +using Microsoft.Framework.ConfigurationModel; +using Microsoft.Framework.DependencyInjection; +using Microsoft.AspNet.Identity.EntityFramework; + +namespace Microsoft.AspNet.Identity +{ + /// + /// Default services + /// + public class IdentityEntityFrameworkServices + { + public static IEnumerable GetDefaultServices(Type userType, Type roleType, Type contextType, Type keyType = null, IConfiguration config = null) + { + ServiceDescriber describe; + if (config == null) + { + describe = new ServiceDescriber(); + } + else + { + describe = new ServiceDescriber(config); + } + Type userStoreType; + Type roleStoreType; + if (keyType != null) + { + userStoreType = typeof(UserStore<,,,>).MakeGenericType(userType, roleType, contextType, keyType); + roleStoreType = typeof(RoleStore<,,>).MakeGenericType(roleType, contextType, keyType); + } + else + { + userStoreType = typeof(UserStore<,,>).MakeGenericType(userType, roleType, contextType); + roleStoreType = typeof(RoleStore<,>).MakeGenericType(roleType, contextType); + } + + yield return describe.Scoped( + typeof(IUserStore<>).MakeGenericType(userType), + userStoreType); + yield return describe.Scoped( + typeof(IRoleStore<>).MakeGenericType(roleType), + roleStoreType); + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Identity/IdentityBuilder.cs b/src/Microsoft.AspNet.Identity/IdentityBuilder.cs index 3368f3b6d0..750550ada0 100644 --- a/src/Microsoft.AspNet.Identity/IdentityBuilder.cs +++ b/src/Microsoft.AspNet.Identity/IdentityBuilder.cs @@ -3,19 +3,46 @@ using System; using Microsoft.Framework.DependencyInjection; -using Microsoft.Framework.OptionsModel; namespace Microsoft.AspNet.Identity { - public class IdentityBuilder where TUser : class where TRole : class + public class IdentityBuilder { - public IServiceCollection Services { get; private set; } - - public IdentityBuilder(IServiceCollection services) + public IdentityBuilder(Type user, Type role, IServiceCollection services) { + UserType = user; + RoleType = role; Services = services; } + public Type UserType { get; private set; } + public Type RoleType { get; private set; } + public IServiceCollection Services { get; private set; } + + public IdentityBuilder AddTokenProvider(Type provider) + { + Services.AddScoped(typeof(IUserTokenProvider<>).MakeGenericType(UserType), provider); + return this; + } + + public IdentityBuilder AddDefaultTokenProviders() + { + Services.Configure(options => + { + options.Name = Resources.DefaultTokenProvider; + }); + + return AddTokenProvider(typeof(DataProtectorTokenProvider<>).MakeGenericType(UserType)) + .AddTokenProvider(typeof(PhoneNumberTokenProvider<>).MakeGenericType(UserType)) + .AddTokenProvider(typeof(EmailTokenProvider<>).MakeGenericType(UserType)); + } + + } + + public class IdentityBuilder : IdentityBuilder where TUser : class where TRole : class + { + public IdentityBuilder(IServiceCollection services) : base(typeof(TUser), typeof(TRole), services) { } + public IdentityBuilder AddInstance(TService instance) where TService : class { @@ -43,7 +70,7 @@ namespace Microsoft.AspNet.Identity return AddInstance(validator); } - public IdentityBuilder AddTokenProvider() where TTokenProvider : class, IUserTokenProvider + public IdentityBuilder AddTokenProvider() where TTokenProvider : IUserTokenProvider { Services.AddScoped, TTokenProvider>(); return this; diff --git a/src/Microsoft.AspNet.Identity/IdentityServiceCollectionExtensions.cs b/src/Microsoft.AspNet.Identity/IdentityServiceCollectionExtensions.cs index 60fdc61754..3be053cec8 100644 --- a/src/Microsoft.AspNet.Identity/IdentityServiceCollectionExtensions.cs +++ b/src/Microsoft.AspNet.Identity/IdentityServiceCollectionExtensions.cs @@ -6,7 +6,6 @@ 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 @@ -18,24 +17,36 @@ namespace Microsoft.Framework.DependencyInjection 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) + 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 { + services.Add(IdentityServices.GetDefaultServices()); + if (identityConfig != null) { + if (useDefaultSubKey) + { + identityConfig = identityConfig.GetSubKey("identity"); + } services.Configure(identityConfig); } if (configureOptions != null) @@ -43,13 +54,6 @@ namespace Microsoft.Framework.DependencyInjection 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; @@ -58,7 +62,6 @@ namespace Microsoft.Framework.DependencyInjection services.Configure(options => { options.AuthenticationType = IdentityOptions.ApplicationCookieAuthenticationType; - //CookieName = ".AspNet.Identity." + ClaimsIdentityOptions.DefaultAuthenticationType, options.LoginPath = new PathString("/Account/Login"); options.Notifications = new CookieAuthenticationNotifications { @@ -91,32 +94,5 @@ namespace Microsoft.Framework.DependencyInjection 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); - } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Identity/IdentityServices.cs b/src/Microsoft.AspNet.Identity/IdentityServices.cs index d632a9aba2..850c14693d 100644 --- a/src/Microsoft.AspNet.Identity/IdentityServices.cs +++ b/src/Microsoft.AspNet.Identity/IdentityServices.cs @@ -1,7 +1,6 @@ // 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 System.Collections.Generic; using Microsoft.Framework.ConfigurationModel; using Microsoft.Framework.DependencyInjection; @@ -30,6 +29,11 @@ namespace Microsoft.AspNet.Identity yield return describe.Transient, PasswordHasher>(); yield return describe.Transient(); yield return describe.Transient, RoleValidator>(); + yield return describe.Scoped>(); + yield return describe.Scoped, ClaimsIdentityFactory>(); + yield return describe.Scoped, UserManager>(); + yield return describe.Scoped, SignInManager>(); + yield return describe.Scoped, RoleManager>(); } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/EntityInMemoryTestServiceCollectionExtensions.cs b/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/EntityInMemoryTestServiceCollectionExtensions.cs index 4ab71973bd..6d21889873 100644 --- a/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/EntityInMemoryTestServiceCollectionExtensions.cs +++ b/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/EntityInMemoryTestServiceCollectionExtensions.cs @@ -20,7 +20,8 @@ namespace Microsoft.AspNet.Identity where TRole : IdentityRole where TDbContext : DbContext { - var builder = services.AddDefaultIdentity(); + var builder = services.AddIdentity(); + builder.AddDefaultTokenProviders(); services.AddInstance>(new InMemoryUserStore(context)); var store = new RoleStore(context); services.AddInstance>(store); diff --git a/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/TestIdentityFactory.cs b/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/TestIdentityFactory.cs index 1af3c89a81..50e7d085b4 100644 --- a/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/TestIdentityFactory.cs +++ b/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/TestIdentityFactory.cs @@ -25,7 +25,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.InMemory.Test public static RoleManager CreateRoleManager(InMemoryContext context) { var services = new ServiceCollection(); - services.AddDefaultIdentity().AddRoleStore(new RoleStore(context)); + services.AddIdentity().AddRoleStore(new RoleStore(context)); return services.BuildServiceProvider().GetRequiredService>(); } diff --git a/test/Microsoft.AspNet.Identity.EntityFramework.Test/DefaultPocoTest.cs b/test/Microsoft.AspNet.Identity.EntityFramework.Test/DefaultPocoTest.cs index 1889b06d1a..475d4133b1 100644 --- a/test/Microsoft.AspNet.Identity.EntityFramework.Test/DefaultPocoTest.cs +++ b/test/Microsoft.AspNet.Identity.EntityFramework.Test/DefaultPocoTest.cs @@ -49,8 +49,8 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test builder.UseServices(services => { DbUtil.ConfigureDbServices(ConnectionString, services); - services.AddDefaultIdentity(); services.Add(DataProtectionServices.GetDefaultServices()); + services.AddIdentity().AddEntityFrameworkStores(); }); var userStore = builder.ApplicationServices.GetRequiredService>(); diff --git a/test/Microsoft.AspNet.Identity.EntityFramework.Test/SqlStoreTestBase.cs b/test/Microsoft.AspNet.Identity.EntityFramework.Test/SqlStoreTestBase.cs index 345e80ea62..a5d0f07889 100644 --- a/test/Microsoft.AspNet.Identity.EntityFramework.Test/SqlStoreTestBase.cs +++ b/test/Microsoft.AspNet.Identity.EntityFramework.Test/SqlStoreTestBase.cs @@ -85,7 +85,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test builder.UseServices(services => { DbUtil.ConfigureDbServices(ConnectionString, services); - services.AddIdentityEntityFramework(); + services.AddIdentity().AddEntityFrameworkStores(); }); var userStore = builder.ApplicationServices.GetRequiredService>(); @@ -110,8 +110,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test builder.UseServices(services => { DbUtil.ConfigureDbServices(ConnectionString, services); - services.AddIdentityEntityFramework(); - services.ConfigureIdentity(options => + services.AddIdentity(null, options => { options.Password.RequiredLength = 1; options.Password.RequireLowercase = false; @@ -119,7 +118,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test options.Password.RequireUppercase = false; options.Password.RequireDigit = false; options.User.UserNameValidationRegex = null; - }); + }).AddEntityFrameworkStores(); }); var userStore = builder.ApplicationServices.GetRequiredService>(); diff --git a/test/Microsoft.AspNet.Identity.EntityFramework.Test/UserStoreTest.cs b/test/Microsoft.AspNet.Identity.EntityFramework.Test/UserStoreTest.cs index 178fcfb9fd..86ba3eafc9 100644 --- a/test/Microsoft.AspNet.Identity.EntityFramework.Test/UserStoreTest.cs +++ b/test/Microsoft.AspNet.Identity.EntityFramework.Test/UserStoreTest.cs @@ -54,8 +54,8 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test builder.UseServices(services => { DbUtil.ConfigureDbServices(ConnectionString, services); - services.AddDefaultIdentity(); services.Add(DataProtectionServices.GetDefaultServices()); + services.AddIdentity().AddEntityFrameworkStores(); }); var userStore = builder.ApplicationServices.GetRequiredService>(); @@ -84,14 +84,14 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test services.AddEntityFramework() .AddSqlServer() .AddDbContext(options => options.UseSqlServer(ConnectionString)); - services.AddIdentityEntityFramework(options => + services.AddIdentity(null, options => { options.Password.RequiredLength = 1; options.Password.RequireLowercase = false; options.Password.RequireNonLetterOrDigit = false; options.Password.RequireUppercase = false; options.Password.RequireDigit = false; - }); + }).AddEntityFrameworkStores(); }); var userStore = builder.ApplicationServices.GetRequiredService>(); diff --git a/test/Microsoft.AspNet.Identity.Test/IdentityBuilderTest.cs b/test/Microsoft.AspNet.Identity.Test/IdentityBuilderTest.cs index c04b73efd6..1d29a5cc14 100644 --- a/test/Microsoft.AspNet.Identity.Test/IdentityBuilderTest.cs +++ b/test/Microsoft.AspNet.Identity.Test/IdentityBuilderTest.cs @@ -1,7 +1,6 @@ // 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.Security.Claims; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection.Fallback; using Microsoft.Framework.OptionsModel; @@ -16,7 +15,7 @@ namespace Microsoft.AspNet.Identity.Test { var services = new ServiceCollection(); var validator = new UserValidator(); - services.AddIdentity().AddUserValidator(validator); + services.AddIdentity().AddUserValidator(validator); Assert.Equal(validator, services.BuildServiceProvider().GetRequiredService>()); } @@ -25,7 +24,7 @@ namespace Microsoft.AspNet.Identity.Test { var services = new ServiceCollection(); var validator = new PasswordValidator(); - services.AddIdentity().AddPasswordValidator(validator); + services.AddIdentity().AddPasswordValidator(validator); Assert.Equal(validator, services.BuildServiceProvider().GetRequiredService>()); } @@ -39,7 +38,7 @@ namespace Microsoft.AspNet.Identity.Test public void EnsureDefaultServices() { var services = new ServiceCollection(); - services.AddIdentity(); + services.AddIdentity(); services.Add(OptionsServices.GetDefaultServices()); var provider = services.BuildServiceProvider(); @@ -57,7 +56,7 @@ namespace Microsoft.AspNet.Identity.Test where TService : class { var services = new ServiceCollection(); - services.AddIdentity().AddInstance(instance); + services.AddIdentity().AddInstance(instance); Assert.Equal(instance, services.BuildServiceProvider().GetRequiredService()); } diff --git a/test/Microsoft.AspNet.Identity.Test/IdentityOptionsTest.cs b/test/Microsoft.AspNet.Identity.Test/IdentityOptionsTest.cs index fe4891f78b..a5fa3162f0 100644 --- a/test/Microsoft.AspNet.Identity.Test/IdentityOptionsTest.cs +++ b/test/Microsoft.AspNet.Identity.Test/IdentityOptionsTest.cs @@ -39,8 +39,10 @@ namespace Microsoft.AspNet.Identity.Test Assert.Equal(ClaimsIdentityOptions.DefaultSecurityStampClaimType, options.ClaimsIdentity.SecurityStampClaimType); } - [Fact] - public void IdentityOptionsFromConfig() + [Theory] + [InlineData(true)] + [InlineData(false)] + public void IdentityOptionsFromConfig(bool useDefaultSubKey) { const string roleClaimType = "rolez"; const string usernameClaimType = "namez"; @@ -66,7 +68,14 @@ namespace Microsoft.AspNet.Identity.Test Assert.Equal(roleClaimType, config.Get("identity:claimsidentity:roleclaimtype")); var services = new ServiceCollection {OptionsServices.GetDefaultServices()}; - services.AddIdentity(config.GetSubKey("identity")); + if (useDefaultSubKey) + { + services.AddIdentity(config); + } + else + { + services.AddIdentity(config.GetSubKey("identity"), null, useDefaultSubKey); + } var accessor = services.BuildServiceProvider().GetRequiredService>(); Assert.NotNull(accessor); var options = accessor.Options; @@ -95,7 +104,7 @@ namespace Microsoft.AspNet.Identity.Test }; var config = new Configuration { new MemoryConfigurationSource(dic) }; var services = new ServiceCollection { OptionsServices.GetDefaultServices() }; - services.AddIdentity(config.GetSubKey("identity"), + services.AddIdentity(config, o => { o.User.RequireUniqueEmail = false; o.Lockout.MaxFailedAccessAttempts++; }); var accessor = services.BuildServiceProvider().GetRequiredService>(); Assert.NotNull(accessor); @@ -117,7 +126,7 @@ namespace Microsoft.AspNet.Identity.Test var builder = new ApplicationBuilder(CallContextServiceLocator.Locator.ServiceProvider); builder.UseServices(services => { - services.AddIdentity(); + services.AddIdentity(); services.ConfigureOptions(); }); @@ -139,7 +148,7 @@ namespace Microsoft.AspNet.Identity.Test var app = new ApplicationBuilder(CallContextServiceLocator.Locator.ServiceProvider); app.UseServices(services => { - services.AddIdentity().ConfigureIdentity(options => options.User.RequireUniqueEmail = true); + services.ConfigureIdentity(options => options.User.RequireUniqueEmail = true); }); var optionsGetter = app.ApplicationServices.GetRequiredService>(); diff --git a/test/Shared/UserManagerTestBase.cs b/test/Shared/UserManagerTestBase.cs index 29b878daa4..6d646ec0c8 100644 --- a/test/Shared/UserManagerTestBase.cs +++ b/test/Shared/UserManagerTestBase.cs @@ -33,7 +33,7 @@ namespace Microsoft.AspNet.Identity.Test services.Add(OptionsServices.GetDefaultServices()); services.Add(HostingServices.GetDefaultServices()); services.Add(DataProtectionServices.GetDefaultServices()); - services.AddDefaultIdentity(); + services.AddIdentity().AddDefaultTokenProviders(); AddUserStore(services, context); AddRoleStore(services, context); services.ConfigureIdentity(options => @@ -44,7 +44,6 @@ namespace Microsoft.AspNet.Identity.Test options.Password.RequireUppercase = false; options.User.UserNameValidationRegex = null; }); - } protected virtual UserManager CreateManager(object context = null, IServiceCollection services = null)