Replace AddDefaultIdentity

Break it into
AddIdentity<>.AddEntityFrameworkStores.AddDefaultTokenProviders()
This commit is contained in:
Hao Kung 2014-11-12 15:49:54 -08:00
parent c84a6b436f
commit 85530742ef
15 changed files with 145 additions and 172 deletions

View File

@ -36,10 +36,12 @@ namespace IdentitySamples
options.DefaultAdminPassword = Configuration.Get("DefaultAdminPassword"); options.DefaultAdminPassword = Configuration.Get("DefaultAdminPassword");
}); });
services.AddDefaultIdentity<ApplicationDbContext, ApplicationUser, IdentityRole>(Configuration.GetSubKey("Identity"), options => services.AddIdentity<ApplicationUser, IdentityRole>(Configuration, options =>
{ {
options.SecurityStampValidationInterval = TimeSpan.FromMinutes(20); options.SecurityStampValidationInterval = TimeSpan.FromMinutes(20);
}); })
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.ConfigureFacebookAuthentication(options => services.ConfigureFacebookAuthentication(options =>
{ {

View File

@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // 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;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Data.Entity; using Microsoft.Data.Entity;
using System; using System;
@ -10,43 +9,18 @@ namespace Microsoft.Framework.DependencyInjection
{ {
public static class IdentityEntityFrameworkBuilderExtensions public static class IdentityEntityFrameworkBuilderExtensions
{ {
public static IdentityBuilder<IdentityUser, IdentityRole> AddEntityFramework(this IdentityBuilder<IdentityUser, IdentityRole> builder) public static IdentityBuilder AddEntityFrameworkStores<TContext>(this IdentityBuilder builder)
{
return builder.AddEntityFramework<IdentityDbContext, IdentityUser, IdentityRole>();
}
public static IdentityBuilder<IdentityUser, IdentityRole> AddEntityFramework<TContext>(this IdentityBuilder<IdentityUser, IdentityRole> builder)
where TContext : DbContext where TContext : DbContext
{ {
return builder.AddEntityFramework<TContext, IdentityUser, IdentityRole>(); builder.Services.Add(IdentityEntityFrameworkServices.GetDefaultServices(builder.UserType, builder.RoleType, typeof(TContext)));
}
public static IdentityBuilder<TUser, IdentityRole> AddEntityFramework<TContext, TUser>(this IdentityBuilder<TUser, IdentityRole> builder)
where TUser : IdentityUser, new()
where TContext : DbContext
{
return builder.AddEntityFramework<TContext, TUser, IdentityRole>();
}
public static IdentityBuilder<TUser, TRole> AddEntityFramework<TContext, TUser, TRole>(this IdentityBuilder<TUser, TRole> builder)
where TUser : IdentityUser, new()
where TRole : IdentityRole, new()
where TContext : DbContext
{
builder.Services.AddScoped<IUserStore<TUser>, UserStore<TUser, TRole, TContext>>();
builder.Services.AddScoped<IRoleStore<TRole>, RoleStore<TRole, TContext>>();
return builder; return builder;
} }
public static IdentityBuilder<TUser, TRole> AddEntityFramework<TContext, TUser, TRole, TKey>(this IdentityBuilder<TUser, TRole> builder) public static IdentityBuilder AddEntityFrameworkStores<TContext, TKey>(this IdentityBuilder builder)
where TUser : IdentityUser<TKey>, new()
where TRole : IdentityRole<TKey>, new()
where TContext : DbContext where TContext : DbContext
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
builder.Services.AddScoped<IUserStore<TUser>, UserStore<TUser, TRole, TContext, TKey>>(); builder.Services.Add(IdentityEntityFrameworkServices.GetDefaultServices(builder.UserType, builder.RoleType, typeof(TContext), typeof(TKey)));
builder.Services.AddScoped<IRoleStore<TRole>, RoleStore<TRole, TContext, TKey>>();
builder.Services.AddScoped<TContext>();
return builder; return builder;
} }
} }

View File

@ -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<IdentityUser, IdentityRole> AddIdentityEntityFramework(this IServiceCollection services)
{
return services.AddIdentityEntityFramework<IdentityDbContext, IdentityUser, IdentityRole>();
}
public static IdentityBuilder<IdentityUser, IdentityRole> AddIdentityEntityFramework<TContext>(this IServiceCollection services)
where TContext : DbContext
{
return services.AddIdentityEntityFramework<TContext, IdentityUser, IdentityRole>();
}
public static IdentityBuilder<TUser, TRole> AddDefaultIdentity<TContext, TUser, TRole>(this IServiceCollection services, IConfiguration config = null,
Action<IdentityOptions> configureOptions = null)
where TUser : IdentityUser, new()
where TRole : IdentityRole, new()
where TContext : DbContext
{
return services.AddDefaultIdentity<TUser, TRole>(config, configureOptions)
.AddEntityFramework<TContext, TUser, TRole>();
}
public static IdentityBuilder<TUser, IdentityRole> AddIdentityEntityFramework<TContext, TUser>(this IServiceCollection services, Action<IdentityOptions> configureOptions = null)
where TUser : IdentityUser, new()
where TContext : DbContext
{
return services.AddIdentityEntityFramework<TContext, TUser, IdentityRole>(null, configureOptions);
}
public static IdentityBuilder<TUser, TRole> AddIdentityEntityFramework<TContext, TUser, TRole>(this IServiceCollection services, IConfiguration config = null, Action<IdentityOptions> configureOptions = null)
where TUser : IdentityUser, new()
where TRole : IdentityRole, new()
where TContext : DbContext
{
var builder = services.AddIdentity<TUser, TRole>(config, configureOptions);
services.AddScoped<IUserStore<TUser>, UserStore<TUser, TRole, TContext>>();
services.AddScoped<IRoleStore<TRole>, RoleStore<TRole, TContext>>();
return builder;
}
public static IdentityBuilder<TUser, TRole> AddIdentityEntityFramework<TContext, TUser, TRole, TKey>(this IServiceCollection services, IConfiguration config = null, Action<IdentityOptions> configureOptions = null)
where TUser : IdentityUser<TKey>, new()
where TRole : IdentityRole<TKey>, new()
where TContext : DbContext
where TKey : IEquatable<TKey>
{
var builder = services.AddIdentity<TUser, TRole>(config, configureOptions);
services.AddScoped<IUserStore<TUser>, UserStore<TUser, TRole, TContext, TKey>>();
services.AddScoped<IRoleStore<TRole>, RoleStore<TRole, TContext, TKey>>();
return builder;
}
}
}

View File

@ -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
{
/// <summary>
/// Default services
/// </summary>
public class IdentityEntityFrameworkServices
{
public static IEnumerable<IServiceDescriptor> 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);
}
}
}

