Fix issue with role claim missing in AddIdentityCore (#1832)

This commit is contained in:
Hao Kung 2018-07-11 12:39:51 -07:00 коммит произвёл GitHub
Родитель 3e27f18127
Коммит b2cca31774
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 52 добавлений и 6 удалений

Просмотреть файл

@ -167,6 +167,7 @@ namespace Microsoft.AspNetCore.Identity
RoleType = typeof(TRole);
AddRoleValidator<RoleValidator<TRole>>();
Services.TryAddScoped<RoleManager<TRole>>();
Services.AddScoped(typeof(IUserClaimsPrincipalFactory<>).MakeGenericType(UserType), typeof(UserClaimsPrincipalFactory<,>).MakeGenericType(UserType, RoleType));
return this;
}

Просмотреть файл

@ -117,6 +117,5 @@ namespace Microsoft.Extensions.DependencyInjection
/// <returns>The services.</returns>
public static IServiceCollection ConfigureExternalCookie(this IServiceCollection services, Action<CookieAuthenticationOptions> configure)
=> services.Configure(IdentityConstants.ExternalScheme, configure);
}
}

Просмотреть файл

@ -22,11 +22,14 @@ namespace Microsoft.AspNetCore.Identity.Test
var services = new ServiceCollection();
services.AddIdentityCore<PocoUser>(o => { })
.AddRoles<PocoRole>()
.AddUserStore<NoopUserStore>()
.AddRoleStore<NoopRoleStore>();
var sp = services.BuildServiceProvider();
Assert.NotNull(sp.GetRequiredService<IRoleValidator<PocoRole>>());
Assert.IsType<NoopRoleStore>(sp.GetRequiredService<IRoleStore<PocoRole>>());
Assert.IsType<RoleManager<PocoRole>>(sp.GetRequiredService<RoleManager<PocoRole>>());
Assert.NotNull(sp.GetRequiredService<RoleManager<PocoRole>>());
Assert.IsType<UserClaimsPrincipalFactory<PocoUser, PocoRole>>(sp.GetRequiredService<IUserClaimsPrincipalFactory<PocoUser>>());
}
[Fact]

Просмотреть файл

@ -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<IServiceCollection> configureServices = null, Func<HttpContext, Task> testpath = null, Uri baseAddress = null)
private static TestServer CreateServer(Action<IServiceCollection> configureServices = null, Func<HttpContext, Task> 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<UserManager<PocoUser>>();
var roleManager = context.RequestServices.GetRequiredService<RoleManager<PocoRole>>();
var signInManager = context.RequestServices.GetRequiredService<SignInManager<PocoUser>>();
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<PocoUser, PocoRole>().AddDefaultTokenProviders();
services.AddSingleton<IUserStore<PocoUser>, InMemoryStore<PocoUser, PocoRole>>();
services.AddSingleton<IRoleStore<PocoRole>, InMemoryStore<PocoUser, PocoRole>>();
if (testCore)
{
services.AddIdentityCore<PocoUser>()
.AddRoles<PocoRole>()
.AddSignInManager()
.AddDefaultTokenProviders();
services.AddAuthentication(IdentityConstants.ApplicationScheme).AddIdentityCookies();
}
else
{
services.AddIdentity<PocoUser, PocoRole>().AddDefaultTokenProviders();
}
var store = new InMemoryStore<PocoUser, PocoRole>();
services.AddSingleton<IUserStore<PocoUser>>(store);
services.AddSingleton<IRoleStore<PocoRole>>(store);
configureServices?.Invoke(services);
});
var server = new TestServer(builder);