diff --git a/src/AspNet.Security.OpenId/Events/OpenIdRedirectContext.cs b/src/AspNet.Security.OpenId/Events/OpenIdRedirectContext.cs new file mode 100644 index 00000000..ade4277f --- /dev/null +++ b/src/AspNet.Security.OpenId/Events/OpenIdRedirectContext.cs @@ -0,0 +1,23 @@ +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Http; + +namespace AspNet.Security.OpenId.Events +{ + public class OpenIdRedirectContext : PropertiesContext + { + public OpenIdRedirectContext(HttpContext context, + AuthenticationScheme scheme, + OpenIdAuthenticationOptions options, + AuthenticationProperties properties, + OpenIdAuthenticationMessage message) + : base(context, scheme, options, properties) + { + ProtocolMessage = message; + } + + /// + /// Gets or sets the protocol message that has been generated by the handler up to this point. + /// + public OpenIdAuthenticationMessage ProtocolMessage { get; set; } + } +} diff --git a/src/AspNet.Security.OpenId/OpenIdAuthenticationEvents.cs b/src/AspNet.Security.OpenId/OpenIdAuthenticationEvents.cs index 38768842..00f9c306 100644 --- a/src/AspNet.Security.OpenId/OpenIdAuthenticationEvents.cs +++ b/src/AspNet.Security.OpenId/OpenIdAuthenticationEvents.cs @@ -6,6 +6,7 @@ using System; using System.Threading.Tasks; +using AspNet.Security.OpenId.Events; using Microsoft.AspNetCore.Authentication; namespace AspNet.Security.OpenId @@ -21,11 +22,23 @@ public class OpenIdAuthenticationEvents : RemoteAuthenticationEvents /// public Func OnAuthenticated { get; set; } = context => Task.FromResult(null); + /// + /// Defines a notification invoked prior to redirecting to the identity provider. + /// + public Func OnRedirectToIdentityProvider { get; set; } = context => Task.CompletedTask; + /// /// Defines a notification invoked when the user is authenticated by the identity provider. /// /// The context of the event carries information in and results out. /// Task to enable asynchronous execution public virtual Task Authenticated(OpenIdAuthenticatedContext context) => OnAuthenticated(context); + + /// + /// Defines a notification invoked prior to redirecting to the identity provider. + /// + /// The context of the event carries information in and results out. + /// Task to enable asynchronous execution + public virtual Task RedirectToIdentityProvider(OpenIdRedirectContext context) => OnRedirectToIdentityProvider(context); } } diff --git a/src/AspNet.Security.OpenId/OpenIdAuthenticationHandler.cs b/src/AspNet.Security.OpenId/OpenIdAuthenticationHandler.cs index d71dfe58..23636963 100644 --- a/src/AspNet.Security.OpenId/OpenIdAuthenticationHandler.cs +++ b/src/AspNet.Security.OpenId/OpenIdAuthenticationHandler.cs @@ -12,6 +12,7 @@ using System.Security.Claims; using System.Text.Encodings.Web; using System.Threading.Tasks; +using AspNet.Security.OpenId.Events; using JetBrains.Annotations; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.WebUtilities; @@ -323,6 +324,10 @@ protected override async Task HandleChallengeAsync(AuthenticationProperties prop value: string.Join(",", Options.Attributes.Select(attribute => attribute.Key))); } + var context = new OpenIdRedirectContext(Context, Scheme, Options, properties, message); + + await Events.RedirectToIdentityProvider(context); + var address = QueryHelpers.AddQueryString(configuration.AuthenticationEndpoint, message.GetParameters() .ToDictionary(parameter => parameter.Key,