From 83bffe35421c2096155f345cb0dff5beb0c98050 Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Tue, 9 Sep 2014 15:26:50 -0700 Subject: [PATCH] #32 - Port Cookies OnSignedIn notification from Katana. --- .../CookieAuthenticationHandler.cs | 21 +++++--- .../CookieAuthenticationNotifications.cs | 15 ++++++ .../CookieResponseSignedInContext.cs | 52 +++++++++++++++++++ .../ICookieAuthenticationNotifications.cs | 6 +++ 4 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 src/Microsoft.AspNet.Security.Cookies/Notifications/CookieResponseSignedInContext.cs diff --git a/src/Microsoft.AspNet.Security.Cookies/CookieAuthenticationHandler.cs b/src/Microsoft.AspNet.Security.Cookies/CookieAuthenticationHandler.cs index 317c27d21a..b83835affc 100644 --- a/src/Microsoft.AspNet.Security.Cookies/CookieAuthenticationHandler.cs +++ b/src/Microsoft.AspNet.Security.Cookies/CookieAuthenticationHandler.cs @@ -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) { diff --git a/src/Microsoft.AspNet.Security.Cookies/Notifications/CookieAuthenticationNotifications.cs b/src/Microsoft.AspNet.Security.Cookies/Notifications/CookieAuthenticationNotifications.cs index 79b7e5bd99..efa30c02e2 100644 --- a/src/Microsoft.AspNet.Security.Cookies/Notifications/CookieAuthenticationNotifications.cs +++ b/src/Microsoft.AspNet.Security.Cookies/Notifications/CookieAuthenticationNotifications.cs @@ -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 /// public Action OnResponseSignIn { get; set; } + /// + /// A delegate assigned to this property will be invoked when the related method is called + /// + public Action OnResponseSignedIn { get; set; } + /// /// A delegate assigned to this property will be invoked when the related method is called /// @@ -64,6 +70,15 @@ namespace Microsoft.AspNet.Security.Cookies OnResponseSignIn.Invoke(context); } + /// + /// Implements the interface method by invoking the related delegate method + /// + /// + public virtual void ResponseSignedIn(CookieResponseSignedInContext context) + { + OnResponseSignedIn.Invoke(context); + } + /// /// Implements the interface method by invoking the related delegate method /// diff --git a/src/Microsoft.AspNet.Security.Cookies/Notifications/CookieResponseSignedInContext.cs b/src/Microsoft.AspNet.Security.Cookies/Notifications/CookieResponseSignedInContext.cs new file mode 100644 index 0000000000..3366fb9f71 --- /dev/null +++ b/src/Microsoft.AspNet.Security.Cookies/Notifications/CookieResponseSignedInContext.cs @@ -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 +{ + /// + /// Context object passed to the ICookieAuthenticationNotifications method ResponseSignedIn. + /// + public class CookieResponseSignedInContext : BaseContext + { + /// + /// Creates a new instance of the context object. + /// + /// The HTTP request context + /// The middleware options + /// Initializes AuthenticationType property + /// Initializes Identity property + /// Initializes Properties property + public CookieResponseSignedInContext( + HttpContext context, + CookieAuthenticationOptions options, + string authenticationType, + ClaimsIdentity identity, + AuthenticationProperties properties) + : base(context, options) + { + AuthenticationType = authenticationType; + Identity = identity; + Properties = properties; + } + + /// + /// The name of the AuthenticationType creating a cookie + /// + public string AuthenticationType { get; private set; } + + /// + /// Contains the claims that were converted into the outgoing cookie. + /// + public ClaimsIdentity Identity { get; private set; } + + /// + /// Contains the extra data that was contained in the outgoing cookie. + /// + public AuthenticationProperties Properties { get; private set; } + } +} diff --git a/src/Microsoft.AspNet.Security.Cookies/Notifications/ICookieAuthenticationNotifications.cs b/src/Microsoft.AspNet.Security.Cookies/Notifications/ICookieAuthenticationNotifications.cs index f97a4546e3..046dcd1538 100644 --- a/src/Microsoft.AspNet.Security.Cookies/Notifications/ICookieAuthenticationNotifications.cs +++ b/src/Microsoft.AspNet.Security.Cookies/Notifications/ICookieAuthenticationNotifications.cs @@ -26,6 +26,12 @@ namespace Microsoft.AspNet.Security.Cookies /// Contains information about the login session as well as the user . void ResponseSignIn(CookieResponseSignInContext context); + /// + /// Called when an endpoint has provided sign in information after it is converted into a cookie. + /// + /// Contains information about the login session as well as the user . + void ResponseSignedIn(CookieResponseSignedInContext context); + /// /// Called when a Challenge, SignIn, or SignOut causes a redirect in the cookie middleware ///