Horst
Horst

Reputation: 259

Blazor redirect back to local url from external url

I have a Blazor App where the user should do a login on an external login page (within intranet) and after login i want to redirect back to the apps main page (dashboard). So what i want basically is the user opens the app and clicks the login button, then the app routes to the external login page, the user enters his username and password, after the user has logged in, the dashboard(home) from my app gets loaded and shown.

So i have a login button and when the user clicks it, he will be routed to "login.razor". In "login.razor" i have the following code:

@page "/login"
@inject NavigationManager NavManager

@code {
    protected override async Task OnInitializedAsync()
    {
        string homeUrl = "/";

        //First attempt:
        // This doesn't work
        //NavManager.NavigateTo($"http://external.login.page?redirectUrl={homeUrl}", forceLoad: true);

        // Second attempt: (doesn't work either)
        // Navigate to external login
        NavManager.NavigateTo($"http://external.login.page", forceLoad: true);

        // Navigate to Home of blazor app (this is suppose to be executed after the user has submitted the above login form)
        NavManager.NavigateTo($"{homeUrl}", forceLoad: true);
    }
}

The first attempt is not working as the server response is an error. Second attempt doesn't work as the app routes almost instantly to "home" after the login page has been loaded.

Does someone has an idea how to solve this?

Upvotes: 3

Views: 9767

Answers (1)

Ahmad Mozaffar
Ahmad Mozaffar

Reputation: 56

I get your point it's something like Azure Active Directory or login with any external provider, basically the redirection process to the dashboard or the page you want should happen on the level of the login page not from your app, I mean the login page in the other app should receive a parameter called for example redirect_uri so the the code will look like this

// Navigate to external login
NavManager.NavigateTo($"http://external.login.page?redirect_url={home_uri}", forceLoad: true);

And on the level of the login application, if the login attempt goes well then get the value of that parameter and redirect the user to it.

Upvotes: 3

Related Questions