1

My WP multisite has more than 300 sites which means I need to store all 300 redirect_urls in Azure AD B2C, but this number of redirect_urls cannot be stored in AD B2C as the maximum number of redirect URIs is 256.

What I do is that when the user logs in through www.abc1.com/wp-admin, it first redirects to B2C login page with the default redirect URL (www.abc.com) that I set. Once the token is verified, the user is redirected to their site (www.abc1.com).

But it doesn't seem to work. I am even not sure if it is the best approach to redirect the user to site they want to access by this way.

//fetch the user' site id and redirect to the site.
$user_blogs = get_blogs_of_user($userID);
if (!empty($user_blogs)) {
$siteInfo = array();
foreach ($user_blogs as $site_id => $site_values) {
$siteInfo[] = $site_values->siteurl;
}
$redirect_url = $siteInfo[0] ; // to the primary site..
}

// Set cookies to authenticate on WP side
wp_set_auth_cookie($userID);

//Redirect to primary site that assigned to the user
wp_safe_redirect($redirect_url);
2
  • When you say "But it doesn't seem to work," what specifically isn't working about it? They're not getting logged in? They are getting redirected to the wrong site? It's throwing an error? More details will help.
    – Nate Allen
    Aug 11, 2020 at 5:52
  • It redirects back to the user site but without active session. Anyway, I placed this above code inside add_action('wp_loaded', 'b2c_verify_token'); but I guess I need to set the cookies like this add_action('init', 'set_auth_cookie'); before the wp_loaded action triggers. Aug 11, 2020 at 14:58

1 Answer 1

1

I can see a few issues with this approach:

  1. If the user doesn't belong to a site yet, they won't get redirected correctly because $redirect_url won't be defined.
  2. If the user belongs to more than 1 site, they will always be redirected to the site with the lowest ID, not necessarily the one they're trying to log into.

Have you tried any Azure SSO plugins? I built an intranet for a client that uses Azure for employees to log into the site, and we had great results using Single Sign-on with Azure Active Directory (for WordPress). It's supposed to work with multisite; you'll just need to make sure it's configured correctly on each site.

2
  • I have tried Azure SSO plugin but this plug has to be configured in each site that would be hard as my wordpress holds more than 1000 sites. Correct me if I am wrong it is also important to set redirect_url in the B2C app registration. Aug 11, 2020 at 7:54
  • I m using this plug(github.com/AzureAD/… ) that I have already modified which now allows me to access the setting page from network panel. Aug 11, 2020 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.