admin管理员组

文章数量:1326049

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/wp-admin, it first redirects to B2C login page with the default redirect URL (www.abc) that I set. Once the token is verified, the user is redirected to their site (www.abc1).

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);

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/wp-admin, it first redirects to B2C login page with the default redirect URL (www.abc) that I set. Once the token is verified, the user is redirected to their site (www.abc1).

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);
Share Improve this question edited Aug 11, 2020 at 5:37 Nate Allen 2,1062 gold badges16 silver badges23 bronze badges asked Aug 10, 2020 at 1:56 princeexpeditionprinceexpedition 237 bronze badges 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 Commented 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. – princeexpedition Commented Aug 11, 2020 at 14:58
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: plugin developmentWordPress Multisite with Azure B2C redirecturls after Login