Skip to content

Commit 26e8f44

Browse files
committed
Update the OWIN client host to resolve the default cookie manager from the application builder when available
1 parent 4f6cef0 commit 26e8f44

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/OpenIddict.Client.Owin/OpenIddictClientOwinBuilder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ public OpenIddictClientOwinBuilder EnableErrorPassthrough()
130130
/// <summary>
131131
/// Sets the cookie manager used to read and write the cookies produced by the OWIN host.
132132
/// </summary>
133+
/// <remarks>
134+
/// If the manager isn't explicitly set, OpenIddict will try to resolve the default instance
135+
/// provided by the OWIN host (only if <see cref="IAppBuilder"/> was registered as a service
136+
/// in the dependency injection container). If no instance can be resolved, the generic
137+
/// <see cref="CookieManager"/> implementation will be used.
138+
/// </remarks>
133139
/// <param name="manager">The cookie manager to use.</param>
134140
/// <returns>The <see cref="OpenIddictClientOwinBuilder"/> instance.</returns>
135141
public OpenIddictClientOwinBuilder SetCookieManager(ICookieManager manager)

src/OpenIddict.Client.Owin/OpenIddictClientOwinConfiguration.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.ComponentModel;
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Extensions.Options;
10+
using Owin;
1011

1112
namespace OpenIddict.Client.Owin;
1213

@@ -46,6 +47,19 @@ public void PostConfigure(string? name, OpenIddictClientOwinOptions options)
4647
throw new ArgumentNullException(nameof(options));
4748
}
4849

50+
// If no cookie manager was explicitly configured but the OWIN application builder was registered as a service
51+
// (which is required when using Autofac with the built-in Katana authentication middleware, as they require
52+
// injecting IAppBuilder in their constructor), try to resolve the default cookie manager provided by the
53+
// host. If it can't be resolved, use the generic implementation that directly operates on OWIN responses.
54+
options.CookieManager ??= _provider.GetService<IAppBuilder>() switch
55+
{
56+
// Note: see https://github.com/aspnet/AspNetKatana/pull/486 for more information.
57+
IAppBuilder builder when builder.Properties.TryGetValue("infrastructure.CookieManager",
58+
out object? property) && property is ICookieManager manager => manager,
59+
60+
_ => new CookieManager()
61+
};
62+
4963
if (options.AuthenticationMode is AuthenticationMode.Active)
5064
{
5165
throw new InvalidOperationException(SR.GetResourceString(SR.ID0314));

src/OpenIddict.Client.Owin/OpenIddictClientOwinOptions.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ public OpenIddictClientOwinOptions()
7272
/// <summary>
7373
/// Gets or sets the cookie manager used to read and write the cookies produced by the OWIN host.
7474
/// </summary>
75-
public ICookieManager CookieManager { get; set; } = new CookieManager();
75+
/// <remarks>
76+
/// If the manager isn't explicitly set, OpenIddict will try to resolve the default instance
77+
/// provided by the OWIN host (only if <see cref="IAppBuilder"/> was registered as a service
78+
/// in the dependency injection container). If no instance can be resolved, the generic
79+
/// <see cref="Microsoft.Owin.Infrastructure.CookieManager"/> implementation will be used.
80+
/// </remarks>
81+
public ICookieManager CookieManager { get; set; } = default!;
7682

7783
/// <summary>
7884
/// Gets or sets the name of the correlation cookie used to bind authorization

0 commit comments

Comments
 (0)