Add builder extension methods

This commit is contained in:
Hao Kung 2016-07-05 15:10:50 -07:00
parent c1b8f3abf3
commit 27f021ab93
2 changed files with 76 additions and 2 deletions

View File

@ -60,7 +60,7 @@ namespace Microsoft.AspNetCore.Identity
/// <summary>
/// Adds an <see cref="IUserValidator{TUser}"/> for the <seealso cref="UserType"/>.
/// </summary>
/// <typeparam name="T">The user type to validate.</typeparam>
/// <typeparam name="T">The user validator type.</typeparam>
/// <returns>The current <see cref="IdentityBuilder"/> instance.</returns>
public virtual IdentityBuilder AddUserValidator<T>() where T : class
{
@ -70,13 +70,23 @@ namespace Microsoft.AspNetCore.Identity
/// <summary>
/// Adds an <see cref="IRoleValidator{TRole}"/> for the <seealso cref="RoleType"/>.
/// </summary>
/// <typeparam name="T">The role type to validate.</typeparam>
/// <typeparam name="T">The role validator type.</typeparam>
/// <returns>The current <see cref="IdentityBuilder"/> instance.</returns>
public virtual IdentityBuilder AddRoleValidator<T>() where T : class
{
return AddScoped(typeof(IRoleValidator<>).MakeGenericType(RoleType), typeof(T));
}
/// <summary>
/// Adds an <see cref="IUserClaimsPrincipalFactory{TUser}"/> for the <seealso cref="UserType"/>.
/// </summary>
/// <typeparam name="T">The type of the claims principal factory.</typeparam>
/// <returns>The current <see cref="IdentityBuilder"/> instance.</returns>
public virtual IdentityBuilder AddClaimsPrincipalFactory<T>() where T : class
{
return AddScoped(typeof(IUserClaimsPrincipalFactory<>).MakeGenericType(UserType), typeof(T));
}
/// <summary>
/// Adds an <see cref="IdentityErrorDescriber"/>.
/// </summary>
@ -199,5 +209,23 @@ namespace Microsoft.AspNetCore.Identity
Services.AddScoped(typeof(TRoleManager), services => services.GetRequiredService(managerType));
return AddScoped(managerType, typeof(TRoleManager));
}
/// <summary>
/// Adds a <see cref="SignInManager{TUser}"/> for the <seealso cref="UserType"/>.
/// </summary>
/// <typeparam name="TSignInManager">The type of the sign in manager to add.</typeparam>
/// <returns>The current <see cref="IdentityBuilder"/> instance.</returns>
public virtual IdentityBuilder AddSignInManager<TSignInManager>() where TSignInManager : class
{
var managerType = typeof(SignInManager<>).MakeGenericType(UserType);
var customType = typeof(TSignInManager);
if (managerType == customType ||
!managerType.GetTypeInfo().IsAssignableFrom(customType.GetTypeInfo()))
{
throw new InvalidOperationException(Resources.FormatInvalidManagerType(customType.Name, "SignInManager", UserType.Name));
}
Services.AddScoped(typeof(TSignInManager), services => services.GetRequiredService(managerType));
return AddScoped(managerType, typeof(TSignInManager));
}
}
}

View File

@ -4,10 +4,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Xunit;
@ -34,6 +37,19 @@ namespace Microsoft.AspNetCore.Identity.Test
Assert.NotNull(thingy);
}
[Fact]
public void CanOverridePrincipalFactory()
{
var services = new ServiceCollection().AddLogging();
services.AddIdentity<TestUser, TestRole>()
.AddClaimsPrincipalFactory<MyClaimsPrincipalFactory>()
.AddUserManager<MyUserManager>()
.AddUserStore<NoopUserStore>()
.AddRoleStore<NoopRoleStore>();
var thingy = services.BuildServiceProvider().GetRequiredService<IUserClaimsPrincipalFactory<TestUser>>() as MyClaimsPrincipalFactory;
Assert.NotNull(thingy);
}
[Fact]
public void CanOverrideRoleValidator()
{
@ -83,6 +99,22 @@ namespace Microsoft.AspNetCore.Identity.Test
Assert.NotNull(myRoleManager);
}
[Fact]
public void CanOverrideSignInManager()
{
var services = new ServiceCollection();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>()
.AddLogging()
.AddIdentity<TestUser, TestRole>()
.AddUserStore<NoopUserStore>()
.AddRoleStore<NoopRoleStore>()
.AddUserManager<MyUserManager>()
.AddClaimsPrincipalFactory<MyClaimsPrincipalFactory>()
.AddSignInManager<MySignInManager>();
var myUserManager = services.BuildServiceProvider().GetRequiredService(typeof(SignInManager<TestUser>)) as MySignInManager;
Assert.NotNull(myUserManager);
}
[Fact]
public void EnsureDefaultServices()
{
@ -118,8 +150,10 @@ namespace Microsoft.AspNetCore.Identity.Test
var builder = services.AddIdentity<TestUser, TestRole>();
Assert.Throws<InvalidOperationException>(() => builder.AddUserManager<UserManager<TestUser>>());
Assert.Throws<InvalidOperationException>(() => builder.AddRoleManager<RoleManager<TestRole>>());
Assert.Throws<InvalidOperationException>(() => builder.AddSignInManager<SignInManager<TestRole>>());
Assert.Throws<InvalidOperationException>(() => builder.AddUserManager<object>());
Assert.Throws<InvalidOperationException>(() => builder.AddRoleManager<object>());
Assert.Throws<InvalidOperationException>(() => builder.AddSignInManager<object>());
}
[Fact]
@ -254,11 +288,23 @@ namespace Microsoft.AspNetCore.Identity.Test
}
}
private class MySignInManager : SignInManager<TestUser>
{
public MySignInManager(UserManager<TestUser> manager, IHttpContextAccessor context, IUserClaimsPrincipalFactory<TestUser> claimsFactory) : base(manager, context, claimsFactory, null, null) { }
}
private class MyUserManager : UserManager<TestUser>
{
public MyUserManager(IUserStore<TestUser> store) : base(store, null, null, null, null, null, null, null, null) { }
}
private class MyClaimsPrincipalFactory : UserClaimsPrincipalFactory<TestUser, TestRole>
{
public MyClaimsPrincipalFactory(UserManager<TestUser> userManager, RoleManager<TestRole> roleManager, IOptions<IdentityOptions> optionsAccessor) : base(userManager, roleManager, optionsAccessor)
{
}
}
private class MyRoleManager : RoleManager<TestRole>
{
public MyRoleManager(IRoleStore<TestRole> store,