Fix params name and AddRoles (#1406)
AddRoles should not be adding a store, that's done by the store implementation, it should only be registering the role services. Also fixed the TUser/TRole generic parameters names since they were actually the store types
This commit is contained in:
parent
3ec59d141f
commit
3a208fe41c
|
|
@ -103,10 +103,10 @@ namespace Microsoft.AspNetCore.Identity
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds an <see cref="IUserStore{TUser}"/> for the <seealso cref="UserType"/>.
|
/// Adds an <see cref="IUserStore{TUser}"/> for the <seealso cref="UserType"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TUser">The user type held in the store.</typeparam>
|
/// <typeparam name="TStore">The user store type.</typeparam>
|
||||||
/// <returns>The current <see cref="IdentityBuilder"/> instance.</returns>
|
/// <returns>The current <see cref="IdentityBuilder"/> instance.</returns>
|
||||||
public virtual IdentityBuilder AddUserStore<TUser>() where TUser : class
|
public virtual IdentityBuilder AddUserStore<TStore>() where TStore : class
|
||||||
=> AddScoped(typeof(IUserStore<>).MakeGenericType(UserType), typeof(TUser));
|
=> AddScoped(typeof(IUserStore<>).MakeGenericType(UserType), typeof(TStore));
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds a token provider.
|
/// Adds a token provider.
|
||||||
|
|
@ -165,7 +165,6 @@ namespace Microsoft.AspNetCore.Identity
|
||||||
public virtual IdentityBuilder AddRoles<TRole>() where TRole : class
|
public virtual IdentityBuilder AddRoles<TRole>() where TRole : class
|
||||||
{
|
{
|
||||||
RoleType = typeof(TRole);
|
RoleType = typeof(TRole);
|
||||||
AddRoleStore<TRole>();
|
|
||||||
AddRoleValidator<RoleValidator<TRole>>();
|
AddRoleValidator<RoleValidator<TRole>>();
|
||||||
Services.TryAddScoped<RoleManager<TRole>, RoleManager<TRole>>();
|
Services.TryAddScoped<RoleManager<TRole>, RoleManager<TRole>>();
|
||||||
return this;
|
return this;
|
||||||
|
|
@ -189,15 +188,15 @@ namespace Microsoft.AspNetCore.Identity
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds a <see cref="IRoleStore{TRole}"/> for the <seealso cref="RoleType"/>.
|
/// Adds a <see cref="IRoleStore{TRole}"/> for the <seealso cref="RoleType"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TRole">The role type held in the store.</typeparam>
|
/// <typeparam name="TStore">The role store.</typeparam>
|
||||||
/// <returns>The current <see cref="IdentityBuilder"/> instance.</returns>
|
/// <returns>The current <see cref="IdentityBuilder"/> instance.</returns>
|
||||||
public virtual IdentityBuilder AddRoleStore<TRole>() where TRole : class
|
public virtual IdentityBuilder AddRoleStore<TStore>() where TStore : class
|
||||||
{
|
{
|
||||||
if (RoleType == null)
|
if (RoleType == null)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException(Resources.NoRoleType);
|
throw new InvalidOperationException(Resources.NoRoleType);
|
||||||
}
|
}
|
||||||
return AddScoped(typeof(IRoleStore<>).MakeGenericType(RoleType), typeof(TRole));
|
return AddScoped(typeof(IRoleStore<>).MakeGenericType(RoleType), typeof(TStore));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,6 @@ using System.Threading.Tasks;
|
||||||
using System.Xml;
|
using System.Xml;
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
using Microsoft.AspNetCore.Authentication;
|
using Microsoft.AspNetCore.Authentication;
|
||||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Builder;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
@ -17,6 +16,31 @@ namespace Microsoft.AspNetCore.Identity.Test
|
||||||
{
|
{
|
||||||
public class IdentityBuilderTest
|
public class IdentityBuilderTest
|
||||||
{
|
{
|
||||||
|
[Fact]
|
||||||
|
public void AddRolesServicesAdded()
|
||||||
|
{
|
||||||
|
var services = new ServiceCollection();
|
||||||
|
services.AddIdentityCore<TestUser>(o => { })
|
||||||
|
.AddRoles<TestRole>()
|
||||||
|
.AddRoleStore<NoopRoleStore>();
|
||||||
|
var sp = services.BuildServiceProvider();
|
||||||
|
Assert.NotNull(sp.GetRequiredService<IRoleValidator<TestRole>>());
|
||||||
|
Assert.IsType<NoopRoleStore>(sp.GetRequiredService<IRoleStore<TestRole>>());
|
||||||
|
Assert.NotNull(sp.GetRequiredService<RoleManager<TestRole>>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void AddRolesWithoutStoreWillError()
|
||||||
|
{
|
||||||
|
var services = new ServiceCollection();
|
||||||
|
services.AddIdentityCore<TestUser>(o => { })
|
||||||
|
.AddRoles<TestRole>();
|
||||||
|
var sp = services.BuildServiceProvider();
|
||||||
|
Assert.NotNull(sp.GetRequiredService<IRoleValidator<TestRole>>());
|
||||||
|
Assert.Null(sp.GetService<IRoleStore<TestRole>>());
|
||||||
|
Assert.Throws<InvalidOperationException>(() => sp.GetService<RoleManager<TestRole>>());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void CanOverrideUserStore()
|
public void CanOverrideUserStore()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue