【问题标题】:Google Identity Services implement manual and automatic signout function谷歌身份服务实现手动和自动退出功能
【发布时间】:2021-10-23 02:15:30
【问题描述】:

我正在为登录按钮使用 Google 的身份服务:https://developers.google.com/identity/gsi/web/guides/personalized-button

现在,一旦用户通过 Google 登录我的网站,我也会触发我自己网站的身份验证机制(通过 ASP.NET 登录表单)。在 60 分钟不活动后,用户会自动退出我的应用程序。但是,Google 登录按钮仍会显示该用户的电子邮件地址。 只需单击一下,同一台机器上的另一个用户现在就可以重新登录我的网站,因为 Google 不会要求再次进行身份验证。这似乎是一个奇怪的设计,但我可能遗漏了一些东西。 如何自动(在我的情况下为 60 分钟)使 Google 登录过期并手动 - 当用户在我的网站上单击注销时 - 从 Google 注销?

我检查了here,但这似乎不是一个可靠的解决方案。 我还检查了here(旧帖子)和here,但是在放置提到的注销代码示例时出现错误

Uncaught ReferenceError: gapi is not defined

即使我同步加载了https://accounts.google.com/gsi/client 脚本。此外,我认为 gapi 仍然不适用于新的 Google 身份服务。

<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="MYAPPIDPLACEHOLDER.apps.googleusercontent.com">


   function handleCredentialResponse(response) {
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                var result = JSON.parse(xhr.response).result;
                if (result == 'ok') {
                    window.location.href = "https://www.example.com";
                }
            }
        }
        //here I check the validity of the token
        xhr.open('POST', 'https://www.example.com/api/gtokensignin');
        xhr.setRequestHeader('Content-Type', 'application/json'); 
        xhr.send(JSON.stringify({ "idtoken": response.credential}));
    }
    
    
<div id="g_id_onload"
     data-client_id="MYAPPIDPLACEHOLDER.apps.googleusercontent.com"
     data-context="signin"
     data-ux_mode="popup"
     data-callback="handleCredentialResponse"
     data-nonce=""
     data-auto_prompt="false"
     data-auto_select="true">
</div>
    
<div class="g_id_signin"
     data-type="standard"
     data-shape="rectangular"
     data-theme="outline"
     data-text="signin_with"
     data-size="large"
     data-logo_alignment="left">
</div>  

example.svc.vb

此代码根据 Google id_token 成功检索用户详细信息

Public Function gtokensignin(ByVal str As oAuthUserSigninDetails) As Stream Implements Iexample.gtokensignin

    Dim dDateTimeNow As DateTime = DateTime.UtcNow

    Dim resultBytes As Byte()
    Dim resultText As String = "false"

    Dim WC As New WebClient
    Dim json As String

    Try
        json = WC.DownloadString("https://oauth2.googleapis.com/tokeninfo?id_token=" + str.idtoken)
    Catch ex As Exception
        Exit Function
    End Try

    Dim obj As JObject = JObject.Parse(json)

    Dim iss As String = obj.Item("iss")
    Dim azp As String = obj.Item("azp")
    Dim aud As String = obj.Item("aud")

    Dim exp As Long = Long.Parse(obj.Item("exp")) * 1000 'convert to milliseconds
    Dim expirationDate As DateTime = DateTimeOffset.FromUnixTimeMilliseconds(Long.Parse(exp)).UtcDateTime

    Dim email As String = obj.Item("email")

End Function

更新 1

好的,所以我在隐身浏览器实例中尝试了@Transformer(见下文)的代码,以确保我的 Google Chrome 登录不会混淆测试。

关于您的选项 1 的步骤和问题:

  1. 你为什么用https://apis.google.com/js/platform.js而不是https://accounts.google.com/gsi/client(如here所述),platform.jsold way of doing things
  2. 单击登录按钮后,我必须输入密码。我成功登录,Google 按钮显示我的电子邮件地址
  3. 我启动了 signOut() 函数并看到登录到 Google Chrome 控制台“用户已退出”。
  4. 我刷新了页面,但仍然在 Google 按钮中看到我的电子邮件地址,当我单击它时,我也无需再次输入密码即可登录。这是设计使然吗?如果是这样:为什么 Google 会为任何人提供无需重新验证即可再次登录的选项?

关于您的选项 2 的步骤和问题:

  1. 我通过自己的链接“https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=https://www.example. com?signout=yes” 2a。如果我在一个隐身浏览器窗口中,我会看到一个 Google 重定向屏幕“该页面正在尝试将您重定向到 example.com”,如果我这样做,我已成功注销。有没有办法不显示重定向屏幕? 2b。如果我在常规 Chrome 浏览器窗口中,我将被重定向到我雇主的 SSO 提供商登录页面,而不是返回我的个人网站 example.com。我猜是因为我们内部也使用 Google 帐户,如何控制?

重要提示:我还退出了我的 Gmail 帐户,我只想退出与我的网站的 Google 会话,并要求用户在再次登录时重新进行身份验证。我不确定这是否是 Google 想要此功能的方式。

【问题讨论】:

  • 您是在处理 cookie 创建,还是由 Google api 来处理?当我创建一个谷歌登录应用程序时,我必须将身份验证令牌存储在该用户的 cookie 中。如果你想注销它们,你需要做的就是删除那个 cookie(或者用一个空值和一个过去的过期日期覆盖 cookie)。
  • @Stevish 我添加了我的代码,谷歌正在处理 cookie 设置,我想你可以从中看到
  • 当,对不起。这与我的经验完全不同。希望你能找到答案!
  • 请查看我的选项1代码,signOut之后请致电auth2.disconnect()
  • @Transformer 谢谢,我已经实现了你的函数,不知道你是否改变了什么?

标签: google-identity google-identity-toolkit


【解决方案1】:

您的问题我如何才能自动(在我的情况下为 60 分钟)使 Google 登录和手动过期 - 当用户在我的网站上点击注销 - sign out from Google?...

Google 将 Auth2 用于身份服务,获取您的 getAuthInstance(),然后管理会话。您可以从那里获得所需的令牌,只需按照文档示例进行操作即可。下面是退出用户的代码。

看这里on github,你可以在你的js代码中采用下面的代码来强制完全退出,有两个步骤可以保证完全退出。 auth2.signOut().then(function () { auth2.disconnect(); });

选项 1. 获取 Auth2 实例,然后调用 auth2.disconnect() auth2.signOut() 成功。


<script type="text/javascript">

    // get your auth2 initializtion
    function onLoad() {
        gapi.load('auth2', function() {
            gapi.auth2.init();
        });
    }

    // On sign-out ,  auth2.disconnect();
    function signOut() {
        var auth2 = gapi.auth2.getAuthInstance();
        auth2.signOut().then(function () {
        // **** this is required to complete the sign-out 
         console.log('User signed out.');   
         auth2.disconnect();  
        });

        // Debugging, uncomment below if the above does not work to troubleshoot
        // auth2.disconnect();  
    }

</script>



<script src="https://apis.google.com/js/platform.js?onload=onLoad" async defer></script>

选项 2:将“ASP 注销操作”重定向到 Google App Engine 注销,如下所示

换掉域名而不是 http://localhost/application-name/logoutUser ,添加你的域名,在你asp Logout action重定向到这个注销页面。

document.location.href = "https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=http://localhost/application-name/logoutUser";

【讨论】:

  • 谢谢,我添加了更新 1,你能再看看吗?
  • 你好弗洛,你能分享why are you doing it in the incognito mode。我有很难遵循你正在尝试做的事情。我可以用 ASP 后端做这件事,你能不能也发布 ASP 代码,Actions/Controllers 和你的启动代码。
  • 谢谢。查看我添加的 svc 代码。别介意我对隐身模式的评论,我只是在测试不同的场景。我提出了一个重要问题:您为什么使用旧的 Google 脚本 https://apis.google.com/js/platform.js 而不是推荐的新脚本 https://accounts.google.com/gsi/client
  • 据我了解,旧的Platform.jsGSI client超集,至少在他们完成将其他内容添加到 GSI 之前,目前 GSI 仅发布了身份验证截至august 2021。上面的代码是我以前用过的,但是是C#的,我不懂VB。
猜你喜欢
  • 1970-01-01
  • 2022-12-28
  • 2018-09-22
  • 2022-07-05
  • 2021-10-26
  • 2014-10-10
  • 1970-01-01
  • 1970-01-01
  • 2021-10-17
相关资源
最近更新 更多