Skip to content

Possible bug in AddMicrosoftIdentityWebAppInternal #2752

@dbeattie71

Description

@dbeattie71

Microsoft.Identity.Web Library

Microsoft.Identity.Web

Microsoft.Identity.Web version

2.15.3

Web app

Sign-in users

Web API

Not Applicable

Token cache serialization

Not Applicable

Description

I'm not sure if this is a bug or not. This behavior may be intended and I'm not configuring it properly.

In my case, I'm using AddMicrosoftIdentityWebApp to add a 2nd scheme. I'm also using Configure<MicrosoftIdentityOptions> to configure ExtraQueryParameters so the appId is appended to the MetadataAddress . Apparently that is required for an app registration that has custom signging keys as a result of using claims mapping feature. The 2nd scheme works if I set it up with AddOpenIdConnect but it doesn't if I use AddMicrosoftIdentityWebApp.

In debugging, I found that in AddMicrosoftIdentityWebAppInternal the appId is appeneded to ExtraQueryParameters and options.MetadataAddress is set. Should mergedOptions.MetadataAddress be getting set here instead? :

if (mergedOptions.Authority != null)
                    {
                        mergedOptions.Authority = AuthorityHelpers.BuildCiamAuthorityIfNeeded(mergedOptions.Authority);
                        if (mergedOptions.ExtraQueryParameters != null)
                        {
                            options.MetadataAddress = mergedOptions.Authority + "/.well-known/openid-configuration?" + string.Join("&", mergedOptions.ExtraQueryParameters.Select(p => $"{p.Key}={p.Value}"));
                        }
                    }

                    PopulateOpenIdOptionsFromMergedOptions(options, mergedOptions);

PopulateOpenIdOptionsFromMergedOptions is called immediatley after and options.MetadataAddress is set again, wiping out its previous setting:
options.MetadataAddress = mergedOptions.MetadataAddress;

Reproduction steps

Create a asp.net core web api and add the relevant code snippets.

Error message

No response

Id Web logs

No response

Relevant code snippets

builder.Services.Configure<MicrosoftIdentityOptions>("MyScheme", options =>
    {
        options.Authority = "https://login.microsoftonline.com/some-tenant-id/v2.0";
        options.ExtraQueryParameters = new Dictionary<string, string>
        {
            {"appId", "some-client-id"}
        };
    });
    
    builder.Services.AddAuthentication()
        .AddMicrosoftIdentityWebApp(
            configurationSection: builder.Configuration.GetSection("MyScheme"),
            openIdConnectScheme: "MyScheme"
        );


### Regression

_No response_

### Expected behavior

I would expect by setting the `Authority` and `ExtraQueryParameters` in `MicrosoftIdentityOptions` for a named scheme, the `MetadataAddress` would have the appId or any other extra query parameters appended to the end.

Metadata

Metadata

Assignees

Labels

P1bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions