Browse Source

Update the OWIN client host to resolve the default cookie manager from the application builder when available

pull/2329/head
Kévin Chalet 8 months ago
parent
commit
20abfe0b2c
  1. 2
      src/OpenIddict.Abstractions/OpenIddictResources.resx
  2. 6
      src/OpenIddict.Client.Owin/OpenIddictClientOwinBuilder.cs
  3. 14
      src/OpenIddict.Client.Owin/OpenIddictClientOwinConfiguration.cs
  4. 8
      src/OpenIddict.Client.Owin/OpenIddictClientOwinOptions.cs

2
src/OpenIddict.Abstractions/OpenIddictResources.resx

@ -1115,7 +1115,7 @@ Note: when using a dependency injection container supporting middleware resoluti
</data>
<data name="ID0317" xml:space="preserve">
<value>The OpenIddict client services cannot be resolved from the DI container.
To register the server services, use 'services.AddOpenIddict().AddClient()'.</value>
To register the client services, use 'services.AddOpenIddict().AddClient()'.</value>
</data>
<data name="ID0318" xml:space="preserve">
<value>The core services must be registered when enabling the OpenIddict client feature.

6
src/OpenIddict.Client.Owin/OpenIddictClientOwinBuilder.cs

@ -130,6 +130,12 @@ public sealed class OpenIddictClientOwinBuilder
/// <summary>
/// Sets the cookie manager used to read and write the cookies produced by the OWIN host.
/// </summary>
/// <remarks>
/// If the manager isn't explicitly set, OpenIddict will try to resolve the default instance
/// provided by the OWIN host (only if <see cref="IAppBuilder"/> was registered as a service
/// in the dependency injection container). If no instance can be resolved, the generic
/// <see cref="CookieManager"/> implementation will be used.
/// </remarks>
/// <param name="manager">The cookie manager to use.</param>
/// <returns>The <see cref="OpenIddictClientOwinBuilder"/> instance.</returns>
public OpenIddictClientOwinBuilder SetCookieManager(ICookieManager manager)

14
src/OpenIddict.Client.Owin/OpenIddictClientOwinConfiguration.cs

@ -7,6 +7,7 @@
using System.ComponentModel;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Owin;
namespace OpenIddict.Client.Owin;
@ -46,6 +47,19 @@ public sealed class OpenIddictClientOwinConfiguration : IConfigureOptions<OpenId
throw new ArgumentNullException(nameof(options));
}
// If no cookie manager was explicitly configured but the OWIN application builder was registered as a service
// (which is required when using Autofac with the built-in Katana authentication middleware, as they require
// injecting IAppBuilder in their constructor), try to resolve the default cookie manager provided by the
// host. If it can't be resolved, use the generic implementation that directly operates on OWIN responses.
options.CookieManager ??= _provider.GetService<IAppBuilder>() switch
{
// See https://github.com/aspnet/AspNetKatana/pull/486 for more information.
IAppBuilder builder when builder.Properties.TryGetValue("infrastructure.CookieManager",
out object? property) && property is ICookieManager manager => manager,
_ => new CookieManager()
};
if (options.AuthenticationMode is AuthenticationMode.Active)
{
throw new InvalidOperationException(SR.GetResourceString(SR.ID0314));

8
src/OpenIddict.Client.Owin/OpenIddictClientOwinOptions.cs

@ -72,7 +72,13 @@ public sealed class OpenIddictClientOwinOptions : AuthenticationOptions
/// <summary>
/// Gets or sets the cookie manager used to read and write the cookies produced by the OWIN host.
/// </summary>
public ICookieManager CookieManager { get; set; } = new CookieManager();
/// <remarks>
/// If the manager isn't explicitly set, OpenIddict will try to resolve the default instance
/// provided by the OWIN host (only if <see cref="IAppBuilder"/> was registered as a service
/// in the dependency injection container). If no instance can be resolved, the generic
/// <see cref="Microsoft.Owin.Infrastructure.CookieManager"/> implementation will be used.
/// </remarks>
public ICookieManager CookieManager { get; set; } = default!;
/// <summary>
/// Gets or sets the name of the correlation cookie used to bind authorization

Loading…
Cancel
Save