diff --git a/src/Core/IdentityBuilder.cs b/src/Core/IdentityBuilder.cs index aa1f2af6..905bedc8 100644 --- a/src/Core/IdentityBuilder.cs +++ b/src/Core/IdentityBuilder.cs @@ -167,6 +167,7 @@ namespace Microsoft.AspNetCore.Identity RoleType = typeof(TRole); AddRoleValidator>(); Services.TryAddScoped>(); + Services.AddScoped(typeof(IUserClaimsPrincipalFactory<>).MakeGenericType(UserType), typeof(UserClaimsPrincipalFactory<,>).MakeGenericType(UserType, RoleType)); return this; } diff --git a/src/Identity/IdentityServiceCollectionExtensions.cs b/src/Identity/IdentityServiceCollectionExtensions.cs index fe655c31..b0d60def 100644 --- a/src/Identity/IdentityServiceCollectionExtensions.cs +++ b/src/Identity/IdentityServiceCollectionExtensions.cs @@ -117,6 +117,5 @@ namespace Microsoft.Extensions.DependencyInjection /// The services. public static IServiceCollection ConfigureExternalCookie(this IServiceCollection services, Action configure) => services.Configure(IdentityConstants.ExternalScheme, configure); - } } diff --git a/test/Identity.Test/IdentityBuilderTest.cs b/test/Identity.Test/IdentityBuilderTest.cs index 28305bf8..e5bc2004 100644 --- a/test/Identity.Test/IdentityBuilderTest.cs +++ b/test/Identity.Test/IdentityBuilderTest.cs @@ -22,11 +22,14 @@ namespace Microsoft.AspNetCore.Identity.Test var services = new ServiceCollection(); services.AddIdentityCore(o => { }) .AddRoles() + .AddUserStore() .AddRoleStore(); var sp = services.BuildServiceProvider(); Assert.NotNull(sp.GetRequiredService>()); Assert.IsType(sp.GetRequiredService>()); Assert.IsType>(sp.GetRequiredService>()); + Assert.NotNull(sp.GetRequiredService>()); + Assert.IsType>(sp.GetRequiredService>()); } [Fact] diff --git a/test/InMemory.Test/FunctionalTest.cs b/test/InMemory.Test/FunctionalTest.cs index 7571a792..7d3ab774 100644 --- a/test/InMemory.Test/FunctionalTest.cs +++ b/test/InMemory.Test/FunctionalTest.cs @@ -44,6 +44,27 @@ namespace Microsoft.AspNetCore.Identity.InMemory Assert.Null(transaction1.SetCookie); } + [Fact] + public async Task CookieContainsRoleClaim() + { + var clock = new TestClock(); + var server = CreateServer(null, null, null, testCore: true); + + var transaction1 = await SendAsync(server, "http://example.com/createMe"); + Assert.Equal(HttpStatusCode.OK, transaction1.Response.StatusCode); + Assert.Null(transaction1.SetCookie); + + var transaction2 = await SendAsync(server, "http://example.com/pwdLogin/false"); + Assert.Equal(HttpStatusCode.OK, transaction2.Response.StatusCode); + Assert.NotNull(transaction2.SetCookie); + Assert.DoesNotContain("; expires=", transaction2.SetCookie); + + var transaction3 = await SendAsync(server, "http://example.com/me", transaction2.CookieNameValue); + Assert.Equal("hao", FindClaimValue(transaction3, ClaimTypes.Name)); + Assert.Equal("role", FindClaimValue(transaction3, ClaimTypes.Role)); + Assert.Null(transaction3.SetCookie); + } + [Fact] public async Task CanCreateMeLoginAndCookieStopsWorkingAfterExpiration() { @@ -263,7 +284,7 @@ namespace Microsoft.AspNetCore.Identity.InMemory return me; } - private static TestServer CreateServer(Action configureServices = null, Func testpath = null, Uri baseAddress = null) + private static TestServer CreateServer(Action configureServices = null, Func testpath = null, Uri baseAddress = null, bool testCore = false) { var builder = new WebHostBuilder() .Configure(app => @@ -274,6 +295,7 @@ namespace Microsoft.AspNetCore.Identity.InMemory var req = context.Request; var res = context.Response; var userManager = context.RequestServices.GetRequiredService>(); + var roleManager = context.RequestServices.GetRequiredService>(); var signInManager = context.RequestServices.GetRequiredService>(); PathString remainder; if (req.Path == new PathString("/normal")) @@ -282,7 +304,16 @@ namespace Microsoft.AspNetCore.Identity.InMemory } else if (req.Path == new PathString("/createMe")) { - var result = await userManager.CreateAsync(new PocoUser("hao"), TestPassword); + var user = new PocoUser("hao"); + var result = await userManager.CreateAsync(user, TestPassword); + if (result.Succeeded) + { + result = await roleManager.CreateAsync(new PocoRole("role")); + } + if (result.Succeeded) + { + result = await userManager.AddToRoleAsync(user, "role"); + } res.StatusCode = result.Succeeded ? 200 : 500; } else if (req.Path == new PathString("/createSimple")) @@ -340,9 +371,21 @@ namespace Microsoft.AspNetCore.Identity.InMemory }) .ConfigureServices(services => { - services.AddIdentity().AddDefaultTokenProviders(); - services.AddSingleton, InMemoryStore>(); - services.AddSingleton, InMemoryStore>(); + if (testCore) + { + services.AddIdentityCore() + .AddRoles() + .AddSignInManager() + .AddDefaultTokenProviders(); + services.AddAuthentication(IdentityConstants.ApplicationScheme).AddIdentityCookies(); + } + else + { + services.AddIdentity().AddDefaultTokenProviders(); + } + var store = new InMemoryStore(); + services.AddSingleton>(store); + services.AddSingleton>(store); configureServices?.Invoke(services); }); var server = new TestServer(builder);