diff --git a/src/Microsoft.Extensions.Identity.Core/IdentityBuilder.cs b/src/Microsoft.Extensions.Identity.Core/IdentityBuilder.cs index 9df2aed7a0..c82e0efe9e 100644 --- a/src/Microsoft.Extensions.Identity.Core/IdentityBuilder.cs +++ b/src/Microsoft.Extensions.Identity.Core/IdentityBuilder.cs @@ -103,10 +103,10 @@ namespace Microsoft.AspNetCore.Identity /// /// Adds an for the . /// - /// The user type held in the store. + /// The user store type. /// The current instance. - public virtual IdentityBuilder AddUserStore() where TUser : class - => AddScoped(typeof(IUserStore<>).MakeGenericType(UserType), typeof(TUser)); + public virtual IdentityBuilder AddUserStore() where TStore : class + => AddScoped(typeof(IUserStore<>).MakeGenericType(UserType), typeof(TStore)); /// /// Adds a token provider. @@ -165,7 +165,6 @@ namespace Microsoft.AspNetCore.Identity public virtual IdentityBuilder AddRoles() where TRole : class { RoleType = typeof(TRole); - AddRoleStore(); AddRoleValidator>(); Services.TryAddScoped, RoleManager>(); return this; @@ -189,15 +188,15 @@ namespace Microsoft.AspNetCore.Identity /// /// Adds a for the . /// - /// The role type held in the store. + /// The role store. /// The current instance. - public virtual IdentityBuilder AddRoleStore() where TRole : class + public virtual IdentityBuilder AddRoleStore() where TStore : class { if (RoleType == null) { throw new InvalidOperationException(Resources.NoRoleType); } - return AddScoped(typeof(IRoleStore<>).MakeGenericType(RoleType), typeof(TRole)); + return AddScoped(typeof(IRoleStore<>).MakeGenericType(RoleType), typeof(TStore)); } /// diff --git a/test/Microsoft.AspNetCore.Identity.InMemory.Test/FunctionalTest.cs b/test/Microsoft.AspNetCore.Identity.InMemory.Test/FunctionalTest.cs index d98020a10d..6be648c38d 100644 --- a/test/Microsoft.AspNetCore.Identity.InMemory.Test/FunctionalTest.cs +++ b/test/Microsoft.AspNetCore.Identity.InMemory.Test/FunctionalTest.cs @@ -12,7 +12,6 @@ using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; diff --git a/test/Microsoft.AspNetCore.Identity.Test/IdentityBuilderTest.cs b/test/Microsoft.AspNetCore.Identity.Test/IdentityBuilderTest.cs index 0c3b8c7aa4..02f602c33b 100644 --- a/test/Microsoft.AspNetCore.Identity.Test/IdentityBuilderTest.cs +++ b/test/Microsoft.AspNetCore.Identity.Test/IdentityBuilderTest.cs @@ -6,7 +6,6 @@ using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; -using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -17,6 +16,31 @@ namespace Microsoft.AspNetCore.Identity.Test { public class IdentityBuilderTest { + [Fact] + public void AddRolesServicesAdded() + { + var services = new ServiceCollection(); + services.AddIdentityCore(o => { }) + .AddRoles() + .AddRoleStore(); + var sp = services.BuildServiceProvider(); + Assert.NotNull(sp.GetRequiredService>()); + Assert.IsType(sp.GetRequiredService>()); + Assert.NotNull(sp.GetRequiredService>()); + } + + [Fact] + public void AddRolesWithoutStoreWillError() + { + var services = new ServiceCollection(); + services.AddIdentityCore(o => { }) + .AddRoles(); + var sp = services.BuildServiceProvider(); + Assert.NotNull(sp.GetRequiredService>()); + Assert.Null(sp.GetService>()); + Assert.Throws(() => sp.GetService>()); + } + [Fact] public void CanOverrideUserStore()