Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect to the original page after a sign-in #4318

Open
premun opened this issue Jan 10, 2025 · 0 comments
Open

Redirect to the original page after a sign-in #4318

premun opened this issue Jan 10, 2025 · 0 comments

Comments

@premun
Copy link
Member

premun commented Jan 10, 2025

Context

When user signs in BarViz, they get routed back to the / address. This is because the allowed redirectUri is configured in the Entra app and cannot be dynamic.
To make this work, we will probably store the original URI in the state: https://learn.microsoft.com/en-us/entra/identity-platform/reply-url#use-a-state-parameter and then read it after the return and redirect there.

Goal

  • Store the current URI in the state field before redirecting to the login page
  • Read the state after the user comes back signed in
  • Redirect the user to the original URI

Notes

Copilot says something like this:

Save the original URL: When a user tries to access a protected resource and gets redirected to the Microsoft login page, save the original URL they were trying to access. You can do this in a middleware or in the OnRedirectToIdentityProvider event.

options.Events = new OpenIdConnectEvents
{
    OnRedirectToIdentityProvider = context =>
    {
        var originalUrl = context.Request.Path + context.Request.QueryString;
        context.ProtocolMessage.State = ... // Encode the original URL in the state parameter
        return Task.CompletedTask;
    }
};

Retrieve the state parameter: After the user signs in, the Microsoft identity platform will redirect them back to your specified redirect URI with the state parameter. You will need to decode this state parameter to redirect the user back to the original URL.

options.Events = new OpenIdConnectEvents
{
    OnMessageReceived = context =>
    {
        ... // Decode the state parameter to get the original URL
        return Task.CompletedTask;
    }
};

Redirect the user back: Once you have retrieved the original URL from the state parameter, you can redirect the user back to that URL.

options.Events = new OpenIdConnectEvents
{
    OnTicketReceived = context =>
    {
        var originalUrl = ... // Retrieve the original URL from the context or session
        context.Properties.RedirectUri = originalUrl;
        return Task.CompletedTask;
    }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant