Replace AddDefaultIdentity
Break it into AddIdentity<>.AddEntityFrameworkStores.AddDefaultTokenProviders()
This commit is contained in:
parent
c84a6b436f
commit
85530742ef
|
|
@ -36,10 +36,12 @@ namespace IdentitySamples
|
|||
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);
|
||||
});
|
||||
})
|
||||
.AddEntityFrameworkStores<ApplicationDbContext>()
|
||||
.AddDefaultTokenProviders();
|
||||
|
||||
services.ConfigureFacebookAuthentication(options =>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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<IdentityUser, IdentityRole> AddEntityFramework(this IdentityBuilder<IdentityUser, IdentityRole> builder)
|
||||
{
|
||||
return builder.AddEntityFramework<IdentityDbContext, IdentityUser, IdentityRole>();
|
||||
}
|
||||
|
||||
public static IdentityBuilder<IdentityUser, IdentityRole> AddEntityFramework<TContext>(this IdentityBuilder<IdentityUser, IdentityRole> builder)
|
||||
public static IdentityBuilder AddEntityFrameworkStores<TContext>(this IdentityBuilder builder)
|
||||
where TContext : DbContext
|
||||
{
|
||||
return builder.AddEntityFramework<TContext, IdentityUser, IdentityRole>();
|
||||
}
|
||||
|
||||
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>>();
|
||||
builder.Services.Add(IdentityEntityFrameworkServices.GetDefaultServices(builder.UserType, builder.RoleType, typeof(TContext)));
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static IdentityBuilder<TUser, TRole> AddEntityFramework<TContext, TUser, TRole, TKey>(this IdentityBuilder<TUser, TRole> builder)
|
||||
where TUser : IdentityUser<TKey>, new()
|
||||
where TRole : IdentityRole<TKey>, new()
|
||||
public static IdentityBuilder AddEntityFrameworkStores<TContext, TKey>(this IdentityBuilder builder)
|
||||
where TContext : DbContext
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
builder.Services.AddScoped<IUserStore<TUser>, UserStore<TUser, TRole, TContext, TKey>>();
|
||||
builder.Services.AddScoped<IRoleStore<TRole>, RoleStore<TRole, TContext, TKey>>();
|
||||
builder.Services.AddScoped<TContext>();
|
||||
builder.Services.Add(IdentityEntityFrameworkServices.GetDefaultServices(builder.UserType, builder.RoleType, typeof(TContext), typeof(TKey)));
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -3,19 +3,46 @@
|
|||
|
||||
using System;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
|
||||
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(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<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)
|
||||
where TService : class
|
||||
{
|
||||
|
|
@ -43,7 +70,7 @@ namespace Microsoft.AspNet.Identity
|
|||
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>();
|
||||
return this;
|
||||
|
|
|
|||
|
|
@ -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<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)
|
||||
{
|
||||
return services.AddIdentity<IdentityUser, IdentityRole>();
|
||||
}
|
||||
|
||||
public static IdentityBuilder<TUser, TRole> AddIdentity<TUser, TRole>(this IServiceCollection services,
|
||||
IConfiguration identityConfig = null, Action<IdentityOptions> configureOptions = null)
|
||||
public static IdentityBuilder<IdentityUser, IdentityRole> AddIdentity(
|
||||
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 TRole : class
|
||||
{
|
||||
services.Add(IdentityServices.GetDefaultServices<TUser, TRole>());
|
||||
|
||||
if (identityConfig != null)
|
||||
{
|
||||
if (useDefaultSubKey)
|
||||
{
|
||||
identityConfig = identityConfig.GetSubKey("identity");
|
||||
}
|
||||
services.Configure<IdentityOptions>(identityConfig);
|
||||
}
|
||||
if (configureOptions != null)
|
||||
|
|
@ -43,13 +54,6 @@ namespace Microsoft.Framework.DependencyInjection
|
|||
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 =>
|
||||
{
|
||||
options.SignInAsAuthenticationType = IdentityOptions.ExternalCookieAuthenticationType;
|
||||
|
|
@ -58,7 +62,6 @@ namespace Microsoft.Framework.DependencyInjection
|
|||
services.Configure<CookieAuthenticationOptions>(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<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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<IPasswordHasher<TUser>, PasswordHasher<TUser>>();
|
||||
yield return describe.Transient<IUserNameNormalizer, UpperInvariantUserNameNormalizer>();
|
||||
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>>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -20,7 +20,8 @@ namespace Microsoft.AspNet.Identity
|
|||
where TRole : IdentityRole
|
||||
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));
|
||||
var store = new RoleStore<TRole, TDbContext>(context);
|
||||
services.AddInstance<IRoleStore<TRole>>(store);
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.InMemory.Test
|
|||
public static RoleManager<IdentityRole> CreateRoleManager(InMemoryContext context)
|
||||
{
|
||||
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>>();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -49,8 +49,8 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
builder.UseServices(services =>
|
||||
{
|
||||
DbUtil.ConfigureDbServices<IdentityDbContext>(ConnectionString, services);
|
||||
services.AddDefaultIdentity<IdentityDbContext, IdentityUser, IdentityRole>();
|
||||
services.Add(DataProtectionServices.GetDefaultServices());
|
||||
services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<IdentityDbContext>();
|
||||
});
|
||||
|
||||
var userStore = builder.ApplicationServices.GetRequiredService<IUserStore<IdentityUser>>();
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
builder.UseServices(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>>();
|
||||
|
|
@ -110,8 +110,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
builder.UseServices(services =>
|
||||
{
|
||||
DbUtil.ConfigureDbServices<TestDbContext>(ConnectionString, services);
|
||||
services.AddIdentityEntityFramework<TestDbContext, TUser, TRole, TKey>();
|
||||
services.ConfigureIdentity(options =>
|
||||
services.AddIdentity<TUser, TRole>(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<TestDbContext, TKey>();
|
||||
});
|
||||
|
||||
var userStore = builder.ApplicationServices.GetRequiredService<IUserStore<TUser>>();
|
||||
|
|
|
|||
|
|
@ -54,8 +54,8 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
builder.UseServices(services =>
|
||||
{
|
||||
DbUtil.ConfigureDbServices<ApplicationDbContext>(ConnectionString, services);
|
||||
services.AddDefaultIdentity<ApplicationDbContext, ApplicationUser, IdentityRole>();
|
||||
services.Add(DataProtectionServices.GetDefaultServices());
|
||||
services.AddIdentity<ApplicationUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>();
|
||||
});
|
||||
|
||||
var userStore = builder.ApplicationServices.GetRequiredService<IUserStore<ApplicationUser>>();
|
||||
|
|
@ -84,14 +84,14 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
services.AddEntityFramework()
|
||||
.AddSqlServer()
|
||||
.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(ConnectionString));
|
||||
services.AddIdentityEntityFramework<ApplicationDbContext, ApplicationUser>(options =>
|
||||
services.AddIdentity<ApplicationUser, IdentityRole>(null, options =>
|
||||
{
|
||||
options.Password.RequiredLength = 1;
|
||||
options.Password.RequireLowercase = false;
|
||||
options.Password.RequireNonLetterOrDigit = false;
|
||||
options.Password.RequireUppercase = false;
|
||||
options.Password.RequireDigit = false;
|
||||
});
|
||||
}).AddEntityFrameworkStores<ApplicationDbContext>();
|
||||
});
|
||||
|
||||
var userStore = builder.ApplicationServices.GetRequiredService<IUserStore<ApplicationUser>>();
|
||||
|
|
|
|||
|
|
@ -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<IdentityUser>();
|
||||
services.AddIdentity<IdentityUser>().AddUserValidator(validator);
|
||||
services.AddIdentity().AddUserValidator(validator);
|
||||
Assert.Equal(validator, services.BuildServiceProvider().GetRequiredService<IUserValidator<IdentityUser>>());
|
||||
}
|
||||
|
||||
|
|
@ -25,7 +24,7 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
{
|
||||
var services = new ServiceCollection();
|
||||
var validator = new PasswordValidator<IdentityUser>();
|
||||
services.AddIdentity<IdentityUser>().AddPasswordValidator(validator);
|
||||
services.AddIdentity().AddPasswordValidator(validator);
|
||||
Assert.Equal(validator, services.BuildServiceProvider().GetRequiredService<IPasswordValidator<IdentityUser>>());
|
||||
}
|
||||
|
||||
|
|
@ -39,7 +38,7 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
public void EnsureDefaultServices()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddIdentity<IdentityUser>();
|
||||
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<IdentityUser>().AddInstance(instance);
|
||||
services.AddIdentity().AddInstance(instance);
|
||||
Assert.Equal(instance, services.BuildServiceProvider().GetRequiredService<TService>());
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<IOptions<IdentityOptions>>();
|
||||
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<IOptions<IdentityOptions>>();
|
||||
Assert.NotNull(accessor);
|
||||
|
|
@ -117,7 +126,7 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
var builder = new ApplicationBuilder(CallContextServiceLocator.Locator.ServiceProvider);
|
||||
builder.UseServices(services =>
|
||||
{
|
||||
services.AddIdentity<IdentityUser>();
|
||||
services.AddIdentity();
|
||||
services.ConfigureOptions<PasswordsNegativeLengthSetup>();
|
||||
});
|
||||
|
||||
|
|
@ -139,7 +148,7 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
var app = new ApplicationBuilder(CallContextServiceLocator.Locator.ServiceProvider);
|
||||
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>>();
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
services.Add(OptionsServices.GetDefaultServices());
|
||||
services.Add(HostingServices.GetDefaultServices());
|
||||
services.Add(DataProtectionServices.GetDefaultServices());
|
||||
services.AddDefaultIdentity<TUser, TRole>();
|
||||
services.AddIdentity<TUser, TRole>().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<TUser> CreateManager(object context = null, IServiceCollection services = null)
|
||||
|
|
|
|||
Loading…
Reference in New Issue