Skip to content
This repository has been archived by the owner. It is now read-only.

Auth/Options 2.0 + Identity #1119

Closed
wants to merge 16 commits into from
Prev

Add overloads for identity cookies

  • Loading branch information...
HaoK committed Apr 18, 2017
commit 2bdfdb3220b3aca7513455652617af5685d5293c
@@ -39,6 +39,24 @@ internal class IdentityConfigureOptions : ConfigureOptions<IdentityOptions>
return services.AddIdentity<TUser, TRole>(setupAction: null);
}

/// <summary>
/// Configures the application cookie.
/// </summary>
/// <param name="services">The services available in the application.</param>
/// <param name="configure">An action to configure the <see cref="CookieAuthenticationOptions"/>.</param>
/// <returns>The services.</returns>
public static IServiceCollection ConfigureApplicationCookie(this IServiceCollection services, Action<CookieAuthenticationOptions> configure)
=> services.Configure(IdentityCookieOptions.ApplicationScheme, configure);

/// <summary>
/// Configure the external cookie.
/// </summary>
/// <param name="services">The services available in the application.</param>
/// <param name="configure">An action to configure the <see cref="CookieAuthenticationOptions"/>.</param>
/// <returns>The services.</returns>
public static IServiceCollection ConfigureExternalCookie(this IServiceCollection services, Action<CookieAuthenticationOptions> configure)
=> services.Configure(IdentityCookieOptions.ExternalScheme, configure);

/// <summary>
/// Adds and configures the identity system for the specified User and Role types.
/// </summary>
@@ -54,9 +72,10 @@ internal class IdentityConfigureOptions : ConfigureOptions<IdentityOptions>
where TRole : class
{
// Services used by identity
services.AddAuthentication(options =>
services.AddAuthenticationCore(options =>
{
options.DefaultAuthenticateScheme = IdentityCookieOptions.ApplicationScheme;
options.DefaultChallengeScheme = IdentityCookieOptions.ApplicationScheme;
options.DefaultSignInScheme = IdentityCookieOptions.ExternalScheme;
});

@@ -51,7 +51,7 @@ public async Task CanCreateMeLoginAndCookieStopsWorkingAfterExpiration()
var clock = new TestClock();
var server = CreateServer(services =>
{
services.Configure<CookieAuthenticationOptions>(IdentityCookieOptions.ApplicationScheme, options =>
services.ConfigureApplicationCookie(options =>
{
options.ExpireTimeSpan = TimeSpan.FromMinutes(10);
options.SlidingExpiration = false;
@@ -53,10 +53,9 @@ public Task SignOutAsync(SignOutContext context)
[Fact]
public async Task OnValidatePrincipalThrowsWithEmptyServiceCollection()
{
var scheme = new IdentityOptions().Cookies.ApplicationCookieAuthenticationScheme;
var httpContext = new Mock<HttpContext>();
httpContext.Setup(c => c.RequestServices).Returns(new ServiceCollection().BuildServiceProvider());
var id = new ClaimsPrincipal(new ClaimsIdentity(scheme));
var id = new ClaimsPrincipal(new ClaimsIdentity(IdentityCookieOptions.ApplicationScheme));
var ticket = new AuthenticationTicket(id, new AuthenticationProperties { IssuedUtc = DateTimeOffset.UtcNow }, scheme);
var context = new CookieValidatePrincipalContext(httpContext.Object, new AuthenticationSchemeBuilder(scheme) { HandlerType = typeof(NoopHandler) }.Build(), ticket, new CookieAuthenticationOptions());
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => SecurityStampValidator.ValidatePrincipalAsync(context));
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.