簡體   English   中英

oidc-client登錄后重定向

[英]Redirect after oidc-client login

我有一個 angular 2 正在運行的 asp.net 核心應用程序。 我正在使用 oidc-client.js 庫來處理登錄。目前,如果用戶已經登錄並點擊了指向該站點的鏈接,那么身份驗證會正確進行並且用戶會被帶到正確的頁面。 但是,如果他們沒有登錄,則可以正常返回到登錄頁面,返回帶有正確 URL 的站點,但最終會進入 /auth-callback 頁面。 那時,重定向 url 會丟失,並且在 auth-callback 之后它們會到達站點的默認路由。

我正在尋找的是 oidc-client 庫中是否有任何選項可用於在 auth-callback 調用后保留正確的 url? 翻閱文檔和示例,沒有什么讓我感到驚訝。 還是我必須自己把一些東西放在一起?

在您的客戶端代碼中,當您調用signinRedirect您可能會濫用 state 屬性以及稍后使用的內容(這不是它的用途),例如new Oidc.UserManager().signinRedirect({state:window.location.href}); .

然后在您的回調頁面中,您可以使用它:

mgr
  .signinRedirectCallback()
  .then(function (user) {
    window.history.replaceState({}, window.document.title, window.location.origin + window.location.pathname);

    window.location = user.state || "/";
  });

那么你必須在你的資產中創建一個單獨的 html 頁面來處理獲取令牌並在登錄重定向后存儲它們

  1. 在您的 openID 提供商上登錄成功
  2. 重定向到您的 html 靜態頁面
  3. 例如,您的頁面將獲取令牌並將它們存儲在 localStorage 中
  4. 當令牌准備好時,您的靜態 html 頁面將重定向到您的 angular 應用程序根目錄

您可以在 /assets 文件夾中創建 redirect_page.html

  <script src="https://cdnjs.cloudflare.com/ajax/libs/oidc-client/1.4.1/oidc-client.min.js"></script>
  <script>
   var config = {
    userStore: new Oidc.WebStorageStateStore({ store: window.localStorage })
   };
   var mgr = new Oidc.UserManager(config);
   mgr.signinRedirectCallback().then(() => {
    window.history.replaceState({},
        window.document.title,
        window.location.origin);
        window.location = "/";
    }, error => {
    console.error(error);
   });

  </script>

並設置您的 userManager 配置

var config = {
  ...
  redirect_uri: `${clientRoot}/assets/redirect_page.html`,
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM