强制使用 Google 帐户选择器

65

即使用户只使用一个帐户登录,我是否有办法强制显示Google帐户选择器?

我尝试通过重定向到此URL:

https://accounts.google.com/AccountChooser?service=lso&continue=[authorizeurl]

看起来它能够正常工作,但我不知道是否存在其他可能导致其失败的条件。

enter image description here

5个回答

108
以下参数在OAuth2授权URL中得到支持:

prompt

目前可以具有的值为noneselect_accountconsent

  • none:会导致Google不显示任何UI,因此如果用户需要登录,或者在多次登录的情况下选择帐户,或者在第一次批准时进行同意,则会失败。 它可以在隐形的i-frame中运行,以从先前授权的用户获取令牌,然后再决定,例如呈现授权按钮。

  • consent:即使用户之前已经授权了您的应用程序,也会强制显示批准页面。 在某些极端情况下可能会有用,例如如果您丢失了用户的refresh_token,因为Google仅在明确同意操作时发出refresh_tokens。

  • select_account:将导致帐户选择器显示,即使只有一个登录用户,就像您所请求的那样。

select_account可以与consent结合使用,如下:

prompt = select_account consent


3
“approval_prompt=force” 和 “prompt=consent” 是相同的吗? - José F. Romaniello
3
是的,但与 prompt=consent 不同,它无法与 'select_account' 选项组合使用。如果现在要编写新代码,请改用 'prompt' 选项。 - breno
1
有没有一种方法可以强制使用 Gmail 帐户登录(例如 hd=gmail.com)? - woloski
1
@woloski,是的,hd=default应该限制在gmail帐户中。 - Ari Porad
6
"prompt=select_account+consent" 无法使用,请使用 "prompt=select_account consent"。详见文档:https://developers.google.com/accounts/docs/OpenIDConnect - Brett C
显示剩余7条评论

13

有些人可能会到这里寻找如何在Microsoft.AspNetCore.Authentication中执行此操作的答案。

我们能够通过在Startup.ConfigureServices方法中使用以下代码来完成它:

services.AddAuthentication()
  .AddGoogle(options =>
  {
      options.ClientId = configHelper.GoogleOAuthClientID;
      options.ClientSecret = configHelper.GoogleOAuthSecret;
      options.CallbackPath = "/signin-google";
      options.AuthorizationEndpoint = string.Concat(options.AuthorizationEndpoint, "?prompt=select_account");
  });

13

另外,在HTML标签中,您可以添加"data-prompt"参数作为"select_account":

<div class="g-signin2" data-onsuccess="onSignIn" data-prompt="select_account"> 

它会每次强制出现帐户选择器,即使您只使用一个帐户登录


对我没用, <div class="g-signin2" data-scope="profile email" data-width="298" data-onsuccess="onSignIn" data-prompt="select_account"></div>,但是每次仍会调用 onSignIn。 - Benjamin Poignant
@BenjaminPoignant 您的问题(成功处理程序在页面加载时立即调用)与此问题略有不同(尽管您的困惑是可以理解的)。正如 https://dev59.com/Q2Up5IYBdhLWcg3wJU9J#15503280 所指出的那样,您需要使用 gapi.auth2.getAuthInstance().signOut(); 从您的应用中注销用户。这个问题是关于如何在这样做后确保当用户再次点击登录按钮时,他们有机会选择要作为哪个Google帐户登录,而不是在点击登录按钮后立即使用他们当前的Google帐户。 - Mark Amery

4

3

如果你正在使用gapi,只需添加prompt: 'select_account'
例如:

gapi.load('auth2', function () {
            gapi.auth2.init({
                client_id: "client_id.apps.googleusercontent.com",
                scope: "profile email", // this isn't required
                ux_mode: 'redirect',
                redirect_uri: 'https://www.example.com',
                prompt: 'select_account'
            }).then(function (auth2) {
                console.log("signed in: " + auth2.isSignedIn.get());
                x = auth2.isSignedIn.get();
                auth2.isSignedIn.listen(onSignIn);
                var button = document.querySelector('#signInButton');
                button.addEventListener('click', function () {
                    auth2.signIn();
                });
            });
        });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接