Updates to AddManager behavior
This commit is contained in:
parent
902de1599c
commit
4e0000163b
|
|
@ -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 System;
|
using System;
|
||||||
|
using System.Reflection;
|
||||||
using Microsoft.Framework.DependencyInjection;
|
using Microsoft.Framework.DependencyInjection;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Identity
|
namespace Microsoft.AspNet.Identity
|
||||||
|
|
@ -159,7 +160,15 @@ namespace Microsoft.AspNet.Identity
|
||||||
/// <returns>The <see cref="IdentityBuilder"/>.</returns>
|
/// <returns>The <see cref="IdentityBuilder"/>.</returns>
|
||||||
public virtual IdentityBuilder AddUserManager<TUserManager>() where TUserManager : class
|
public virtual IdentityBuilder AddUserManager<TUserManager>() where TUserManager : class
|
||||||
{
|
{
|
||||||
return AddScoped(typeof(UserManager<>).MakeGenericType(UserType), typeof(TUserManager));
|
var userManagerType = typeof(UserManager<>).MakeGenericType(UserType);
|
||||||
|
var customType = typeof(TUserManager);
|
||||||
|
if (userManagerType == customType ||
|
||||||
|
!userManagerType.GetTypeInfo().IsAssignableFrom(customType.GetTypeInfo()))
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException(Resources.FormatInvalidManagerType(customType.Name, "UserManager", UserType.Name));
|
||||||
|
}
|
||||||
|
Services.AddScoped(customType, services => services.GetRequiredService(userManagerType));
|
||||||
|
return AddScoped(userManagerType, customType);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -169,7 +178,15 @@ namespace Microsoft.AspNet.Identity
|
||||||
/// <returns>The <see cref="IdentityBuilder"/>.</returns>
|
/// <returns>The <see cref="IdentityBuilder"/>.</returns>
|
||||||
public virtual IdentityBuilder AddRoleManager<TRoleManager>() where TRoleManager : class
|
public virtual IdentityBuilder AddRoleManager<TRoleManager>() where TRoleManager : class
|
||||||
{
|
{
|
||||||
return AddScoped(typeof(RoleManager<>).MakeGenericType(RoleType), typeof(TRoleManager));
|
var managerType = typeof(RoleManager<>).MakeGenericType(RoleType);
|
||||||
|
var customType = typeof(TRoleManager);
|
||||||
|
if (managerType == customType ||
|
||||||
|
!managerType.GetTypeInfo().IsAssignableFrom(customType.GetTypeInfo()))
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException(Resources.FormatInvalidManagerType(customType.Name, "RoleManager", RoleType.Name));
|
||||||
|
}
|
||||||
|
Services.AddScoped(typeof(TRoleManager), services => services.GetRequiredService(managerType));
|
||||||
|
return AddScoped(managerType, typeof(TRoleManager));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -122,6 +122,22 @@ namespace Microsoft.AspNet.Identity
|
||||||
return string.Format(CultureInfo.CurrentCulture, GetString("InvalidEmail"), p0);
|
return string.Format(CultureInfo.CurrentCulture, GetString("InvalidEmail"), p0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Type {0} must be derived from {1}<{2}>.
|
||||||
|
/// </summary>
|
||||||
|
internal static string InvalidManagerType
|
||||||
|
{
|
||||||
|
get { return GetString("InvalidManagerType"); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Type {0} must be derived from {1}<{2}>.
|
||||||
|
/// </summary>
|
||||||
|
internal static string FormatInvalidManagerType(object p0, object p1, object p2)
|
||||||
|
{
|
||||||
|
return string.Format(CultureInfo.CurrentCulture, GetString("InvalidManagerType"), p0, p1, p2);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The provided PasswordHasherCompatibilityMode is invalid.
|
/// The provided PasswordHasherCompatibilityMode is invalid.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -145,6 +145,10 @@
|
||||||
<value>Email '{0}' is invalid.</value>
|
<value>Email '{0}' is invalid.</value>
|
||||||
<comment>Invalid email</comment>
|
<comment>Invalid email</comment>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="InvalidManagerType" xml:space="preserve">
|
||||||
|
<value>Type {0} must be derived from {1}<{2}>.</value>
|
||||||
|
<comment>Error when the manager type is not derived correctly</comment>
|
||||||
|
</data>
|
||||||
<data name="InvalidPasswordHasherCompatibilityMode" xml:space="preserve">
|
<data name="InvalidPasswordHasherCompatibilityMode" xml:space="preserve">
|
||||||
<value>The provided PasswordHasherCompatibilityMode is invalid.</value>
|
<value>The provided PasswordHasherCompatibilityMode is invalid.</value>
|
||||||
<comment>Error when the password hasher doesn't understand the format it's being asked to produce.</comment>
|
<comment>Error when the password hasher doesn't understand the format it's being asked to produce.</comment>
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,46 @@ namespace Microsoft.AspNet.Identity.Test
|
||||||
Assert.NotNull(manager.Options);
|
Assert.NotNull(manager.Options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void AddUserManagerWithCustomManagerReturnsSameInstance()
|
||||||
|
{
|
||||||
|
var services = new ServiceCollection()
|
||||||
|
.AddTransient<IUserStore<TestUser>, NoopUserStore>()
|
||||||
|
.AddSingleton<IHttpContextAccessor, HttpContextAccessor>()
|
||||||
|
.AddLogging();
|
||||||
|
services.AddIdentity<TestUser, TestRole>()
|
||||||
|
.AddUserManager<CustomUserManager>()
|
||||||
|
.AddRoleManager<CustomRoleManager>();
|
||||||
|
var provider = services.BuildServiceProvider();
|
||||||
|
Assert.Same(provider.GetRequiredService<UserManager<TestUser>>(),
|
||||||
|
provider.GetRequiredService<CustomUserManager>());
|
||||||
|
Assert.Same(provider.GetRequiredService<RoleManager<TestRole>>(),
|
||||||
|
provider.GetRequiredService<CustomRoleManager>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void AddManagerWithWrongTypesThrows()
|
||||||
|
{
|
||||||
|
var services = new ServiceCollection();
|
||||||
|
var builder = services.AddIdentity<TestUser, TestRole>();
|
||||||
|
Assert.Throws<InvalidOperationException>(() => builder.AddUserManager<UserManager<TestUser>>());
|
||||||
|
Assert.Throws<InvalidOperationException>(() => builder.AddRoleManager<RoleManager<TestRole>>());
|
||||||
|
Assert.Throws<InvalidOperationException>(() => builder.AddUserManager<object>());
|
||||||
|
Assert.Throws<InvalidOperationException>(() => builder.AddRoleManager<object>());
|
||||||
|
}
|
||||||
|
|
||||||
|
public class CustomUserManager : UserManager<TestUser>
|
||||||
|
{
|
||||||
|
public CustomUserManager() : base(new Mock<IUserStore<TestUser>>().Object, null, null, null, null, null, null, null, null, null)
|
||||||
|
{ }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class CustomRoleManager : RoleManager<TestRole>
|
||||||
|
{
|
||||||
|
public CustomRoleManager() : base(new Mock<IRoleStore<TestRole>>().Object, null, null, null, null, null)
|
||||||
|
{ }
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task CreateCallsStore()
|
public async Task CreateCallsStore()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue