dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.4k stars 10k forks source link

App does not take settings from appsettings.Development.json in Debug/Development #52306

Closed Rhywden closed 11 months ago

Rhywden commented 11 months ago

Is there an existing issue for this?

Describe the bug

Created a new Blazor Web App. Configured it so that OIDC settings reside in appsettings.json / appsettings.Development.json. Both files exist per default on a newly created project. I then bound those settings to the OIDC service like thus: Program.cs

builder.Services.AddAuthentication(options =>
    {
        options.DefaultScheme = IdentityConstants.ApplicationScheme;
        options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
    }).AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, opt =>
    {
        builder.Configuration.Bind("OIDC", opt);
        opt.Events.OnRedirectToIdentityProvider = async n =>
        {
            n.ProtocolMessage.RedirectUri = builder.Configuration["OIDC:RedirectUri"];
            await Task.FromResult(0);
        };
    })
    .AddIdentityCookies();

appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-Beratung-79de502f-e4a8-47df-81f9-88a690b8a386;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "OIDC": {
    "Authority": "https://id.foo/realms/master",
    "ClientId": "Foo",
    "DefaultScopes": [
      "openid",
      "profile",
      "offline_access",
      "roles"
    ],
    "RedirectUri": "https://foo/signin-oidc",
    "ResponseType": "code"
  }
}

appsettings.Development.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    },
    "OIDC": {
      "Authority": "https://id.foo/realms/master",
      "ClientId": "FooDev",
      "DefaultScopes": [
        "openid",
        "profile",
        "offline_access",
        "roles"
      ],
      "RedirectUri": "https://localhost:7162/signin-oidc",
      "ResponseType": "code",
      "SaveTokens": true
    }
  }
}

No other changes were made. I then started the app in Debug inside Visual Studio.

Expected Behavior

I expected to land at https://localhost:7162/signin-oidc after a succesful auth. Instead I landed at https://foo/signin-oidc, i.e. the address for the production site.

Steps To Reproduce

  1. Create a new Blazor Web App
  2. Try to bind options to appsettings.json / appsettings.Development.json

Exceptions (if any)

No response

.NET Version

8.0.100

Anything else?

Visual Studio Enterprise 2022 17.8.1

.NET SDK: Version: 8.0.100 Commit: 57efcf1350 Workload version: 8.0.100-manifests.71b9f198

Runtime Environment: OS Name: Windows OS Version: 10.0.22631 OS Platform: Windows RID: win-x64 Base Path: C:\Program Files\dotnet\sdk\8.0.100\

.NET workloads installed: Workload version: 8.0.100-manifests.71b9f198 [android] Installation Source: VS 17.9.34310.174, VS 17.8.34316.72 Manifest Version: 34.0.43/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.android\34.0.43\WorkloadManifest.json Install Type: Msi

[maui-windows] Installation Source: VS 17.9.34310.174, VS 17.8.34316.72 Manifest Version: 8.0.3/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maui\8.0.3\WorkloadManifest.json Install Type: Msi

[maccatalyst] Installation Source: VS 17.9.34310.174, VS 17.8.34316.72 Manifest Version: 17.0.8478/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maccatalyst\17.0.8478\WorkloadManifest.json Install Type: Msi

[ios] Installation Source: VS 17.9.34310.174, VS 17.8.34316.72 Manifest Version: 17.0.8478/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.ios\17.0.8478\WorkloadManifest.json Install Type: Msi

Host: Version: 8.0.0 Architecture: x64 Commit: 5535e31a71

.NET SDKs installed: 8.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found: x86 [C:\Program Files (x86)\dotnet] registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables: Not set

global.json file: Not found

mkArtakMSFT commented 11 months ago

Thanks for contacting us. This is most probably due to the fact that the environment you run your app does not match the appsettings.*.json file matching that environment. Hopefully this documentation will help you get started with this correctly: https://learn.microsoft.com/aspnet/core/fundamentals/environments?view=aspnetcore-8.0

Note, that this has nothing to do with the build configuration (debug or release).

Rhywden commented 11 months ago

Yeah, that would make sense if I had already customized the default project. And the default, as far as I can remember, is to "build in development".

But I did not. But here's some more proof:

image

You can see that the bool isDevelopment is true but that the clientId is set to FooProd when it clearly should be FooDev.

Rhywden commented 11 months ago

@mkArtakMSFT Oh, I just noticed that you can also see this by the fact that the ConnectionString is only set on appsettings.json but not in appsettings.Development.json which would actually make the default project (i.e. the one you get right after clicking "Create" in the project template dialog) crash and burn.

Due to the lovely "Throw exception if ConnectionString is not set" guard clause in line 39 in my screenshot.

Rhywden commented 11 months ago

@mkArtakMSFT @martincostello Uh, guys, this is very much neither answered nor resolved.