Rationalize IdentityDbContext ctors
Also clean up unit tests
This commit is contained in:
parent
92267b4c25
commit
3a9036a69f
|
|
@ -8,13 +8,7 @@ namespace IdentitySample.Models
|
|||
{
|
||||
public class ApplicationUser : IdentityUser { }
|
||||
|
||||
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
|
||||
{
|
||||
public ApplicationDbContext(IServiceProvider serviceProvider, IOptions<IdentityDbContextOptions> optionsAccessor)
|
||||
: base(serviceProvider, optionsAccessor.Options)
|
||||
{
|
||||
}
|
||||
}
|
||||
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { }
|
||||
|
||||
public class IdentityDbContextOptions : DbContextOptions
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
using System;
|
||||
using IdentitySample.Models;
|
||||
using Microsoft.AspNet.Builder;
|
||||
using Microsoft.AspNet.Diagnostics;
|
||||
|
|
@ -6,7 +7,6 @@ using Microsoft.AspNet.Routing;
|
|||
using Microsoft.Data.Entity;
|
||||
using Microsoft.Framework.ConfigurationModel;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using System;
|
||||
|
||||
namespace IdentitySamples
|
||||
{
|
||||
|
|
@ -27,13 +27,13 @@ namespace IdentitySamples
|
|||
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddEntityFramework().AddSqlServer();
|
||||
services.AddScoped<ApplicationDbContext>();
|
||||
services.AddEntityFramework()
|
||||
.AddSqlServer()
|
||||
.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.Get("Data:IdentityConnection:ConnectionString")));
|
||||
services.Configure<IdentityDbContextOptions>(options =>
|
||||
{
|
||||
options.DefaultAdminUserName = Configuration.Get("DefaultAdminUsername");
|
||||
options.DefaultAdminPassword = Configuration.Get("DefaultAdminPassword");
|
||||
options.UseSqlServer(Configuration.Get("Data:IdentityConnection:ConnectionString"));
|
||||
});
|
||||
|
||||
services.AddDefaultIdentity<ApplicationDbContext, ApplicationUser, IdentityRole>(Configuration, options =>
|
||||
|
|
|
|||
|
|
@ -7,20 +7,10 @@ using Microsoft.Data.Entity.Metadata;
|
|||
|
||||
namespace Microsoft.AspNet.Identity.EntityFramework
|
||||
{
|
||||
public class IdentityDbContext :
|
||||
IdentityDbContext<IdentityUser, IdentityRole, string>
|
||||
{
|
||||
public IdentityDbContext() { }
|
||||
public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) { }
|
||||
}
|
||||
public class IdentityDbContext : IdentityDbContext<IdentityUser, IdentityRole, string> { }
|
||||
|
||||
public class IdentityDbContext<TUser> :
|
||||
IdentityDbContext<TUser, IdentityRole, string>
|
||||
where TUser : IdentityUser
|
||||
{
|
||||
public IdentityDbContext() { }
|
||||
public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) { }
|
||||
}
|
||||
public class IdentityDbContext<TUser> : IdentityDbContext<TUser, IdentityRole, string> where TUser : IdentityUser
|
||||
{ }
|
||||
|
||||
public class IdentityDbContext<TUser, TRole, TKey> : DbContext
|
||||
where TUser : IdentityUser<TKey>
|
||||
|
|
@ -34,9 +24,6 @@ namespace Microsoft.AspNet.Identity.EntityFramework
|
|||
public DbSet<TRole> Roles { get; set; }
|
||||
public DbSet<IdentityRoleClaim<TKey>> RoleClaims { get; set; }
|
||||
|
||||
public IdentityDbContext() { }
|
||||
public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) { }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder builder)
|
||||
{
|
||||
builder.Entity<TUser>(b =>
|
||||
|
|
|
|||
|
|
@ -1,27 +1,26 @@
|
|||
// 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 Microsoft.AspNet.Builder;
|
||||
using System;
|
||||
using Microsoft.AspNet.Identity;
|
||||
using Microsoft.AspNet.Identity.EntityFramework;
|
||||
using Microsoft.Data.Entity;
|
||||
using Microsoft.Framework.ConfigurationModel;
|
||||
using System;
|
||||
|
||||
namespace Microsoft.Framework.DependencyInjection
|
||||
{
|
||||
public static class IdentityEntityFrameworkServiceCollectionExtensions
|
||||
{
|
||||
// MOVE to builder extension
|
||||
public static IdentityBuilder<IdentityUser, IdentityRole> AddIdentitySqlServer(this IServiceCollection services)
|
||||
public static IdentityBuilder<IdentityUser, IdentityRole> AddIdentityEntityFramework(this IServiceCollection services)
|
||||
{
|
||||
return services.AddIdentitySqlServer<IdentityDbContext, IdentityUser, IdentityRole>();
|
||||
return services.AddIdentityEntityFramework<IdentityDbContext, IdentityUser, IdentityRole>();
|
||||
}
|
||||
|
||||
public static IdentityBuilder<IdentityUser, IdentityRole> AddIdentitySqlServer<TContext>(this IServiceCollection services)
|
||||
public static IdentityBuilder<IdentityUser, IdentityRole> AddIdentityEntityFramework<TContext>(this IServiceCollection services)
|
||||
where TContext : DbContext
|
||||
{
|
||||
return services.AddIdentitySqlServer<TContext, IdentityUser, IdentityRole>();
|
||||
return services.AddIdentityEntityFramework<TContext, IdentityUser, IdentityRole>();
|
||||
}
|
||||
|
||||
public static IdentityBuilder<TUser, TRole> AddDefaultIdentity<TContext, TUser, TRole>(this IServiceCollection services, IConfiguration config = null,
|
||||
|
|
@ -34,14 +33,14 @@ namespace Microsoft.Framework.DependencyInjection
|
|||
.AddEntityFramework<TContext, TUser, TRole>();
|
||||
}
|
||||
|
||||
public static IdentityBuilder<TUser, IdentityRole> AddIdentitySqlServer<TContext, TUser>(this IServiceCollection services, Action<IdentityOptions> configureOptions = null)
|
||||
public static IdentityBuilder<TUser, IdentityRole> AddIdentityEntityFramework<TContext, TUser>(this IServiceCollection services, Action<IdentityOptions> configureOptions = null)
|
||||
where TUser : IdentityUser, new()
|
||||
where TContext : DbContext
|
||||
{
|
||||
return services.AddIdentitySqlServer<TContext, TUser, IdentityRole>(null, configureOptions);
|
||||
return services.AddIdentityEntityFramework<TContext, TUser, IdentityRole>(null, configureOptions);
|
||||
}
|
||||
|
||||
public static IdentityBuilder<TUser, TRole> AddIdentitySqlServer<TContext, TUser, TRole>(this IServiceCollection services, IConfiguration config = null, Action<IdentityOptions> configureOptions = null)
|
||||
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
|
||||
|
|
@ -49,11 +48,10 @@ namespace Microsoft.Framework.DependencyInjection
|
|||
var builder = services.AddIdentity<TUser, TRole>(config, configureOptions);
|
||||
services.AddScoped<IUserStore<TUser>, UserStore<TUser, TRole, TContext>>();
|
||||
services.AddScoped<IRoleStore<TRole>, RoleStore<TRole, TContext>>();
|
||||
services.AddScoped<TContext>();
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static IdentityBuilder<TUser, TRole> AddIdentitySqlServer<TContext, TUser, TRole, TKey>(this IServiceCollection services, IConfiguration config = null, Action<IdentityOptions> configureOptions = null)
|
||||
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
|
||||
|
|
@ -62,7 +60,6 @@ namespace Microsoft.Framework.DependencyInjection
|
|||
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>>();
|
||||
services.AddScoped<TContext>();
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ using Microsoft.Framework.OptionsModel;
|
|||
|
||||
namespace Microsoft.AspNet.Identity.EntityFramework.InMemory.Test
|
||||
{
|
||||
public class InMemoryEFUserStoreTest : UserManagerTestBase<IdentityUser, IdentityRole>
|
||||
public class InMemoryEFUserStoreTest : UserManagerTestBase<IdentityUser, IdentityRole>
|
||||
{
|
||||
protected override object CreateTestContext()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -32,10 +32,6 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
{
|
||||
public DbSet<User<TKey>> Users { get; set; }
|
||||
|
||||
public CustomDbContext(IServiceProvider services) :
|
||||
base(services, services.GetService<IOptions<DbContextOptions>>().Options)
|
||||
{ }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
|
@ -45,11 +41,9 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public CustomDbContext<TKey> GetContext<TKey>() where TKey : IEquatable<TKey>
|
||||
{
|
||||
var serviceProvider = UserStoreTest.ConfigureDbServices(ConnectionString).BuildServiceProvider();
|
||||
return new CustomDbContext<TKey>(serviceProvider);
|
||||
return DbUtil.Create<CustomDbContext<TKey>>(ConnectionString);
|
||||
}
|
||||
|
||||
public CustomDbContext<TKey> CreateContext<TKey>(bool delete = false) where TKey : IEquatable<TKey>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,48 @@
|
|||
// 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 Microsoft.AspNet.Hosting;
|
||||
using Microsoft.Data.Entity;
|
||||
using Microsoft.Data.Entity.Services;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.DependencyInjection.Fallback;
|
||||
using Microsoft.Framework.Logging;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
||||
{
|
||||
[TestCaseOrderer("Microsoft.AspNet.Identity.Test.PriorityOrderer", "Microsoft.AspNet.Identity.EntityFramework.Test")]
|
||||
public static class DbUtil
|
||||
{
|
||||
public static IServiceCollection ConfigureDbServices(string connectionString, IServiceCollection services = null)
|
||||
{
|
||||
return ConfigureDbServices<IdentityDbContext>(connectionString, services);
|
||||
}
|
||||
|
||||
public static IServiceCollection ConfigureDbServices<TContext>(string connectionString, IServiceCollection services = null) where TContext : DbContext
|
||||
{
|
||||
if (services == null)
|
||||
{
|
||||
services = new ServiceCollection();
|
||||
}
|
||||
services.Add(HostingServices.GetDefaultServices());
|
||||
services.AddEntityFramework().AddSqlServer().AddDbContext<TContext>(options => options.UseSqlServer(connectionString));
|
||||
services.Add(OptionsServices.GetDefaultServices());
|
||||
services.AddInstance<ILoggerFactory>(new NullLoggerFactory());
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IdentityDbContext Create(string connectionString)
|
||||
{
|
||||
return Create<IdentityDbContext>(connectionString);
|
||||
}
|
||||
|
||||
public static TContext Create<TContext>(string connectionString) where TContext : DbContext, new()
|
||||
{
|
||||
var serviceProvider = ConfigureDbServices<TContext>(connectionString).BuildServiceProvider();
|
||||
return serviceProvider.GetService<TContext>();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -21,10 +21,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
private readonly string ConnectionString = @"Server=(localdb)\v11.0;Database=DefaultSchemaTest" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year + ";Trusted_Connection=True;";
|
||||
public IdentityDbContext CreateContext(bool ensureCreated = false)
|
||||
{
|
||||
var services = UserStoreTest.ConfigureDbServices(ConnectionString);
|
||||
var serviceProvider = services.BuildServiceProvider();
|
||||
var db = new IdentityDbContext(serviceProvider,
|
||||
serviceProvider.GetService<IOptions<DbContextOptions>>().Options);
|
||||
var db = DbUtil.Create(ConnectionString);
|
||||
if (ensureCreated)
|
||||
{
|
||||
db.Database.EnsureCreated();
|
||||
|
|
@ -53,10 +50,8 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
|
||||
builder.UseServices(services =>
|
||||
{
|
||||
UserStoreTest.ConfigureDbServices(ConnectionString, services);
|
||||
services.AddIdentitySqlServer();
|
||||
// todo: constructor resolution doesn't work well with IdentityDbContext since it has 4 constructors
|
||||
services.AddInstance(context);
|
||||
DbUtil.ConfigureDbServices<IdentityDbContext>(ConnectionString, services);
|
||||
services.AddDefaultIdentity<IdentityDbContext, IdentityUser, IdentityRole>();
|
||||
});
|
||||
|
||||
var userStore = builder.ApplicationServices.GetService<IUserStore<IdentityUser>>();
|
||||
|
|
|
|||
|
|
@ -7,12 +7,8 @@ using System.Security.Claims;
|
|||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Builder;
|
||||
using Microsoft.AspNet.Identity.Test;
|
||||
using Microsoft.Data.Entity;
|
||||
using Microsoft.Data.Entity.Services;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.DependencyInjection.Fallback;
|
||||
using Microsoft.Framework.Logging;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
||||
|
|
@ -25,10 +21,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
{
|
||||
public abstract string ConnectionString { get; }
|
||||
|
||||
public class ApplicationDbContext : IdentityDbContext<TUser, TRole, TKey>
|
||||
{
|
||||
public ApplicationDbContext(IServiceProvider services, IOptions<DbContextOptions> options) : base(services, options.Options) { }
|
||||
}
|
||||
public class TestDbContext : IdentityDbContext<TUser, TRole, TKey> { }
|
||||
|
||||
[TestPriority(-1000)]
|
||||
[Fact]
|
||||
|
|
@ -46,17 +39,13 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
|
||||
public void DropDb()
|
||||
{
|
||||
var serviceProvider = UserStoreTest.ConfigureDbServices(ConnectionString).BuildServiceProvider();
|
||||
var db = new ApplicationDbContext(serviceProvider,
|
||||
serviceProvider.GetService<IOptions<DbContextOptions>>());
|
||||
var db = DbUtil.Create<TestDbContext>(ConnectionString);
|
||||
db.Database.EnsureDeleted();
|
||||
}
|
||||
|
||||
public ApplicationDbContext CreateContext(bool delete = false)
|
||||
public TestDbContext CreateContext(bool delete = false)
|
||||
{
|
||||
var serviceProvider = UserStoreTest.ConfigureDbServices(ConnectionString).BuildServiceProvider();
|
||||
var db = new ApplicationDbContext(serviceProvider,
|
||||
serviceProvider.GetService<IOptions<DbContextOptions>>());
|
||||
var db = DbUtil.Create<TestDbContext>(ConnectionString);
|
||||
if (delete)
|
||||
{
|
||||
db.Database.EnsureDeleted();
|
||||
|
|
@ -76,17 +65,17 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
{
|
||||
context = CreateTestContext();
|
||||
}
|
||||
return MockHelpers.CreateManager(new UserStore<TUser, TRole, ApplicationDbContext, TKey>((ApplicationDbContext)context));
|
||||
return MockHelpers.CreateManager(new UserStore<TUser, TRole, TestDbContext, TKey>((TestDbContext)context));
|
||||
}
|
||||
|
||||
protected override RoleManager<TRole> CreateRoleManager(object context = null)
|
||||
{
|
||||
var services = DbUtil.ConfigureDbServices<TestDbContext>(ConnectionString);
|
||||
if (context == null)
|
||||
{
|
||||
context = CreateTestContext();
|
||||
}
|
||||
var services = UserStoreTest.ConfigureDbServices(ConnectionString);
|
||||
services.AddIdentity<TUser, TRole>().AddRoleStore(new RoleStore<TRole, ApplicationDbContext, TKey>((ApplicationDbContext)context));
|
||||
services.AddIdentity<TUser, TRole>().AddRoleStore(new RoleStore<TRole, TestDbContext, TKey>((TestDbContext)context));
|
||||
return services.BuildServiceProvider().GetService<RoleManager<TRole>>();
|
||||
}
|
||||
|
||||
|
|
@ -103,8 +92,8 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
|
||||
builder.UseServices(services =>
|
||||
{
|
||||
UserStoreTest.ConfigureDbServices(ConnectionString, services);
|
||||
services.AddIdentitySqlServer<ApplicationDbContext, TUser, TRole, TKey>();
|
||||
DbUtil.ConfigureDbServices<TestDbContext>(ConnectionString, services);
|
||||
services.AddIdentityEntityFramework<TestDbContext, TUser, TRole, TKey>();
|
||||
});
|
||||
|
||||
var userStore = builder.ApplicationServices.GetService<IUserStore<TUser>>();
|
||||
|
|
@ -128,8 +117,8 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
|
||||
builder.UseServices(services =>
|
||||
{
|
||||
UserStoreTest.ConfigureDbServices(ConnectionString, services);
|
||||
services.AddIdentitySqlServer<ApplicationDbContext, TUser, TRole, TKey>();
|
||||
DbUtil.ConfigureDbServices<TestDbContext>(ConnectionString, services);
|
||||
services.AddIdentityEntityFramework<TestDbContext, TUser, TRole, TKey>();
|
||||
services.ConfigureIdentity(options =>
|
||||
{
|
||||
options.Password.RequiredLength = 1;
|
||||
|
|
|
|||
|
|
@ -3,10 +3,8 @@
|
|||
|
||||
using System;
|
||||
using Microsoft.AspNet.Identity.Test;
|
||||
using Microsoft.Data.Entity.Services;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.DependencyInjection.Fallback;
|
||||
using Microsoft.Framework.Logging;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
||||
|
|
@ -42,9 +40,9 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
}
|
||||
}
|
||||
|
||||
public class ApplicationUserStore : UserStore<GuidUser, GuidRole, ApplicationDbContext, Guid>
|
||||
public class ApplicationUserStore : UserStore<GuidUser, GuidRole, TestDbContext, Guid>
|
||||
{
|
||||
public ApplicationUserStore(ApplicationDbContext context) : base(context) { }
|
||||
public ApplicationUserStore(TestDbContext context) : base(context) { }
|
||||
|
||||
public override Guid ConvertIdFromString(string userId)
|
||||
{
|
||||
|
|
@ -52,9 +50,9 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
}
|
||||
}
|
||||
|
||||
public class ApplicationRoleStore : RoleStore<GuidRole, ApplicationDbContext, Guid>
|
||||
public class ApplicationRoleStore : RoleStore<GuidRole, TestDbContext, Guid>
|
||||
{
|
||||
public ApplicationRoleStore(ApplicationDbContext context) : base(context) { }
|
||||
public ApplicationRoleStore(TestDbContext context) : base(context) { }
|
||||
|
||||
public override Guid ConvertIdFromString(string id)
|
||||
{
|
||||
|
|
@ -68,7 +66,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
{
|
||||
context = CreateTestContext();
|
||||
}
|
||||
return MockHelpers.CreateManager(new ApplicationUserStore((ApplicationDbContext)context));
|
||||
return MockHelpers.CreateManager(new ApplicationUserStore((TestDbContext)context));
|
||||
}
|
||||
|
||||
protected override RoleManager<GuidRole> CreateRoleManager(object context)
|
||||
|
|
@ -77,8 +75,8 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
{
|
||||
context = CreateTestContext();
|
||||
}
|
||||
var services = UserStoreTest.ConfigureDbServices(ConnectionString);
|
||||
services.AddIdentity<GuidUser, GuidRole>().AddRoleStore(new ApplicationRoleStore((ApplicationDbContext)context));
|
||||
var services = DbUtil.ConfigureDbServices(ConnectionString);
|
||||
services.AddIdentity<GuidUser, GuidRole>().AddRoleStore(new ApplicationRoleStore((TestDbContext)context));
|
||||
return services.BuildServiceProvider().GetService<RoleManager<GuidRole>>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +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 Microsoft.AspNet.Identity.Test;
|
||||
using Microsoft.Data.Entity;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.DependencyInjection.Fallback;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
using System;
|
||||
using Xunit;
|
||||
|
||||
|
|
|
|||
|
|
@ -6,29 +6,24 @@ using System.Linq;
|
|||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Builder;
|
||||
using Microsoft.AspNet.Hosting;
|
||||
using Microsoft.AspNet.Identity.Test;
|
||||
using Microsoft.Data.Entity;
|
||||
using Microsoft.Data.Entity.Services;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.DependencyInjection.Fallback;
|
||||
using Microsoft.Framework.Logging;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
||||
{
|
||||
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { }
|
||||
|
||||
[TestCaseOrderer("Microsoft.AspNet.Identity.Test.PriorityOrderer", "Microsoft.AspNet.Identity.EntityFramework.Test")]
|
||||
public class UserStoreTest : UserManagerTestBase<IdentityUser, IdentityRole>
|
||||
{
|
||||
private readonly string ConnectionString = @"Server=(localdb)\v11.0;Database=SqlUserStoreTest" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year + ";Trusted_Connection=True;";
|
||||
|
||||
public class ApplicationUser : IdentityUser { }
|
||||
|
||||
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
|
||||
{
|
||||
public ApplicationDbContext(IServiceProvider services, IOptions<DbContextOptions> options) : base(services, options.Options) { }
|
||||
}
|
||||
|
||||
[TestPriority(-1000)]
|
||||
[Fact]
|
||||
public void DropDatabaseStart()
|
||||
|
|
@ -45,9 +40,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
|
||||
public void DropDb()
|
||||
{
|
||||
var serviceProvider = ConfigureDbServices(ConnectionString).BuildServiceProvider();
|
||||
var db = new ApplicationDbContext(serviceProvider,
|
||||
serviceProvider.GetService<IOptions<DbContextOptions>>());
|
||||
var db = DbUtil.Create<ApplicationDbContext>(ConnectionString);
|
||||
db.Database.EnsureDeleted();
|
||||
}
|
||||
|
||||
|
|
@ -59,8 +52,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
|
||||
builder.UseServices(services =>
|
||||
{
|
||||
ConfigureDbServices(ConnectionString, services);
|
||||
services.AddScoped<ApplicationDbContext>();
|
||||
DbUtil.ConfigureDbServices<ApplicationDbContext>(ConnectionString, services);
|
||||
services.AddDefaultIdentity<ApplicationDbContext, ApplicationUser, IdentityRole>();
|
||||
});
|
||||
|
||||
|
|
@ -86,8 +78,11 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
builder.UseServices(services =>
|
||||
{
|
||||
services.AddInstance<ILoggerFactory>(new NullLoggerFactory());
|
||||
services.AddEntityFramework().AddSqlServer();
|
||||
services.AddIdentitySqlServer<ApplicationDbContext, ApplicationUser>(options =>
|
||||
services.Add(HostingServices.GetDefaultServices());
|
||||
services.AddEntityFramework()
|
||||
.AddSqlServer()
|
||||
.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(ConnectionString));
|
||||
services.AddIdentityEntityFramework<ApplicationDbContext, ApplicationUser>(options =>
|
||||
{
|
||||
options.Password.RequiredLength = 1;
|
||||
options.Password.RequireLowercase = false;
|
||||
|
|
@ -95,8 +90,6 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
options.Password.RequireUppercase = false;
|
||||
options.Password.RequireDigit = false;
|
||||
});
|
||||
services.Configure<DbContextOptions>(options =>
|
||||
options.UseSqlServer(ConnectionString));
|
||||
});
|
||||
|
||||
var userStore = builder.ApplicationServices.GetService<IUserStore<ApplicationUser>>();
|
||||
|
|
@ -127,8 +120,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
|
||||
public IdentityDbContext CreateContext(bool delete = false)
|
||||
{
|
||||
var serviceProvider = ConfigureDbServices(ConnectionString).BuildServiceProvider();
|
||||
var db = new IdentityDbContext(serviceProvider, serviceProvider.GetService<IOptions<DbContextOptions>>().Options);
|
||||
var db = DbUtil.Create<IdentityDbContext>(ConnectionString);
|
||||
if (delete)
|
||||
{
|
||||
db.Database.EnsureDeleted();
|
||||
|
|
@ -147,25 +139,9 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
CreateContext();
|
||||
}
|
||||
|
||||
public static IServiceCollection ConfigureDbServices(string connectionString, IServiceCollection services = null)
|
||||
{
|
||||
if (services == null)
|
||||
{
|
||||
services = new ServiceCollection();
|
||||
}
|
||||
services.AddEntityFramework().AddSqlServer();
|
||||
services.Add(OptionsServices.GetDefaultServices());
|
||||
services.AddInstance<ILoggerFactory>(new NullLoggerFactory());
|
||||
services.Configure<DbContextOptions>(options =>
|
||||
options.UseSqlServer(connectionString));
|
||||
return services;
|
||||
}
|
||||
|
||||
public ApplicationDbContext CreateAppContext()
|
||||
{
|
||||
CreateContext();
|
||||
var serviceProvider = ConfigureDbServices(ConnectionString).BuildServiceProvider();
|
||||
var db = new ApplicationDbContext(serviceProvider, serviceProvider.GetService<IOptions<DbContextOptions>>());
|
||||
var db = DbUtil.Create<ApplicationDbContext>(ConnectionString);
|
||||
db.Database.EnsureCreated();
|
||||
return db;
|
||||
}
|
||||
|
|
@ -186,7 +162,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
|
|||
|
||||
public RoleManager<IdentityRole> CreateRoleManager(IdentityDbContext context)
|
||||
{
|
||||
var services = ConfigureDbServices(ConnectionString);
|
||||
var services = DbUtil.ConfigureDbServices(ConnectionString);
|
||||
services.AddIdentity().AddRoleStore(new RoleStore<IdentityRole>(context));
|
||||
return services.BuildServiceProvider().GetService<RoleManager<IdentityRole>>();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
{
|
||||
"dependencies": {
|
||||
"Microsoft.AspNet.Hosting": "1.0.0-*",
|
||||
"Microsoft.AspNet.Http": "1.0.0-*",
|
||||
"Microsoft.AspNet.Identity": "3.0.0-*",
|
||||
"Microsoft.AspNet.Identity.EntityFramework": "1.0.0-*",
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
// 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.
|
||||
|
||||
namespace Microsoft.AspNet.Identity.Test
|
||||
{
|
||||
public class ApplicationUser : IdentityUser { }
|
||||
}
|
||||
Loading…
Reference in New Issue