OpenIdConnectOptions Validate Method Throwing Exception in ASP.NET 6.0

Anonymous
2022-04-01T19:34:36.267+00:00

I have a new ASP.NET Core 6.0 web app, and I am trying to configure OpenID Connect (OIDC) in the Program.cs file. I am using "Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.0" in my .csproj file.

This is the code so far for my OIDC logic in my Program.cs file:

using Microsoft.IdentityModel.Protocols.OpenIdConnect;

var builder = WebApplication.CreateBuilder(args);

Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectConfiguration oidcConfig = new OpenIdConnectConfiguration();
oidcConfig.TokenEndpoint = "https://removed.com";
oidcConfig.UserInfoEndpoint = "https://removed.com";
oidcConfig.JwksUri = "https://removed.com";

Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectOptions oidcOptions = new Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectOptions();
oidcOptions.Authority = "https://removed.com";
oidcOptions.MetadataAddress = "https://removed.com";
oidcOptions.Configuration = oidcConfig;
oidcOptions.ClientId = "xxxxxxxxxxxxxxx";
oidcOptions.ClientSecret = "xxxxxxxxxxxxxxx";
oidcOptions.ResponseType = OpenIdConnectResponseType.IdToken;
oidcOptions.GetClaimsFromUserInfoEndpoint = true;
oidcOptions.SaveTokens = true;
oidcOptions.Validate();

When I run my code and it gets to oidcOptions.Validate(), the following exception is thrown:

An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.AspNetCore.Authentication.OpenIdConnect.dll: 'Provide Authority, MetadataAddress, Configuration, or ConfigurationManager to OpenIdConnectOptions'

Why is this exception being thrown? When I run my code in debug mode, I can see that my oidcOptions object has the values I provided for Authority, MetadataAddress, and Configuration.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,161 questions
{count} votes

1 answer

Sort by: Most helpful
  1. SurferOnWww 1,906 Reputation points
    2022-04-03T01:24:13.41+00:00

    The following document describes the settings in the Program.cs of ASP.NET Core MVC application when Duende IdentityServer is used:

    Interactive Applications with ASP.NET Core
    https://docs.duendesoftware.com/identityserver/v5/quickstarts/2_interactive/