Merge pull request #1873 from dotnet-maestro-bot/merge/release/2.2-to-master
[automated] Merge branch 'release/2.2' => 'master'
This commit is contained in:
commit
8ef14785a4
|
|
@ -167,6 +167,7 @@ namespace Microsoft.AspNetCore.Identity
|
||||||
RoleType = typeof(TRole);
|
RoleType = typeof(TRole);
|
||||||
AddRoleValidator<RoleValidator<TRole>>();
|
AddRoleValidator<RoleValidator<TRole>>();
|
||||||
Services.TryAddScoped<RoleManager<TRole>>();
|
Services.TryAddScoped<RoleManager<TRole>>();
|
||||||
|
Services.AddScoped(typeof(IUserClaimsPrincipalFactory<>).MakeGenericType(UserType), typeof(UserClaimsPrincipalFactory<,>).MakeGenericType(UserType, RoleType));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -117,6 +117,5 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
/// <returns>The services.</returns>
|
/// <returns>The services.</returns>
|
||||||
public static IServiceCollection ConfigureExternalCookie(this IServiceCollection services, Action<CookieAuthenticationOptions> configure)
|
public static IServiceCollection ConfigureExternalCookie(this IServiceCollection services, Action<CookieAuthenticationOptions> configure)
|
||||||
=> services.Configure(IdentityConstants.ExternalScheme, configure);
|
=> services.Configure(IdentityConstants.ExternalScheme, configure);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,11 +22,14 @@ namespace Microsoft.AspNetCore.Identity.Test
|
||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection();
|
||||||
services.AddIdentityCore<PocoUser>(o => { })
|
services.AddIdentityCore<PocoUser>(o => { })
|
||||||
.AddRoles<PocoRole>()
|
.AddRoles<PocoRole>()
|
||||||
|
.AddUserStore<NoopUserStore>()
|
||||||
.AddRoleStore<NoopRoleStore>();
|
.AddRoleStore<NoopRoleStore>();
|
||||||
var sp = services.BuildServiceProvider();
|
var sp = services.BuildServiceProvider();
|
||||||
Assert.NotNull(sp.GetRequiredService<IRoleValidator<PocoRole>>());
|
Assert.NotNull(sp.GetRequiredService<IRoleValidator<PocoRole>>());
|
||||||
Assert.IsType<NoopRoleStore>(sp.GetRequiredService<IRoleStore<PocoRole>>());
|
Assert.IsType<NoopRoleStore>(sp.GetRequiredService<IRoleStore<PocoRole>>());
|
||||||
Assert.IsType<RoleManager<PocoRole>>(sp.GetRequiredService<RoleManager<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]
|
[Fact]
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,27 @@ namespace Microsoft.AspNetCore.Identity.InMemory
|
||||||
Assert.Null(transaction1.SetCookie);
|
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]
|
[Fact]
|
||||||
public async Task CanCreateMeLoginAndCookieStopsWorkingAfterExpiration()
|
public async Task CanCreateMeLoginAndCookieStopsWorkingAfterExpiration()
|
||||||
{
|
{
|
||||||
|
|
@ -263,7 +284,7 @@ namespace Microsoft.AspNetCore.Identity.InMemory
|
||||||
return me;
|
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()
|
var builder = new WebHostBuilder()
|
||||||
.Configure(app =>
|
.Configure(app =>
|
||||||
|
|
@ -274,6 +295,7 @@ namespace Microsoft.AspNetCore.Identity.InMemory
|
||||||
var req = context.Request;
|
var req = context.Request;
|
||||||
var res = context.Response;
|
var res = context.Response;
|
||||||
var userManager = context.RequestServices.GetRequiredService<UserManager<PocoUser>>();
|
var userManager = context.RequestServices.GetRequiredService<UserManager<PocoUser>>();
|
||||||
|
var roleManager = context.RequestServices.GetRequiredService<RoleManager<PocoRole>>();
|
||||||
var signInManager = context.RequestServices.GetRequiredService<SignInManager<PocoUser>>();
|
var signInManager = context.RequestServices.GetRequiredService<SignInManager<PocoUser>>();
|
||||||
PathString remainder;
|
PathString remainder;
|
||||||
if (req.Path == new PathString("/normal"))
|
if (req.Path == new PathString("/normal"))
|
||||||
|
|
@ -282,7 +304,16 @@ namespace Microsoft.AspNetCore.Identity.InMemory
|
||||||
}
|
}
|
||||||
else if (req.Path == new PathString("/createMe"))
|
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;
|
res.StatusCode = result.Succeeded ? 200 : 500;
|
||||||
}
|
}
|
||||||
else if (req.Path == new PathString("/createSimple"))
|
else if (req.Path == new PathString("/createSimple"))
|
||||||
|
|
@ -340,9 +371,21 @@ namespace Microsoft.AspNetCore.Identity.InMemory
|
||||||
})
|
})
|
||||||
.ConfigureServices(services =>
|
.ConfigureServices(services =>
|
||||||
{
|
{
|
||||||
services.AddIdentity<PocoUser, PocoRole>().AddDefaultTokenProviders();
|
if (testCore)
|
||||||
services.AddSingleton<IUserStore<PocoUser>, InMemoryStore<PocoUser, PocoRole>>();
|
{
|
||||||
services.AddSingleton<IRoleStore<PocoRole>, InMemoryStore<PocoUser, PocoRole>>();
|
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);
|
configureServices?.Invoke(services);
|
||||||
});
|
});
|
||||||
var server = new TestServer(builder);
|
var server = new TestServer(builder);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue