Split cookie events

This commit is contained in:
Hao Kung 2015-09-21 17:12:21 -07:00
parent 6c9157ff51
commit b189475551
10 changed files with 91 additions and 51 deletions

View File

@ -204,7 +204,7 @@ namespace Microsoft.AspNet.Authentication.Cookies
cookieValue,
cookieOptions);
ApplyHeaders();
await ApplyHeaders(shouldRedirectToReturnUrl: false);
}
catch (Exception exception)
{
@ -288,8 +288,9 @@ namespace Microsoft.AspNet.Authentication.Cookies
await Options.Events.SignedIn(signedInContext);
var shouldLoginRedirect = Options.LoginPath.HasValue && OriginalPath == Options.LoginPath;
ApplyHeaders(shouldLoginRedirect);
// Only redirect on the login path
var shouldRedirect = Options.LoginPath.HasValue && OriginalPath == Options.LoginPath;
await ApplyHeaders(shouldRedirect);
}
catch (Exception exception)
{
@ -326,8 +327,9 @@ namespace Microsoft.AspNet.Authentication.Cookies
Options.CookieName,
context.CookieOptions);
var shouldLogoutRedirect = Options.LogoutPath.HasValue && OriginalPath == Options.LogoutPath;
ApplyHeaders(shouldLogoutRedirect);
// Only redirect on the logout path
var shouldRedirect = Options.LogoutPath.HasValue && OriginalPath == Options.LogoutPath;
await ApplyHeaders(shouldRedirect);
}
catch (Exception exception)
{
@ -341,12 +343,11 @@ namespace Microsoft.AspNet.Authentication.Cookies
}
}
private void ApplyHeaders(bool shouldRedirectToReturnUrl = false)
private async Task ApplyHeaders(bool shouldRedirectToReturnUrl)
{
Response.Headers[HeaderNames.CacheControl] = HeaderValueNoCache;
Response.Headers[HeaderNames.Pragma] = HeaderValueNoCache;
Response.Headers[HeaderNames.Expires] = HeaderValueMinusOne;
if (shouldRedirectToReturnUrl && Response.StatusCode == 200)
{
var query = Request.Query;
@ -354,10 +355,11 @@ namespace Microsoft.AspNet.Authentication.Cookies
if (!StringValues.IsNullOrEmpty(redirectUri)
&& IsHostRelative(redirectUri))
{
var redirectContext = new CookieApplyRedirectContext(Context, Options, redirectUri);
Options.Events.ApplyRedirect(redirectContext);
var redirectContext = new CookieRedirectContext(Context, Options, redirectUri);
await Options.Events.RedirectToReturnUrl(redirectContext);
}
}
}
private static bool IsHostRelative(string path)
@ -384,8 +386,8 @@ namespace Microsoft.AspNet.Authentication.Cookies
OriginalPathBase +
Options.AccessDeniedPath;
var redirectContext = new CookieApplyRedirectContext(Context, Options, accessDeniedUri);
await Options.Events.ApplyRedirect(redirectContext);
var redirectContext = new CookieRedirectContext(Context, Options, accessDeniedUri);
await Options.Events.RedirectToAccessDenied(redirectContext);
}
catch (Exception exception)
{
@ -411,8 +413,8 @@ namespace Microsoft.AspNet.Authentication.Cookies
}
var loginUri = Options.LoginPath + QueryString.Create(Options.ReturnUrlParameter, redirectUri);
var redirectContext = new CookieApplyRedirectContext(Context, Options, BuildRedirectUri(loginUri));
await Options.Events.ApplyRedirect(redirectContext);
var redirectContext = new CookieRedirectContext(Context, Options, BuildRedirectUri(loginUri));
await Options.Events.RedirectToLogin(redirectContext);
}
catch (Exception exception)
{

View File

@ -36,7 +36,34 @@ namespace Microsoft.AspNet.Authentication.Cookies
/// <summary>
/// A delegate assigned to this property will be invoked when the related method is called
/// </summary>
public Func<CookieApplyRedirectContext, Task> OnApplyRedirect { get; set; } = context =>
public Func<CookieRedirectContext, Task> OnRedirectToReturnUrl { get; set; } = context =>
{
context.Response.Redirect(context.RedirectUri);
return Task.FromResult(0);
};
/// <summary>
/// A delegate assigned to this property will be invoked when the related method is called
/// </summary>
public Func<CookieRedirectContext, Task> OnRedirectToAccessDenied { get; set; } = context =>
{
context.Response.Redirect(context.RedirectUri);
return Task.FromResult(0);
};
/// <summary>
/// A delegate assigned to this property will be invoked when the related method is called
/// </summary>
public Func<CookieRedirectContext, Task> OnRedirectToLogin { get; set; } = context =>
{
context.Response.Redirect(context.RedirectUri);
return Task.FromResult(0);
};
/// <summary>
/// A delegate assigned to this property will be invoked when the related method is called
/// </summary>
public Func<CookieRedirectContext, Task> OnRedirectToLogout { get; set; } = context =>
{
context.Response.Redirect(context.RedirectUri);
return Task.FromResult(0);
@ -76,7 +103,25 @@ namespace Microsoft.AspNet.Authentication.Cookies
/// Implements the interface method by invoking the related delegate method
/// </summary>
/// <param name="context">Contains information about the event</param>
public virtual Task ApplyRedirect(CookieApplyRedirectContext context) => OnApplyRedirect(context);
public virtual Task RedirectToLogout(CookieRedirectContext context) => OnRedirectToLogout(context);
/// <summary>
/// Implements the interface method by invoking the related delegate method
/// </summary>
/// <param name="context">Contains information about the event</param>
public virtual Task RedirectToLogin(CookieRedirectContext context) => OnRedirectToLogin(context);
/// <summary>
/// Implements the interface method by invoking the related delegate method
/// </summary>
/// <param name="context">Contains information about the event</param>
public virtual Task RedirectToReturnUrl(CookieRedirectContext context) => OnRedirectToReturnUrl(context);
/// <summary>
/// Implements the interface method by invoking the related delegate method
/// </summary>
/// <param name="context">Contains information about the event</param>
public virtual Task RedirectToAccessDenied(CookieRedirectContext context) => OnRedirectToAccessDenied(context);
/// <summary>
/// Implements the interface method by invoking the related delegate method

View File

@ -9,7 +9,7 @@ namespace Microsoft.AspNet.Authentication.Cookies
/// <summary>
/// Context passed when a Challenge, SignIn, or SignOut causes a redirect in the cookie middleware
/// </summary>
public class CookieApplyRedirectContext : BaseContext<CookieAuthenticationOptions>
public class CookieRedirectContext : BaseContext<CookieAuthenticationOptions>
{
/// <summary>
/// Creates a new context object.
@ -18,7 +18,7 @@ namespace Microsoft.AspNet.Authentication.Cookies
/// <param name="options">The cookie middleware options</param>
/// <param name="redirectUri">The initial redirect URI</param>
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "2#", Justification = "Represents header value")]
public CookieApplyRedirectContext(HttpContext context, CookieAuthenticationOptions options, string redirectUri)
public CookieRedirectContext(HttpContext context, CookieAuthenticationOptions options, string redirectUri)
: base(context, options)
{
RedirectUri = redirectUri;

View File

@ -32,10 +32,28 @@ namespace Microsoft.AspNet.Authentication.Cookies
Task SignedIn(CookieSignedInContext context);
/// <summary>
/// Called when a Challenge, SignIn, or SignOut causes a redirect in the cookie middleware
/// Called when a SignOut causes a redirect in the cookie middleware
/// </summary>
/// <param name="context">Contains information about the event</param>
Task ApplyRedirect(CookieApplyRedirectContext context);
Task RedirectToLogout(CookieRedirectContext context);
/// <summary>
/// Called when a SignIn causes a redirect in the cookie middleware
/// </summary>
/// <param name="context">Contains information about the event</param>
Task RedirectToLogin(CookieRedirectContext context);
/// <summary>
/// Called when redirecting back to the return url in the cookie middleware
/// </summary>
/// <param name="context">Contains information about the event</param>
Task RedirectToReturnUrl(CookieRedirectContext context);
/// <summary>
/// Called when an access denied causes a redirect in the cookie middleware
/// </summary>
/// <param name="context">Contains information about the event</param>
Task RedirectToAccessDenied(CookieRedirectContext context);
/// <summary>
/// Called during the sign-out flow to augment the cookie cleanup process.

View File

@ -29,6 +29,6 @@ namespace Microsoft.AspNet.Authentication.OAuth
/// Called when a Challenge causes a redirect to the authorize endpoint.
/// </summary>
/// <param name="context">Contains redirect URI and <see cref="AuthenticationProperties"/> of the challenge.</param>
Task RedirectToAuthorizationEndpoint(OAuthRedirectToAuthorizationEndpointContext context);
Task RedirectToAuthorizationEndpoint(OAuthRedirectToAuthorizationContext context);
}
}

View File

@ -24,7 +24,7 @@ namespace Microsoft.AspNet.Authentication.OAuth
/// <summary>
/// Gets or sets the delegate that is invoked when the RedirectToAuthorizationEndpoint method is invoked.
/// </summary>
public Func<OAuthRedirectToAuthorizationEndpointContext, Task> OnRedirectToAuthorizationEndpoint { get; set; } = context =>
public Func<OAuthRedirectToAuthorizationContext, Task> OnRedirectToAuthorizationEndpoint { get; set; } = context =>
{
context.Response.Redirect(context.RedirectUri);
return Task.FromResult(0);
@ -48,6 +48,6 @@ namespace Microsoft.AspNet.Authentication.OAuth
/// Called when a Challenge causes a redirect to authorize endpoint in the OAuth middleware.
/// </summary>
/// <param name="context">Contains redirect URI and <see cref="AuthenticationProperties"/> of the challenge.</param>
public virtual Task RedirectToAuthorizationEndpoint(OAuthRedirectToAuthorizationEndpointContext context) => OnRedirectToAuthorizationEndpoint(context);
public virtual Task RedirectToAuthorizationEndpoint(OAuthRedirectToAuthorizationContext context) => OnRedirectToAuthorizationEndpoint(context);
}
}

View File

@ -9,7 +9,7 @@ namespace Microsoft.AspNet.Authentication.OAuth
/// <summary>
/// Context passed when a Challenge causes a redirect to authorize endpoint in the middleware.
/// </summary>
public class OAuthRedirectToAuthorizationEndpointContext : BaseContext<OAuthOptions>
public class OAuthRedirectToAuthorizationContext : BaseContext<OAuthOptions>
{
/// <summary>
/// Creates a new context object.
@ -17,7 +17,7 @@ namespace Microsoft.AspNet.Authentication.OAuth
/// <param name="context">The HTTP request context.</param>
/// <param name="properties">The authentication properties of the challenge.</param>
/// <param name="redirectUri">The initial redirect URI.</param>
public OAuthRedirectToAuthorizationEndpointContext(HttpContext context, OAuthOptions options, AuthenticationProperties properties, string redirectUri)
public OAuthRedirectToAuthorizationContext(HttpContext context, OAuthOptions options, AuthenticationProperties properties, string redirectUri)
: base(context, options)
{
RedirectUri = redirectUri;

View File

@ -1,25 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.Http;
namespace Microsoft.AspNet.Authentication.OAuth
{
/// <summary>
/// Provides context information to middleware providers.
/// </summary>
public class OAuthReturnEndpointContext : SigningInContext
{
/// <summary>
/// Initializes a new <see cref="OAuthReturnEndpointContext"/>.
/// </summary>
/// <param name="context">The HTTP environment.</param>
/// <param name="ticket">The authentication ticket.</param>
public OAuthReturnEndpointContext(
HttpContext context,
AuthenticationTicket ticket)
: base(context, ticket)
{
}
}
}

View File

@ -50,7 +50,7 @@ namespace Microsoft.AspNet.Authentication.OAuth
return true;
}
var context = new OAuthReturnEndpointContext(Context, ticket)
var context = new SigningInContext(Context, ticket)
{
SignInScheme = Options.SignInScheme,
RedirectUri = ticket.Properties.RedirectUri,
@ -212,7 +212,7 @@ namespace Microsoft.AspNet.Authentication.OAuth
var authorizationEndpoint = BuildChallengeUrl(properties, BuildRedirectUri(Options.CallbackPath));
var redirectContext = new OAuthRedirectToAuthorizationEndpointContext(
var redirectContext = new OAuthRedirectToAuthorizationContext(
Context, Options,
properties, authorizationEndpoint);
await Options.Events.RedirectToAuthorizationEndpoint(redirectContext);