#32 - Port Cookies OnSignedIn notification from Katana.

This commit is contained in:
Chris Ross 2014-09-09 15:26:50 -07:00
parent d6b82b8799
commit 83bffe3542
4 changed files with 88 additions and 6 deletions

View File

@ -138,7 +138,7 @@ namespace Microsoft.AspNet.Security.Cookies
if (shouldSignin)
{
var context = new CookieResponseSignInContext(
var signInContext = new CookieResponseSignInContext(
Context,
Options,
Options.AuthenticationType,
@ -162,15 +162,15 @@ namespace Microsoft.AspNet.Security.Cookies
signin.Properties.ExpiresUtc = issuedUtc.Add(Options.ExpireTimeSpan);
}
Options.Notifications.ResponseSignIn(context);
Options.Notifications.ResponseSignIn(signInContext);
if (context.Properties.IsPersistent)
if (signInContext.Properties.IsPersistent)
{
DateTimeOffset expiresUtc = context.Properties.ExpiresUtc ?? issuedUtc.Add(Options.ExpireTimeSpan);
context.CookieOptions.Expires = expiresUtc.ToUniversalTime().DateTime;
DateTimeOffset expiresUtc = signInContext.Properties.ExpiresUtc ?? issuedUtc.Add(Options.ExpireTimeSpan);
signInContext.CookieOptions.Expires = expiresUtc.ToUniversalTime().DateTime;
}
model = new AuthenticationTicket(context.Identity, context.Properties);
model = new AuthenticationTicket(signInContext.Identity, signInContext.Properties);
if (Options.SessionStore != null)
{
if (_sessionKey != null)
@ -190,6 +190,15 @@ namespace Microsoft.AspNet.Security.Cookies
Options.CookieName,
cookieValue,
cookieOptions);
var signedInContext = new CookieResponseSignedInContext(
Context,
Options,
Options.AuthenticationType,
signInContext.Identity,
signInContext.Properties);
Options.Notifications.ResponseSignedIn(signedInContext);
}
else if (shouldSignout)
{

View File

@ -21,6 +21,7 @@ namespace Microsoft.AspNet.Security.Cookies
{
OnValidateIdentity = context => Task.FromResult(0);
OnResponseSignIn = context => { };
OnResponseSignedIn = context => { };
OnResponseSignOut = context => { };
OnApplyRedirect = DefaultBehavior.ApplyRedirect;
}
@ -35,6 +36,11 @@ namespace Microsoft.AspNet.Security.Cookies
/// </summary>
public Action<CookieResponseSignInContext> OnResponseSignIn { get; set; }
/// <summary>
/// A delegate assigned to this property will be invoked when the related method is called
/// </summary>
public Action<CookieResponseSignedInContext> OnResponseSignedIn { get; set; }
/// <summary>
/// A delegate assigned to this property will be invoked when the related method is called
/// </summary>
@ -64,6 +70,15 @@ namespace Microsoft.AspNet.Security.Cookies
OnResponseSignIn.Invoke(context);
}
/// <summary>
/// Implements the interface method by invoking the related delegate method
/// </summary>
/// <param name="context"></param>
public virtual void ResponseSignedIn(CookieResponseSignedInContext context)
{
OnResponseSignedIn.Invoke(context);
}
/// <summary>
/// Implements the interface method by invoking the related delegate method
/// </summary>

View File

@ -0,0 +1,52 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Security.Claims;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Security;
using Microsoft.AspNet.Security.Notifications;
namespace Microsoft.AspNet.Security.Cookies
{
/// <summary>
/// Context object passed to the ICookieAuthenticationNotifications method ResponseSignedIn.
/// </summary>
public class CookieResponseSignedInContext : BaseContext<CookieAuthenticationOptions>
{
/// <summary>
/// Creates a new instance of the context object.
/// </summary>
/// <param name="context">The HTTP request context</param>
/// <param name="options">The middleware options</param>
/// <param name="authenticationType">Initializes AuthenticationType property</param>
/// <param name="identity">Initializes Identity property</param>
/// <param name="properties">Initializes Properties property</param>
public CookieResponseSignedInContext(
HttpContext context,
CookieAuthenticationOptions options,
string authenticationType,
ClaimsIdentity identity,
AuthenticationProperties properties)
: base(context, options)
{
AuthenticationType = authenticationType;
Identity = identity;
Properties = properties;
}
/// <summary>
/// The name of the AuthenticationType creating a cookie
/// </summary>
public string AuthenticationType { get; private set; }
/// <summary>
/// Contains the claims that were converted into the outgoing cookie.
/// </summary>
public ClaimsIdentity Identity { get; private set; }
/// <summary>
/// Contains the extra data that was contained in the outgoing cookie.
/// </summary>
public AuthenticationProperties Properties { get; private set; }
}
}

View File

@ -26,6 +26,12 @@ namespace Microsoft.AspNet.Security.Cookies
/// <param name="context">Contains information about the login session as well as the user <see cref="System.Security.Claims.ClaimsIdentity"/>.</param>
void ResponseSignIn(CookieResponseSignInContext context);
/// <summary>
/// Called when an endpoint has provided sign in information after it is converted into a cookie.
/// </summary>
/// <param name="context">Contains information about the login session as well as the user <see cref="System.Security.Claims.ClaimsIdentity"/>.</param>
void ResponseSignedIn(CookieResponseSignedInContext context);
/// <summary>
/// Called when a Challenge, SignIn, or SignOut causes a redirect in the cookie middleware
/// </summary>