View File

@ -3,19 +3,46 @@
using System; using System;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Identity namespace Microsoft.AspNet.Identity
{ {
public class IdentityBuilder<TUser, TRole> where TUser : class where TRole : class public class IdentityBuilder
{ {
public IServiceCollection Services { get; private set; } public IdentityBuilder(Type user, Type role, IServiceCollection services)
public IdentityBuilder(IServiceCollection services)
{ {
UserType = user;
RoleType = role;
Services = services; 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<DataProtectionTokenProviderOptions>(options =>
{
options.Name = Resources.DefaultTokenProvider;
});
return AddTokenProvider(typeof(DataProtectorTokenProvider<>).MakeGenericType(UserType))
.AddTokenProvider(typeof(PhoneNumberTokenProvider<>).MakeGenericType(UserType))
.AddTokenProvider(typeof(EmailTokenProvider<>).MakeGenericType(UserType));
}
}
public class IdentityBuilder<TUser, TRole> : IdentityBuilder where TUser : class where TRole : class
{
public IdentityBuilder(IServiceCollection services) : base(typeof(TUser), typeof(TRole), services) { }
public IdentityBuilder<TUser, TRole> AddInstance<TService>(TService instance) public IdentityBuilder<TUser, TRole> AddInstance<TService>(TService instance)
where TService : class where TService : class
{ {
@ -43,7 +70,7 @@ namespace Microsoft.AspNet.Identity
return AddInstance(validator); return AddInstance(validator);
} }
public IdentityBuilder<TUser, TRole> AddTokenProvider<TTokenProvider>() where TTokenProvider : class, IUserTokenProvider<TUser> public IdentityBuilder<TUser, TRole> AddTokenProvider<TTokenProvider>() where TTokenProvider : IUserTokenProvider<TUser>
{ {
Services.AddScoped<IUserTokenProvider<TUser>, TTokenProvider>(); Services.AddScoped<IUserTokenProvider<TUser>, TTokenProvider>();
return this; return this;

View File

@ -6,7 +6,6 @@ using Microsoft.AspNet.Http;
using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Security; using Microsoft.AspNet.Security;
using Microsoft.AspNet.Security.Cookies; using Microsoft.AspNet.Security.Cookies;
using Microsoft.AspNet.Security.DataProtection;
using Microsoft.Framework.ConfigurationModel; using Microsoft.Framework.ConfigurationModel;
namespace Microsoft.Framework.DependencyInjection namespace Microsoft.Framework.DependencyInjection
@ -18,24 +17,36 @@ namespace Microsoft.Framework.DependencyInjection
return services.Configure(configure); return services.Configure(configure);
} }
public static IdentityBuilder<IdentityUser, IdentityRole> AddIdentity(this IServiceCollection services,
IConfiguration identityConfig = null, Action<IdentityOptions> configureOptions = null)
{
return services.AddIdentity<IdentityUser, IdentityRole>(identityConfig, configureOptions);
}
public static IdentityBuilder<IdentityUser, IdentityRole> AddIdentity(this IServiceCollection services) public static IdentityBuilder<IdentityUser, IdentityRole> AddIdentity(this IServiceCollection services)
{ {
return services.AddIdentity<IdentityUser, IdentityRole>(); return services.AddIdentity<IdentityUser, IdentityRole>();
} }
public static IdentityBuilder<TUser, TRole> AddIdentity<TUser, TRole>(this IServiceCollection services, public static IdentityBuilder<IdentityUser, IdentityRole> AddIdentity(
IConfiguration identityConfig = null, Action<IdentityOptions> configureOptions = null) this IServiceCollection services,
IConfiguration identityConfig = null,
Action<IdentityOptions> configureOptions = null,
bool useDefaultSubKey = true)
{
return services.AddIdentity<IdentityUser, IdentityRole>(identityConfig, configureOptions, useDefaultSubKey);
}
public static IdentityBuilder<TUser, TRole> AddIdentity<TUser, TRole>(
this IServiceCollection services,
IConfiguration identityConfig = null,
Action<IdentityOptions> configureOptions = null,
bool useDefaultSubKey = true)
where TUser : class where TUser : class
where TRole : class where TRole : class
{ {
services.Add(IdentityServices.GetDefaultServices<TUser, TRole>());
if (identityConfig != null) if (identityConfig != null)
{ {
if (useDefaultSubKey)
{
identityConfig = identityConfig.GetSubKey("identity");
}
services.Configure<IdentityOptions>(identityConfig); services.Configure<IdentityOptions>(identityConfig);
} }
if (configureOptions != null) if (configureOptions != null)
@ -43,13 +54,6 @@ namespace Microsoft.Framework.DependencyInjection
services.ConfigureIdentity(configureOptions); services.ConfigureIdentity(configureOptions);
} }
services.Add(IdentityServices.GetDefaultServices<TUser, TRole>(identityConfig));
services.AddScoped<UserManager<TUser>>();
services.AddScoped<SignInManager<TUser>>();
services.AddScoped<ISecurityStampValidator, SecurityStampValidator<TUser>>();
services.AddScoped<RoleManager<TRole>>();
services.AddScoped<IClaimsIdentityFactory<TUser>, ClaimsIdentityFactory<TUser, TRole>>();
services.Configure<ExternalAuthenticationOptions>(options => services.Configure<ExternalAuthenticationOptions>(options =>
{ {
options.SignInAsAuthenticationType = IdentityOptions.ExternalCookieAuthenticationType; options.SignInAsAuthenticationType = IdentityOptions.ExternalCookieAuthenticationType;
@ -58,7 +62,6 @@ namespace Microsoft.Framework.DependencyInjection
services.Configure<CookieAuthenticationOptions>(options => services.Configure<CookieAuthenticationOptions>(options =>
{ {
options.AuthenticationType = IdentityOptions.ApplicationCookieAuthenticationType; options.AuthenticationType = IdentityOptions.ApplicationCookieAuthenticationType;
//CookieName = ".AspNet.Identity." + ClaimsIdentityOptions.DefaultAuthenticationType,
options.LoginPath = new PathString("/Account/Login"); options.LoginPath = new PathString("/Account/Login");
options.Notifications = new CookieAuthenticationNotifications options.Notifications = new CookieAuthenticationNotifications
{ {
@ -91,32 +94,5 @@ namespace Microsoft.Framework.DependencyInjection
return new IdentityBuilder<TUser, TRole>(services); return new IdentityBuilder<TUser, TRole>(services);
} }
public static IdentityBuilder<TUser, TRole> AddDefaultIdentity<TUser, TRole>(this IServiceCollection services, IConfiguration config = null, Action<IdentityOptions> configureOptions = null)
where TUser : class
where TRole : class
{
services.Configure<DataProtectionTokenProviderOptions>(options =>
{
options.Name = Resources.DefaultTokenProvider;
});
return services.AddIdentity<TUser, TRole>(config)
.AddTokenProvider<DataProtectorTokenProvider<TUser>>()
.AddTokenProvider<PhoneNumberTokenProvider<TUser>>()
.AddTokenProvider<EmailTokenProvider<TUser>>();
}
public static IdentityBuilder<TUser, IdentityRole> AddIdentity<TUser>(this IServiceCollection services)
where TUser : class
{
return services.AddIdentity<TUser, IdentityRole>();
}
public static IdentityBuilder<TUser, IdentityRole> AddIdentity<TUser>(this IServiceCollection services,
IConfiguration identityConfig)
where TUser : class
{
return services.AddIdentity<TUser, IdentityRole>(identityConfig);
}
} }
} }

View File

@ -1,7 +1,6 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // 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. // 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 System.Collections.Generic;
using Microsoft.Framework.ConfigurationModel; using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
@ -30,6 +29,11 @@ namespace Microsoft.AspNet.Identity
yield return describe.Transient<IPasswordHasher<TUser>, PasswordHasher<TUser>>(); yield return describe.Transient<IPasswordHasher<TUser>, PasswordHasher<TUser>>();
yield return describe.Transient<IUserNameNormalizer, UpperInvariantUserNameNormalizer>(); yield return describe.Transient<IUserNameNormalizer, UpperInvariantUserNameNormalizer>();
yield return describe.Transient<IRoleValidator<TRole>, RoleValidator<TRole>>(); yield return describe.Transient<IRoleValidator<TRole>, RoleValidator<TRole>>();
yield return describe.Scoped<ISecurityStampValidator, SecurityStampValidator<TUser>>();
yield return describe.Scoped<IClaimsIdentityFactory<TUser>, ClaimsIdentityFactory<TUser, TRole>>();
yield return describe.Scoped<UserManager<TUser>, UserManager<TUser>>();
yield return describe.Scoped<SignInManager<TUser>, SignInManager<TUser>>();
yield return describe.Scoped<RoleManager<TRole>, RoleManager<TRole>>();
} }
} }
} }

View File

@ -20,7 +20,8 @@ namespace Microsoft.AspNet.Identity
where TRole : IdentityRole where TRole : IdentityRole
where TDbContext : DbContext where TDbContext : DbContext
{ {
var builder = services.AddDefaultIdentity<TUser, TRole>(); var builder = services.AddIdentity<TUser, TRole>();
builder.AddDefaultTokenProviders();
services.AddInstance<IUserStore<TUser>>(new InMemoryUserStore<TUser, TDbContext>(context)); services.AddInstance<IUserStore<TUser>>(new InMemoryUserStore<TUser, TDbContext>(context));
var store = new RoleStore<TRole, TDbContext>(context); var store = new RoleStore<TRole, TDbContext>(context);
services.AddInstance<IRoleStore<TRole>>(store); services.AddInstance<IRoleStore<TRole>>(store);

View File

@ -25,7 +25,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.InMemory.Test
public static RoleManager<IdentityRole> CreateRoleManager(InMemoryContext context) public static RoleManager<IdentityRole> CreateRoleManager(InMemoryContext context)
{ {
var services = new ServiceCollection(); var services = new ServiceCollection();
services.AddDefaultIdentity<IdentityUser, IdentityRole>().AddRoleStore(new RoleStore<IdentityRole>(context)); services.AddIdentity<IdentityUser, IdentityRole>().AddRoleStore(new RoleStore<IdentityRole>(context));
return services.BuildServiceProvider().GetRequiredService<RoleManager<IdentityRole>>(); return services.BuildServiceProvider().GetRequiredService<RoleManager<IdentityRole>>();
} }

View File

@ -49,8 +49,8 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
builder.UseServices(services => builder.UseServices(services =>
{ {
DbUtil.ConfigureDbServices<IdentityDbContext>(ConnectionString, services); DbUtil.ConfigureDbServices<IdentityDbContext>(ConnectionString, services);
services.AddDefaultIdentity<IdentityDbContext, IdentityUser, IdentityRole>();
services.Add(DataProtectionServices.GetDefaultServices()); services.Add(DataProtectionServices.GetDefaultServices());
services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<IdentityDbContext>();
}); });
var userStore = builder.ApplicationServices.GetRequiredService<IUserStore<IdentityUser>>(); var userStore = builder.ApplicationServices.GetRequiredService<IUserStore<IdentityUser>>();

View File

@ -85,7 +85,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
builder.UseServices(services => builder.UseServices(services =>
{ {
DbUtil.ConfigureDbServices<TestDbContext>(ConnectionString, services); DbUtil.ConfigureDbServices<TestDbContext>(ConnectionString, services);
services.AddIdentityEntityFramework<TestDbContext, TUser, TRole, TKey>(); services.AddIdentity<TUser, TRole>().AddEntityFrameworkStores<TestDbContext, TKey>();
}); });
var userStore = builder.ApplicationServices.GetRequiredService<IUserStore<TUser>>(); var userStore = builder.ApplicationServices.GetRequiredService<IUserStore<TUser>>();
@ -110,8 +110,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
builder.UseServices(services => builder.UseServices(services =>
{ {
DbUtil.ConfigureDbServices<TestDbContext>(ConnectionString, services); DbUtil.ConfigureDbServices<TestDbContext>(ConnectionString, services);
services.AddIdentityEntityFramework<TestDbContext, TUser, TRole, TKey>(); services.AddIdentity<TUser, TRole>(null, options =>
services.ConfigureIdentity(options =>
{ {
options.Password.RequiredLength = 1; options.Password.RequiredLength = 1;
options.Password.RequireLowercase = false; options.Password.RequireLowercase = false;
@ -119,7 +118,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
options.Password.RequireUppercase = false; options.Password.RequireUppercase = false;
options.Password.RequireDigit = false; options.Password.RequireDigit = false;
options.User.UserNameValidationRegex = null; options.User.UserNameValidationRegex = null;
}); }).AddEntityFrameworkStores<TestDbContext, TKey>();
}); });
var userStore = builder.ApplicationServices.GetRequiredService<IUserStore<TUser>>(); var userStore = builder.ApplicationServices.GetRequiredService<IUserStore<TUser>>();

View File

@ -54,8 +54,8 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
builder.UseServices(services => builder.UseServices(services =>
{ {
DbUtil.ConfigureDbServices<ApplicationDbContext>(ConnectionString, services); DbUtil.ConfigureDbServices<ApplicationDbContext>(ConnectionString, services);
services.AddDefaultIdentity<ApplicationDbContext, ApplicationUser, IdentityRole>();
services.Add(DataProtectionServices.GetDefaultServices()); services.Add(DataProtectionServices.GetDefaultServices());
services.AddIdentity<ApplicationUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>();
}); });
var userStore = builder.ApplicationServices.GetRequiredService<IUserStore<ApplicationUser>>(); var userStore = builder.ApplicationServices.GetRequiredService<IUserStore<ApplicationUser>>();
@ -84,14 +84,14 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
services.AddEntityFramework() services.AddEntityFramework()
.AddSqlServer() .AddSqlServer()
.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(ConnectionString)); .AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(ConnectionString));
services.AddIdentityEntityFramework<ApplicationDbContext, ApplicationUser>(options => services.AddIdentity<ApplicationUser, IdentityRole>(null, options =>
{ {
options.Password.RequiredLength = 1; options.Password.RequiredLength = 1;
options.Password.RequireLowercase = false; options.Password.RequireLowercase = false;
options.Password.RequireNonLetterOrDigit = false; options.Password.RequireNonLetterOrDigit = false;
options.Password.RequireUppercase = false; options.Password.RequireUppercase = false;
options.Password.RequireDigit = false; options.Password.RequireDigit = false;
}); }).AddEntityFrameworkStores<ApplicationDbContext>();
}); });
var userStore = builder.ApplicationServices.GetRequiredService<IUserStore<ApplicationUser>>(); var userStore = builder.ApplicationServices.GetRequiredService<IUserStore<ApplicationUser>>();

View File

@ -1,7 +1,6 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // 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. // 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;
using Microsoft.Framework.DependencyInjection.Fallback; using Microsoft.Framework.DependencyInjection.Fallback;
using Microsoft.Framework.OptionsModel; using Microsoft.Framework.OptionsModel;
@ -16,7 +15,7 @@ namespace Microsoft.AspNet.Identity.Test
{ {
var services = new ServiceCollection(); var services = new ServiceCollection();
var validator = new UserValidator<IdentityUser>(); var validator = new UserValidator<IdentityUser>();
services.AddIdentity<IdentityUser>().AddUserValidator(validator); services.AddIdentity().AddUserValidator(validator);
Assert.Equal(validator, services.BuildServiceProvider().GetRequiredService<IUserValidator<IdentityUser>>()); Assert.Equal(validator, services.BuildServiceProvider().GetRequiredService<IUserValidator<IdentityUser>>());
} }
@ -25,7 +24,7 @@ namespace Microsoft.AspNet.Identity.Test
{ {
var services = new ServiceCollection(); var services = new ServiceCollection();
var validator = new PasswordValidator<IdentityUser>(); var validator = new PasswordValidator<IdentityUser>();
services.AddIdentity<IdentityUser>().AddPasswordValidator(validator); services.AddIdentity().AddPasswordValidator(validator);
Assert.Equal(validator, services.BuildServiceProvider().GetRequiredService<IPasswordValidator<IdentityUser>>()); Assert.Equal(validator, services.BuildServiceProvider().GetRequiredService<IPasswordValidator<IdentityUser>>());
} }
@ -39,7 +38,7 @@ namespace Microsoft.AspNet.Identity.Test
public void EnsureDefaultServices() public void EnsureDefaultServices()
{ {
var services = new ServiceCollection(); var services = new ServiceCollection();
services.AddIdentity<IdentityUser>(); services.AddIdentity();
services.Add(OptionsServices.GetDefaultServices()); services.Add(OptionsServices.GetDefaultServices());
var provider = services.BuildServiceProvider(); var provider = services.BuildServiceProvider();
@ -57,7 +56,7 @@ namespace Microsoft.AspNet.Identity.Test
where TService : class where TService : class
{ {
var services = new ServiceCollection(); var services = new ServiceCollection();
services.AddIdentity<IdentityUser>().AddInstance(instance); services.AddIdentity().AddInstance(instance);
Assert.Equal(instance, services.BuildServiceProvider().GetRequiredService<TService>()); Assert.Equal(instance, services.BuildServiceProvider().GetRequiredService<TService>());
} }

View File

@ -39,8 +39,10 @@ namespace Microsoft.AspNet.Identity.Test
Assert.Equal(ClaimsIdentityOptions.DefaultSecurityStampClaimType, options.ClaimsIdentity.SecurityStampClaimType); Assert.Equal(ClaimsIdentityOptions.DefaultSecurityStampClaimType, options.ClaimsIdentity.SecurityStampClaimType);
} }
[Fact] [Theory]
public void IdentityOptionsFromConfig() [InlineData(true)]
[InlineData(false)]
public void IdentityOptionsFromConfig(bool useDefaultSubKey)
{ {
const string roleClaimType = "rolez"; const string roleClaimType = "rolez";
const string usernameClaimType = "namez"; const string usernameClaimType = "namez";
@ -66,7 +68,14 @@ namespace Microsoft.AspNet.Identity.Test
Assert.Equal(roleClaimType, config.Get("identity:claimsidentity:roleclaimtype")); Assert.Equal(roleClaimType, config.Get("identity:claimsidentity:roleclaimtype"));
var services = new ServiceCollection {OptionsServices.GetDefaultServices()}; 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<IOptions<IdentityOptions>>(); var accessor = services.BuildServiceProvider().GetRequiredService<IOptions<IdentityOptions>>();
Assert.NotNull(accessor); Assert.NotNull(accessor);
var options = accessor.Options; var options = accessor.Options;
@ -95,7 +104,7 @@ namespace Microsoft.AspNet.Identity.Test
}; };
var config = new Configuration { new MemoryConfigurationSource(dic) }; var config = new Configuration { new MemoryConfigurationSource(dic) };
var services = new ServiceCollection { OptionsServices.GetDefaultServices() }; var services = new ServiceCollection { OptionsServices.GetDefaultServices() };
services.AddIdentity(config.GetSubKey("identity"), services.AddIdentity(config,
o => { o.User.RequireUniqueEmail = false; o.Lockout.MaxFailedAccessAttempts++; }); o => { o.User.RequireUniqueEmail = false; o.Lockout.MaxFailedAccessAttempts++; });
var accessor = services.BuildServiceProvider().GetRequiredService<IOptions<IdentityOptions>>(); var accessor = services.BuildServiceProvider().GetRequiredService<IOptions<IdentityOptions>>();
Assert.NotNull(accessor); Assert.NotNull(accessor);
@ -117,7 +126,7 @@ namespace Microsoft.AspNet.Identity.Test
var builder = new ApplicationBuilder(CallContextServiceLocator.Locator.ServiceProvider); var builder = new ApplicationBuilder(CallContextServiceLocator.Locator.ServiceProvider);
builder.UseServices(services => builder.UseServices(services =>
{ {
services.AddIdentity<IdentityUser>(); services.AddIdentity();
services.ConfigureOptions<PasswordsNegativeLengthSetup>(); services.ConfigureOptions<PasswordsNegativeLengthSetup>();
}); });
@ -139,7 +148,7 @@ namespace Microsoft.AspNet.Identity.Test
var app = new ApplicationBuilder(CallContextServiceLocator.Locator.ServiceProvider); var app = new ApplicationBuilder(CallContextServiceLocator.Locator.ServiceProvider);
app.UseServices(services => app.UseServices(services =>
{ {
services.AddIdentity<IdentityUser>().ConfigureIdentity(options => options.User.RequireUniqueEmail = true); services.ConfigureIdentity(options => options.User.RequireUniqueEmail = true);
}); });
var optionsGetter = app.ApplicationServices.GetRequiredService<IOptions<IdentityOptions>>(); var optionsGetter = app.ApplicationServices.GetRequiredService<IOptions<IdentityOptions>>();

View File

@ -33,7 +33,7 @@ namespace Microsoft.AspNet.Identity.Test
services.Add(OptionsServices.GetDefaultServices()); services.Add(OptionsServices.GetDefaultServices());
services.Add(HostingServices.GetDefaultServices()); services.Add(HostingServices.GetDefaultServices());
services.Add(DataProtectionServices.GetDefaultServices()); services.Add(DataProtectionServices.GetDefaultServices());
services.AddDefaultIdentity<TUser, TRole>(); services.AddIdentity<TUser, TRole>().AddDefaultTokenProviders();
AddUserStore(services, context); AddUserStore(services, context);
AddRoleStore(services, context); AddRoleStore(services, context);
services.ConfigureIdentity(options => services.ConfigureIdentity(options =>
@ -44,7 +44,6 @@ namespace Microsoft.AspNet.Identity.Test
options.Password.RequireUppercase = false; options.Password.RequireUppercase = false;
options.User.UserNameValidationRegex = null; options.User.UserNameValidationRegex = null;
}); });
} }
protected virtual UserManager<TUser> CreateManager(object context = null, IServiceCollection services = null) protected virtual UserManager<TUser> CreateManager(object context = null, IServiceCollection services = null)