Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer sign out scheme when using external identity providers and asp.net identity #1265

Merged
merged 2 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/AspNetIdentity/IdentityServerBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using Duende.IdentityServer.AspNetIdentity;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using Duende.IdentityServer.Configuration;

namespace Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -81,6 +83,8 @@ public static IIdentityServerBuilder AddAspNetIdentity<TUser>(this IIdentityServ
builder.AddResourceOwnerValidator<ResourceOwnerPasswordValidator<TUser>>();
builder.AddProfileService<ProfileService<TUser>>();

builder.Services.AddSingleton<IPostConfigureOptions<IdentityServerOptions>, UseAspNetIdentityCookieScheme>();

return builder;
}

Expand Down
40 changes: 40 additions & 0 deletions src/AspNetIdentity/PostConfigureIdentityServerOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Duende.IdentityServer.Configuration;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;

namespace Duende.IdentityServer.AspNetIdentity;

/// <summary>
/// Identity server options configuration
/// </summary>
public class UseAspNetIdentityCookieScheme : IPostConfigureOptions<IdentityServerOptions>
{
private readonly IOptions<Microsoft.AspNetCore.Authentication.AuthenticationOptions> _authOptions;

/// <summary>
/// ctor
/// </summary>
/// <param name="authOptions"></param>
public UseAspNetIdentityCookieScheme(IOptions<Microsoft.AspNetCore.Authentication.AuthenticationOptions> authOptions)
{
_authOptions = authOptions;
}

/// <inheritdoc/>
public void PostConfigure(string name, IdentityServerOptions options)
{
// If we are using ASP.NET Identity and the dynamic providers don't have a
// sign out scheme set, then we need the dynamic providers to use ASP.NET
// Identity's cookie at sign out time. If the sign out scheme is explicitly
// set, then we don't override that though.

if (DefaultAuthSchemeIsAspNetIdentity() &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic has been refactored a bit to make the intentions clearer

!options.DynamicProviders.SignOutSchemeSetExplicitly)
{
options.DynamicProviders.SignOutScheme = IdentityConstants.ApplicationScheme;
}

bool DefaultAuthSchemeIsAspNetIdentity() =>
_authOptions.Value.DefaultAuthenticateScheme == IdentityConstants.ApplicationScheme;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,24 @@ public class DynamicProviderOptions
/// <summary>
/// Scheme for signout. Defaults to the constant IdentityServerConstants.DefaultCookieAuthenticationScheme.
/// </summary>
public string SignOutScheme { get; set; } = IdentityServerConstants.DefaultCookieAuthenticationScheme;
public string SignOutScheme
{
get
{
return _signOutScheme ?? IdentityServerConstants.DefaultCookieAuthenticationScheme;
}
set
{
_signOutScheme = value;
}
}

private string? _signOutScheme;

/// <summary>
/// Gets a value indicating if the SignOutScheme was set explicitly, either by application logic or by options binding.
/// </summary>
public bool SignOutSchemeSetExplicitly { get => _signOutScheme != null; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The options class now understands if the sign out scheme has actually been set. We use that in our configuration so that we don't override a user who is trying to set the scheme explicitly.


/// <summary>
/// Registers a provider configuration model and authentication handler for the protocol type being used.
Expand Down
Loading