刷新后重定向到特定页面 (oidc.js)
Redirect to specific page after refreshing (oidc.js)
我正在使用 .NET Core (+IdentityServer) 后端以及 SPA React 应用程序。
每当我更改 SPA 应用程序中的页面时,url 都会更新。 (例如:/ 到 /users)。
问题:
每当我使用 F5 刷新页面或想使用 url 转到连接到路由的特定组件时,我只会在浏览器中执行以下操作:
- 在url中按回车键时的基本状态:localhost:3000/users
- 按下回车后:http://localhost:3000/signin-oidc#id_token=...
- 当用户登录时,页面重定向到主页面 url:localhost:3000/
所以,基本上我总是从当前 url 重定向到主页面。
如何使用 oidc-client 在 React 中使应用程序在刷新时重定向回 localhost:3000/users 或 url 更改?
让用户能够刷新页面(或转到特定 url)并记住用户已经获得授权的最佳做法是什么?
如果我对你的理解是正确的,并且你正在使用 oidc-client-js 库来实现 Open Id Connect,那么我的 NodeJS Code Sample may be useful to you, and the Authenticator class 特别是。
恢复重定向前的位置/深层链接
这是通过这样的代码管理的,如果用户在您的应用程序中为某个位置添加了书签并且需要先登录,这也很有用:
// Store the state when redirecting the page
await this._userManager.signinRedirect({
state: location.hash,
});
// Restore it afterwards - and remove tokens from the URL
const user = await this._userManager.signinRedirectCallback();
history.replaceState({}, document.title, user.state.hash);
正在重新加载页面或打开新标签页
这最好通过使用授权服务器会话 cookie 的静默令牌更新来管理,这样就没有顶级重定向。
await this._userManager.signinSilent();
运行 我的样本针对你的系统
通过将 SPA 中的详细信息和 API 配置文件更改为指向 Identity Server,运行 将示例与您自己的系统进行比较,可能会帮助您进行比较。
我正在使用 .NET Core (+IdentityServer) 后端以及 SPA React 应用程序。
每当我更改 SPA 应用程序中的页面时,url 都会更新。 (例如:/ 到 /users)。
问题: 每当我使用 F5 刷新页面或想使用 url 转到连接到路由的特定组件时,我只会在浏览器中执行以下操作:
- 在url中按回车键时的基本状态:localhost:3000/users
- 按下回车后:http://localhost:3000/signin-oidc#id_token=...
- 当用户登录时,页面重定向到主页面 url:localhost:3000/
所以,基本上我总是从当前 url 重定向到主页面。
如何使用 oidc-client 在 React 中使应用程序在刷新时重定向回 localhost:3000/users 或 url 更改? 让用户能够刷新页面(或转到特定 url)并记住用户已经获得授权的最佳做法是什么?
如果我对你的理解是正确的,并且你正在使用 oidc-client-js 库来实现 Open Id Connect,那么我的 NodeJS Code Sample may be useful to you, and the Authenticator class 特别是。
恢复重定向前的位置/深层链接
这是通过这样的代码管理的,如果用户在您的应用程序中为某个位置添加了书签并且需要先登录,这也很有用:
// Store the state when redirecting the page
await this._userManager.signinRedirect({
state: location.hash,
});
// Restore it afterwards - and remove tokens from the URL
const user = await this._userManager.signinRedirectCallback();
history.replaceState({}, document.title, user.state.hash);
正在重新加载页面或打开新标签页
这最好通过使用授权服务器会话 cookie 的静默令牌更新来管理,这样就没有顶级重定向。
await this._userManager.signinSilent();
运行 我的样本针对你的系统
通过将 SPA 中的详细信息和 API 配置文件更改为指向 Identity Server,运行 将示例与您自己的系统进行比较,可能会帮助您进行比较。