How to Redirect Users to a Home Page or Custom Page After They Sign In

 If you’re using Power Apps portals (now Power Pages) or a similar setup with Azure Active Directory (AD) / Azure B2C logins, you may want users to land on a home page—or different pages depending on how they signed in—rather than the default profile page. Below are the core steps to achieve this, along with examples of handling custom query parameters.


1. Disable the Default Profile Redirect

  1. In your Portal Management app, go to Site Settings.
  2. Search for the setting named ProfileRedirectEnabled.
  3. Change its value from true to false.
  4. Save your changes, then Sync your changes in Power Pages Design Studio (or clear cache if using older portals).

Once disabled, the portal will no longer redirect users to the profile page after sign-in. Instead, they will remain on (or be returned to) the page they were on.


2. Redirect Users to Different Pages Based on How They Signed In

Assume you have two authentication methods:

  • Azure AD (referrer might contain microsoftonline)
  • Azure B2C (referrer might contain b2clogin)

You want to automatically send them to different pages. You can insert the following script into your Home Page (or any other landing page), either in the JavaScript section or at the top of the HTML for an immediate redirect.

<script>
/**
 * handleReferrerRedirect
 * @description Checks the document referrer. If it contains certain strings, redirect accordingly.
 * Usage: Place this in the home page's HTML (or JS) so it runs immediately on load.
 */
function handleReferrerRedirect()
{
    // Check if referrer indicates B2C login
    if (document.referrer && document.referrer.indexOf("b2clogin") > -1)
    {
        window.location.href = "/b2c-page"; // Replace with your B2C landing page
        return;
    }

    // Check if referrer indicates AAD login
    if (document.referrer && document.referrer.indexOf("microsoftonline") > -1)
    {
        window.location.href = "/aad-page"; // Replace with your AAD landing page
        return;
    }
}

// Immediately call the function
handleReferrerRedirect();
</script>
  • If you see a brief flicker of the home page before redirect, place this script at the very top of the page (in HTML) so it executes before the page fully loads.

3. Bonus: Passing Query Parameters

You can also handle scenarios where a user clicks a deep link (e.g., including an id or other parameters in the URL). If the user isn’t authenticated, they’ll be forced to sign in first—but you still want to land them on the correct page with the original parameters.

Imagine a link like:

https://your-portal-url.com/signin?returnUrl=/my-page?id=123

After successful sign-in, you’d like the user to end up on:

https://your-portal-url.com/my-page?id=123

Place a script like the following on your home page (or common landing page) to capture and redirect with the original parameters:

<script>
    /**
     * handleReturnUrl
     * @description Checks if returnUrl is present in the query string and redirects there.
     * Usage: Place on the landing page so that after sign-in, it processes the returnUrl param.
     */
    function handleReturnUrl()
    {
        var urlParams = new URLSearchParams(window.location.search);
        var returnUrl = urlParams.get("returnUrl");
        if (returnUrl)
        {
            // Redirect the user to the full path, preserving query parameters
            window.location.href = window.location.origin + returnUrl;
        }
    }
   
    // Immediately call the function
    handleReturnUrl();
    </script>
   

Whenever a user arrives at your site with ?returnUrl=, the script grabs that path and navigates them there—ensuring they see the intended page (and ID) after sign-in.


Summary

  1. Disable Profile Redirect: Change ProfileRedirectEnabled to false in Site Settings.
  2. Custom Redirect by Referrer: Add a short JavaScript snippet to your home page that checks document.referrer and sends the user to the desired page (B2C or AAD).
  3. Handle Query Parameters: If you need to preserve query parameters (like IDs) across sign-in, use a script to detect and redirect after authentication.

These quick edits ensure a seamless user experience, guiding users exactly where you want them to go right after they log in. Feel free to modify the code snippets above to match your specific page URLs and parameter names.

No comments:

Post a Comment