Move builder extensions to IdentityBuilder
Also correctly take TContext instead of looking for DbContext as a service
This commit is contained in:
parent
6432655d26
commit
2d1c1c177d
|
|
@ -1,28 +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 Microsoft.AspNet.Identity;
|
|
||||||
using Microsoft.AspNet.Identity.Entity;
|
|
||||||
using Microsoft.Data.Entity;
|
|
||||||
|
|
||||||
namespace Microsoft.Framework.DependencyInjection
|
|
||||||
{
|
|
||||||
public static class EntityServiceCollectionExtensions
|
|
||||||
{
|
|
||||||
public static ServiceCollection AddEntity<TUser>(this ServiceCollection services)
|
|
||||||
where TUser : User
|
|
||||||
{
|
|
||||||
services.AddScoped<IUserStore<TUser>, UserStore<TUser>>();
|
|
||||||
services.AddScoped<UserManager<TUser>>();
|
|
||||||
return services;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ServiceCollection AddEntity<TUser, TContext>(this ServiceCollection services)
|
|
||||||
where TUser : User where TContext : DbContext
|
|
||||||
{
|
|
||||||
services.AddEntity<TUser>();
|
|
||||||
services.AddScoped<DbContext, TContext>();
|
|
||||||
return services;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
// 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.Entity;
|
using Microsoft.AspNet.Identity.Entity;
|
||||||
|
using Microsoft.Data.Entity;
|
||||||
using Microsoft.Framework.DependencyInjection;
|
using Microsoft.Framework.DependencyInjection;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Identity
|
namespace Microsoft.AspNet.Identity
|
||||||
|
|
@ -26,5 +27,13 @@ namespace Microsoft.AspNet.Identity
|
||||||
builder.Services.AddScoped<UserManager<TUser>>();
|
builder.Services.AddScoped<UserManager<TUser>>();
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
public static IdentityBuilder<TUser, IdentityRole> AddEntity<TUser, TContext>(this IdentityBuilder<TUser, IdentityRole> builder)
|
||||||
|
where TUser : User where TContext : DbContext
|
||||||
|
{
|
||||||
|
builder.Services.AddScoped<IUserStore<TUser>, UserStore<TUser, TContext>>();
|
||||||
|
builder.Services.AddScoped<UserManager<TUser>>();
|
||||||
|
builder.Services.AddScoped<TContext>();
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
// 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;
|
||||||
|
using Microsoft.AspNet.Identity.Entity;
|
||||||
|
using Microsoft.Data.Entity;
|
||||||
|
|
||||||
|
namespace Microsoft.Framework.DependencyInjection
|
||||||
|
{
|
||||||
|
public static class IdentityEntityServiceCollectionExtensions
|
||||||
|
{
|
||||||
|
//public static IdentityBuilder<TUser, IdentityRole> AddEntity<TUser>(this ServiceCollection services)
|
||||||
|
// where TUser : User
|
||||||
|
//{
|
||||||
|
// services.AddScoped<IUserStore<TUser>, UserStore<TUser>>();
|
||||||
|
// services.AddScoped<UserManager<TUser>>();
|
||||||
|
// return services;
|
||||||
|
//}
|
||||||
|
|
||||||
|
//public static ServiceCollection AddEntity<TUser, TContext>(this ServiceCollection services)
|
||||||
|
// where TUser : User where TContext : DbContext
|
||||||
|
//{
|
||||||
|
// services.AddEntity<TUser>();
|
||||||
|
// services.AddScoped<DbContext, TContext>();
|
||||||
|
// return services;
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -12,20 +12,22 @@ namespace Microsoft.AspNet.Identity.Entity
|
||||||
{
|
{
|
||||||
public IdentitySqlContext() { }
|
public IdentitySqlContext() { }
|
||||||
public IdentitySqlContext(IServiceProvider serviceProvider) : base(serviceProvider) { }
|
public IdentitySqlContext(IServiceProvider serviceProvider) : base(serviceProvider) { }
|
||||||
|
public IdentitySqlContext(ImmutableDbContextOptions options) : base(options) { }
|
||||||
|
public IdentitySqlContext(IServiceProvider serviceProvider, ImmutableDbContextOptions options) : base(serviceProvider, options) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class IdentitySqlContext<TUser> : DbContext
|
public class IdentitySqlContext<TUser> : DbContext
|
||||||
where TUser : User
|
where TUser : User
|
||||||
{
|
{
|
||||||
|
|
||||||
public DbSet<TUser> Users { get; set; }
|
public DbSet<TUser> Users { get; set; }
|
||||||
public DbSet<IdentityUserClaim> UserClaims { get; set; }
|
public DbSet<IdentityUserClaim> UserClaims { get; set; }
|
||||||
//public DbSet<TRole> Roles { get; set; }
|
//public DbSet<TRole> Roles { get; set; }
|
||||||
|
|
||||||
public IdentitySqlContext(IServiceProvider serviceProvider)
|
|
||||||
: base(serviceProvider) { }
|
|
||||||
|
|
||||||
public IdentitySqlContext() { }
|
public IdentitySqlContext() { }
|
||||||
|
public IdentitySqlContext(IServiceProvider serviceProvider) : base(serviceProvider) { }
|
||||||
|
public IdentitySqlContext(ImmutableDbContextOptions options) : base(options) { }
|
||||||
|
public IdentitySqlContext(IServiceProvider serviceProvider, ImmutableDbContextOptions options) : base(serviceProvider, options) { }
|
||||||
|
|
||||||
|
|
||||||
protected override void OnConfiguring(DbContextOptions builder)
|
protected override void OnConfiguring(DbContextOptions builder)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -13,23 +13,22 @@ using Microsoft.Data.Entity;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Identity.Entity
|
namespace Microsoft.AspNet.Identity.Entity
|
||||||
{
|
{
|
||||||
// Real Sql implementation
|
public class UserStore<TUser> : UserStore<TUser, DbContext> where TUser : User
|
||||||
public class SqlUserStore :
|
|
||||||
UserStore<User>
|
|
||||||
{
|
{
|
||||||
public SqlUserStore(DbContext context) : base(context) { }
|
public UserStore(DbContext context) : base(context) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class UserStore<TUser> :
|
public class UserStore<TUser, TContext> :
|
||||||
//IUserRoleStore<TUser>,
|
//IUserRoleStore<TUser>,
|
||||||
IUserPasswordStore<TUser>,
|
IUserPasswordStore<TUser>,
|
||||||
IQueryableUserStore<TUser>,
|
IQueryableUserStore<TUser>,
|
||||||
IUserClaimStore<TUser>
|
IUserClaimStore<TUser>
|
||||||
where TUser : User
|
where TUser : User
|
||||||
|
where TContext : DbContext
|
||||||
{
|
{
|
||||||
private bool _disposed;
|
private bool _disposed;
|
||||||
|
|
||||||
public UserStore(DbContext context)
|
public UserStore(TContext context)
|
||||||
{
|
{
|
||||||
if (context == null)
|
if (context == null)
|
||||||
{
|
{
|
||||||
|
|
@ -39,7 +38,7 @@ namespace Microsoft.AspNet.Identity.Entity
|
||||||
AutoSaveChanges = true;
|
AutoSaveChanges = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DbContext Context { get; private set; }
|
public TContext Context { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// If true will call SaveChanges after CreateAsync/UpdateAsync/DeleteAsync
|
/// If true will call SaveChanges after CreateAsync/UpdateAsync/DeleteAsync
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
// 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.Security;
|
||||||
|
using Microsoft.Framework.DependencyInjection;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.Identity
|
||||||
|
{
|
||||||
|
public static class IdentityBuilderExtensions
|
||||||
|
{
|
||||||
|
public static IdentityBuilder<TUser, IdentityRole> AddSecurity<TUser>(this IdentityBuilder<TUser, IdentityRole> builder)
|
||||||
|
where TUser : class
|
||||||
|
{
|
||||||
|
builder.Services.AddScoped<SignInManager<TUser>>();
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -8,7 +8,7 @@ namespace Microsoft.Framework.DependencyInjection
|
||||||
{
|
{
|
||||||
public static class IdentityServiceCollectionExtensions
|
public static class IdentityServiceCollectionExtensions
|
||||||
{
|
{
|
||||||
public static ServiceCollection AddIdentity<TUser, TRole>(this ServiceCollection services)
|
public static IdentityBuilder<TUser, TRole> AddIdentity<TUser, TRole>(this ServiceCollection services)
|
||||||
where TUser : class
|
where TUser : class
|
||||||
where TRole : class
|
where TRole : class
|
||||||
{
|
{
|
||||||
|
|
@ -16,25 +16,26 @@ namespace Microsoft.Framework.DependencyInjection
|
||||||
services.Add(IdentityServices.GetDefaultRoleServices<TRole>());
|
services.Add(IdentityServices.GetDefaultRoleServices<TRole>());
|
||||||
services.AddTransient<IOptionsSetup<IdentityOptions>, IdentityOptionsSetup>();
|
services.AddTransient<IOptionsSetup<IdentityOptions>, IdentityOptionsSetup>();
|
||||||
services.AddSingleton<IOptionsAccessor<IdentityOptions>, OptionsAccessor<IdentityOptions>>();
|
services.AddSingleton<IOptionsAccessor<IdentityOptions>, OptionsAccessor<IdentityOptions>>();
|
||||||
return services;
|
return new IdentityBuilder<TUser, TRole>(services);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ServiceCollection AddIdentity<TUser, TRole>(this ServiceCollection services, Action<IdentityBuilder<TUser, TRole>> actionBuilder)
|
public static IdentityBuilder<TUser, TRole> AddIdentity<TUser, TRole>(this ServiceCollection services, Action<IdentityBuilder<TUser, TRole>> actionBuilder)
|
||||||
where TUser : class
|
where TUser : class
|
||||||
where TRole : class
|
where TRole : class
|
||||||
{
|
{
|
||||||
services.AddIdentity<TUser, TRole>();
|
services.AddIdentity<TUser, TRole>();
|
||||||
actionBuilder(new IdentityBuilder<TUser, TRole>(services));
|
var builder = new IdentityBuilder<TUser, TRole>(services);
|
||||||
return services;
|
actionBuilder(builder);
|
||||||
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ServiceCollection AddIdentity<TUser>(this ServiceCollection services)
|
public static IdentityBuilder<TUser, IdentityRole> AddIdentity<TUser>(this ServiceCollection services)
|
||||||
where TUser : class
|
where TUser : class
|
||||||
{
|
{
|
||||||
return services.AddIdentity<TUser, IdentityRole>();
|
return services.AddIdentity<TUser, IdentityRole>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ServiceCollection AddIdentity<TUser>(this ServiceCollection services, Action<IdentityBuilder<TUser, IdentityRole>> actionBuilder)
|
public static IdentityBuilder<TUser, IdentityRole> AddIdentity<TUser>(this ServiceCollection services, Action<IdentityBuilder<TUser, IdentityRole>> actionBuilder)
|
||||||
where TUser : class
|
where TUser : class
|
||||||
{
|
{
|
||||||
return services.AddIdentity<TUser, IdentityRole>(actionBuilder);
|
return services.AddIdentity<TUser, IdentityRole>(actionBuilder);
|
||||||
|
|
|
||||||
|
|
@ -22,14 +22,6 @@ namespace Microsoft.AspNet.Identity.Entity.Test
|
||||||
public class SqlUserStoreTest
|
public class SqlUserStoreTest
|
||||||
{
|
{
|
||||||
public class ApplicationUser : User { }
|
public class ApplicationUser : User { }
|
||||||
public class ApplicationUserManager : UserManager<ApplicationUser>
|
|
||||||
{
|
|
||||||
public ApplicationUserManager(IServiceProvider services, IUserStore<ApplicationUser> store, IOptionsAccessor<IdentityOptions> options) : base(services, store, options) { }
|
|
||||||
}
|
|
||||||
public class ApplicationRoleManager : RoleManager<EntityRole>
|
|
||||||
{
|
|
||||||
public ApplicationRoleManager(IServiceProvider services, IRoleStore<EntityRole> store) : base(services, store) { }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class ApplicationDbContext : IdentitySqlContext<ApplicationUser>
|
public class ApplicationDbContext : IdentitySqlContext<ApplicationUser>
|
||||||
{
|
{
|
||||||
|
|
@ -39,25 +31,17 @@ namespace Microsoft.AspNet.Identity.Entity.Test
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task EnsureStartupUsageWorks()
|
public async Task EnsureStartupUsageWorks()
|
||||||
{
|
{
|
||||||
IBuilder builder = new Microsoft.AspNet.Builder.Builder(new ServiceCollection().BuildServiceProvider());
|
EnsureDatabase();
|
||||||
|
IBuilder builder = new Builder.Builder(new ServiceCollection().BuildServiceProvider());
|
||||||
//builder.UseServices(services => services.AddIdentity<ApplicationUser>(s =>
|
|
||||||
// s.AddEntity<ApplicationDbContext>()
|
|
||||||
//{
|
|
||||||
|
|
||||||
builder.UseServices(services =>
|
builder.UseServices(services =>
|
||||||
{
|
{
|
||||||
services.AddEntityFramework();
|
services.AddEntityFramework().AddSqlServer();
|
||||||
services.AddInstance<DbContext>(CreateAppContext());
|
services.AddIdentity<ApplicationUser>().AddEntity<ApplicationUser, ApplicationDbContext>();
|
||||||
services.AddIdentity<ApplicationUser>(s =>
|
|
||||||
{
|
|
||||||
s.AddEntity();
|
|
||||||
s.AddUserManager<ApplicationUserManager>();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
var userStore = builder.ApplicationServices.GetService<IUserStore<ApplicationUser>>();
|
var userStore = builder.ApplicationServices.GetService<IUserStore<ApplicationUser>>();
|
||||||
var userManager = builder.ApplicationServices.GetService<ApplicationUserManager>();
|
var userManager = builder.ApplicationServices.GetService<UserManager<ApplicationUser>>();
|
||||||
|
|
||||||
Assert.NotNull(userStore);
|
Assert.NotNull(userStore);
|
||||||
Assert.NotNull(userManager);
|
Assert.NotNull(userManager);
|
||||||
|
|
@ -88,14 +72,16 @@ namespace Microsoft.AspNet.Identity.Entity.Test
|
||||||
var serviceProvider = services.BuildServiceProvider();
|
var serviceProvider = services.BuildServiceProvider();
|
||||||
|
|
||||||
var db = new IdentitySqlContext(serviceProvider);
|
var db = new IdentitySqlContext(serviceProvider);
|
||||||
|
|
||||||
// TODO: Recreate DB, doesn't support String ID or Identity context yet
|
|
||||||
db.Database.EnsureCreated();
|
db.Database.EnsureCreated();
|
||||||
|
|
||||||
// TODO: CreateAsync DB?
|
|
||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void EnsureDatabase()
|
||||||
|
{
|
||||||
|
CreateContext();
|
||||||
|
}
|
||||||
|
|
||||||
public static ApplicationDbContext CreateAppContext()
|
public static ApplicationDbContext CreateAppContext()
|
||||||
{
|
{
|
||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection();
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ namespace Microsoft.AspNet.Identity.Security.Test
|
||||||
[InlineData(false)]
|
[InlineData(false)]
|
||||||
public async Task VerifyAccountControllerSignIn(bool isPersistent)
|
public async Task VerifyAccountControllerSignIn(bool isPersistent)
|
||||||
{
|
{
|
||||||
IBuilder app = new Microsoft.AspNet.Builder.Builder(new ServiceCollection().BuildServiceProvider());
|
IBuilder app = new Builder.Builder(new ServiceCollection().BuildServiceProvider());
|
||||||
app.UseCookieAuthentication(new CookieAuthenticationOptions
|
app.UseCookieAuthentication(new CookieAuthenticationOptions
|
||||||
{
|
{
|
||||||
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie
|
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie
|
||||||
|
|
@ -48,10 +48,7 @@ namespace Microsoft.AspNet.Identity.Security.Test
|
||||||
services.AddIdentity<ApplicationUser, IdentityRole>(s =>
|
services.AddIdentity<ApplicationUser, IdentityRole>(s =>
|
||||||
{
|
{
|
||||||
s.AddInMemory();
|
s.AddInMemory();
|
||||||
s.AddUserManager<ApplicationUserManager>();
|
}).AddSecurity<ApplicationUser>();
|
||||||
s.AddRoleManager<ApplicationRoleManager>();
|
|
||||||
});
|
|
||||||
services.AddTransient<ApplicationSignInManager>();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
|
|
@ -60,8 +57,8 @@ namespace Microsoft.AspNet.Identity.Security.Test
|
||||||
UserName = "Yolo"
|
UserName = "Yolo"
|
||||||
};
|
};
|
||||||
const string password = "Yol0Sw@g!";
|
const string password = "Yol0Sw@g!";
|
||||||
var userManager = app.ApplicationServices.GetService<ApplicationUserManager>();
|
var userManager = app.ApplicationServices.GetService<UserManager<ApplicationUser>>();
|
||||||
var signInManager = app.ApplicationServices.GetService<ApplicationSignInManager>();
|
var signInManager = app.ApplicationServices.GetService<SignInManager<ApplicationUser>>();
|
||||||
|
|
||||||
IdentityResultAssert.IsSuccess(await userManager.CreateAsync(user, password));
|
IdentityResultAssert.IsSuccess(await userManager.CreateAsync(user, password));
|
||||||
var result = await signInManager.PasswordSignInAsync(user.UserName, password, isPersistent, false);
|
var result = await signInManager.PasswordSignInAsync(user.UserName, password, isPersistent, false);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue