【问题标题】:Silent Redirect uri not being called using oidc-client没有使用 oidc-client 调用静默重定向 uri
【发布时间】:2020-11-25 22:04:12
【问题描述】:

我们尝试使用 Oidc-Client 库以静默方式更新令牌(刷新令牌)。我们能够成功登录。 但是一旦用户的令牌过期,即使配置如下,静默回调页面也不会被调用。如果有任何遗漏或重新更正,请提供帮助。 还在身份服务器中将静默重定向 uri 配置为 redirect_uri 之一。

Login.ts

Office.initialize = function () {  
     var settings = {
      authority: "https://xxxx.xxxxx.com/xxxx/v1", 
      client_id: "https://xxx.xxx.com/",
      redirect_uri: "https://localhost:3000/taskpane.html",
      post_logout_redirect_uri: "https://localhost:3000/logout.html", 
      revokeAccessTokenOnSignout: true,      
      response_type: "id_token token",
      scope: "openid read:xxxx read:xxxx",
      state: true,
      filterProtocolClaims: true,  
      loadUserInfo: true,
      nonce:true, 
      clearHashAfterLogin: true,
      automaticSilentRenew: true,     
      silent_redirect_uri: 'https://localhost:3000/silent-refresh.html',      
      monitorsession:true,  
      metadata: {        
        issuer: 'https://xxx.xxx.com/xxx/v1',                    
        authorization_endpoint:  "https://xxx.xxx.com/xxxxx/v1/connect/authorize"                  
       
    }    
    };
    
    var mgr = new Oidc.UserManager(settings);
    mgr.signinRedirect();
    
mgr.events.addAccessTokenExpiring(function(){
    console.log("token expiring...");
}); 

}

silent-refresh.html

<head>
    <title>RefreshToken</title>
    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
</head>
<body>
    <script type="text/javascript" src=https://cdnjs.cloudflare.com/ajax/libs/oidc-client/1.10.0/oidc-client.js></script>
<script>
    new Oidc.UserManager().signinSilentCallback().then((user)=>
    { consolse.log("silentrenewed");}
    )
        .catch((err) => {
            console.log(err);
        });
        
</script>
</body>

Auth.ts

import { UserManager, WebStorageStateStore } from "oidc-client";

export default class AuthSigninService {
  private userManager: UserManager;

  constructor() {
  
      const settings: any = {
        ..................
        automaticSilentRenew: true,                                        
      accessTokenExpiringNotificationTime: 4,   
        silent_redirect_uri: "https://localhost:3000/taskpane.html",      
        monitorsession:false,  
             };

      this.userManager = new UserManager(settings);
    }
    
    public signin()
    {
        return this.userManager.signinRedirect();
    }  

      
  public async silentRenew() {
    try {
      const user = await this.userManager.signinSilentCallback().then((success) => {
        console.log("silentrenewed");
        console.log(success);
      }
      )
        .catch((err) => {
          console.log(err);
        });

    }
    catch (err) {
      console.log(err);
    }
    } 
}

taskpane.ts

document.getElementById('btnSilent').onclick = SilentRenew;

async function SilentRenew() {

  const auth = new AuthSigninService();
  auth.silentRenew();
   
}

【问题讨论】:

    标签: typescript single-page-application identityserver4 openid-connect oidc-client-js


    【解决方案1】:

    可能的原因:

    • 与内联脚本有关
    • 也许 UserManager 的第二个实例也需要使用设置进行初始化

    我会尝试什么

    • 在主窗口,调用 await mgr.signInSilent();进行“按需”静默续订并查看是否获得任何 console.log 输出。

    • 将 iframe 代码作为主应用程序的一部分,而不是在 HTML 页面中内联运行它

    比较的东西

    我的code sample做iframe静默更新,spa代码可能会给你一些想法。

    我倾向于将静默续订 URI 设置为主 index.html 页面,我觉得这样更简单。然后编写如下代码:

    if (window.top === window.self) {
    
        // If index.html is running on the main window, run the app
        const app = new App();
        app.execute();
    
    } else {
      
        // If index.html is running on an iframe, handle token renewal responses
        const app = new IFrameApp();
        app.execute();
    
    }
    

    这里是my OAuth code

    【讨论】:

    • 感谢加里的即时回复。但我仍然无法得到答案。我还尝试将 index.html 作为静默重定向页面,但它没有在控制台上调用。由于基于谷歌搜索,它足以实现以下内容 automaticSilentRenew: true,silent_redirect_uri: 'localhost:3000/silent-refresh.html' 也称为 signinSilentCallback() 方法....除此之外还有什么遗漏吗?
    • 我会尝试跟踪 HTTP 请求以查看是否正在发送传出消息,如果是,授权服务器返回什么响应。可能是 cookie 被丢弃,AS 试图呈现登录页面而不是正确处理 prompt=none
    • 我一直在 Chrome 浏览器的网络选项卡中进行跟踪,但我在任何时候都看不到被调用的静默刷新.html 页面,这是重点问题..
    • 尝试设置 Log.Level='debug': 如my code,然后调用 signInSilent() 以响应按钮单击,看看是否能告诉你任何信息。感觉 OIDC 客户端库正在抛出错误,因此您可能需要覆盖 signInSilentError 事件处理程序。
    • 谢谢加里。我会尽力让你知道。
    猜你喜欢
    • 2018-05-20
    • 2021-11-10
    • 2017-08-01
    • 2020-04-14
    • 2018-03-28
    • 2021-07-09
    • 2021-12-30
    • 2011-11-01
    • 2015-03-12
    相关资源
    最近更新 更多