Blazor WebAssembly login authentication/login from .well-known/openid-configuration gives localhost URL instead of public URL

Viewed 502

The autogenerated oidc configuration file gives localhost instead of my public URL. How do I set it up so that it gives the right URL?

http://167.172.118.170/.well-known/openid-configuration

In the login link on my test site: http://167.172.118.170/authentication/login the login redirects to a localhost address instead of the public 167.172.118.170 address, like this:

http://localhost:5008/connect/authorize?client_id=MyProject.Web.Client&redirect_uri=http%3A%2F%2F167.172.118.170%2Fauthentication%2Flogin-callback&response_type=code&scope=MyProject.Web.ServerAPI%20openid%20profile&state=b18bc58127b54ea9aaff1a210b7899de&code_challenge=OOIuUi2yJnYcjZZIu4LveJfbLz0Na7IKkzlDTKb81IE&code_challenge_method=S256&response_mode=query

How do I configure it so that it goes to http://167.172.118.170/connect/authorize instead?

This is the content of the authentication razor file:

@page "/authentication/{action}"
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
<RemoteAuthenticatorView Action="@Action" />

@code{
    [Parameter] public string Action { get; set; }
}

https://github.com/jonasarcangel/BlazorLoginNetworkErrorIssue/blob/master/src/MyProject.Web.Client.Shell/Pages/Authentication.razor

There is no option to provide the server URL.

2 Answers

Your application is not configured properly.

You need to configure your identity sever using similar code like below in your Program.cs

builder.Services.AddOidcAuthentication(options =>
        {
            builder.Configuration.Bind("OidcConfiguration", options.ProviderOptions);
        });

"OidcConfiguration" comes from your configuration file and will have parameters like below:

"OidcConfiguration": {
    "Authority": "https://YourIdentityServerIP",
    "ClientId": "YourClienId",
    "DefaultScopes": [
      "openid",
      "profile",
      "api"
    ],
    "RedirectUri": "https://yourclientapp/authentication/login-callback",
    "PostLogoutRedirectUri": "https://yourclientapp/authentication/logout-callback",
    "ResponseType": "code"

  }

You can use this two links to see how i configured mine in a pet project

Program Files: https://github.com/oteebest/edu-client-blazor/blob/master/CBTClient/Program.cs

appsetting.json https://github.com/oteebest/edu-client-blazor/blob/master/CBTClient/wwwroot/appsettings.json

The solution was to add the following:

using IdentityServer4.Extensions;

//...
app.Use((ctx, next) => 
{
    ctx.SetIdentityServerOrigin("http://167.172.118.170");
    return next();
});
Related