Search code examples
c#asp.net-coreblazorblazor-webassemblyauth0

How to specify the URL to return to after a successfull login with Auth0 and Blazor WASM?


My Blazor App runs on https://localhost:5001/app

But after I log in, Auth0 always returns to https://localhost:5001

How can I change that?

Here is how I register the service:

builder.Services.AddOidcAuthentication(options =>
{
    builder.Configuration.Bind("Auth0", options.ProviderOptions);
    options.ProviderOptions.DefaultScopes.Add("email");
    options.ProviderOptions.AdditionalProviderParameters.Add("audience", (string)builder.Configuration["Auth0:Audience"]!);
    options.ProviderOptions.AdditionalProviderParameters.Add("prompt", "select_account");
    // options.ProviderOptions.AdditionalProviderParameters.Add("returnTo", "https://localhost:5001/app"); <== NOT WORKING
});

And here is my Authentication component:

@page "/app/authentication/{action}"

@using Microsoft.Extensions.Configuration
@inject NavigationManager Nav

<RemoteAuthenticatorView Action="@Action" OnLogInSucceeded=@loginHandler OnLogOutSucceeded=@logoutHandler>
    <LogOut>
        @{
            var authority = (string)Configuration["Auth0:Authority"]!;
            var clientId = (string)Configuration["Auth0:ClientId"]!;
            var returnTo = "https://localhost:5001/app";
            Nav.NavigateTo($"{authority}/v2/logout?client_id={clientId}&returnTo={returnTo}");
        }
    </LogOut>
</RemoteAuthenticatorView>

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

    private void loginHandler() {}
    private void logoutHandler() {}
}

Solution

  • You may try as below:

    builder.Services.AddOidcAuthentication(options =>
    {
        options.ProviderOptions.RedirectUri = "{Your target url}";
        
    });
    

    I misunderstood your requirement at first, now I reproduced the issue successfully

    Solved the issue based on RBee's comment(the RemoteAuthenticatorView Component would redirect you to "/" by default,to avoid this, you have to set forceload to ture):

    @page "/app/authentication/{action}"
    @using Microsoft.AspNetCore.Components.WebAssembly.Authentication
    @inject NavigationManager Nav
    
    <RemoteAuthenticatorView Action="@Action" OnLogInSucceeded="@LoginSucceeded" />
    
    @code {
        [Parameter]
        public string? Action { get; set; }
        
    
        
    
        public void LoginSucceeded()
        {
            Nav.NavigateTo("/app",true);
        }
    
        
    }
    

    It's ok now: enter image description here