IdentityServer4 登录成功后,跳转到原来页面

IdentityServer4 登录成功后,默认会跳转到Config.Client配置的RedirectUris地址http://localhost:5003/callback.html,用于获取 Token,比如跳转后的地址:

http://localhost:5003/callback.html#id_token=eyJhbGciOiJSUzI1NiIsImtpZCI6IjkwYWRmOTliMDhmODExMjBiMzdlMWI1NzVmMDliODcwIiwidHlwIjoiSldUIn0.eyJuYmYiOjE0OTQ2NjA3ODQsImV4cCI6MTQ5NDY2MTA4NCwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo1MDAxIiwiYXVkIjoiaDUiLCJub25jZSI6IjQ0OTYwZDQzYjgwNTQzOWJiYjI4NWFkZjJiYjFkNDA0IiwiaWF0IjoxNDk0NjYwNzg0LCJhdF9oYXNoIjoieE9Lb0V1VnlMNHdXUVhsbllaak5udyIsInNpZCI6ImMzYzZkZjhhOTBjYjA3NGJhMWE4YjZjMzgzM2E5OTEwIiwic3ViIjoiMiIsImF1dGhfdGltZSI6MTQ5NDY2MDc4MywiaWRwIjoibG9jYWwiLCJhbXIiOlsicHdkIl19.AUNbgEoIOdw7_4NB9Hju-npbqWtqbytLe9uPhXuCwJwAbTeSIvHM3hE9ksqUCUIZtR8zYKYrCzUmHvqoZWvOEautxY9sJToPyyFeuuf7eKe5pgaplNFu6M3J8QBN9XRuGhYhipc-Yd3pa1zNmKAPC3yQbo_x139cBWIWHMIOkGxgW7IOcdrNvEB0Dq29tXcrUiOZkeOYC9sP_lLAhWA6_TeVXaJdwQ0vZQRtF7JaQ-Zt2yiFDhu2r3walhtg6AnCMnDVovyz1Vl--j-lANYkuprI-Zyzak-Cps-tT6_JC9sm8OIqVg_aK2t71AWg4dYAfgzYl_3_-N46OiOg8I-p5g&access_token=eyJhbGciOiJSUzI1NiIsImtpZCI6IjkwYWRmOTliMDhmODExMjBiMzdlMWI1NzVmMDliODcwIiwidHlwIjoiSldUIn0.eyJuYmYiOjE0OTQ2NjA3ODQsImV4cCI6MTQ5NDY2MDc5NCwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo1MDAxIiwiYXVkIjpbImh0dHA6Ly9sb2NhbGhvc3Q6NTAwMS9yZXNvdXJjZXMiLCJhcGkxIl0sImNsaWVudF9pZCI6Img1Iiwic3ViIjoiMiIsImF1dGhfdGltZSI6MTQ5NDY2MDc4MywiaWRwIjoibG9jYWwiLCJuYW1lIjoiMTIzIiwibmljayI6Iuifi-ifgCIsIm1vYmlsZSI6IjE4NjU4MTMwMzI2Iiwic2NvcGUiOlsib3BlbmlkIiwicHJvZmlsZSIsImFwaTEiXSwiYW1yIjpbInB3ZCJdfQ.lOw-ZLnYt5VejRmh2JnQfL6q2z8T9iLQELUJv4v3vvOkIn5wmUDmhLWkS6GQFXaKwybKCQwlh7OOtb08WZyE-ID7TJS1to7ZqxFR0Q8AGmvsaQxQ2pRTsLcHj11mxdbYhekTxfUldlYYYklupXOCJRe64ioMszpgrwDW1-oepsoUSws9X3yAvwf4rMigZ93-_FMzhfQHBoP2v3vu4x0mRrcas4sX1JIOasBGcTLAttO3HuNHAKrsDZfpxnzhyUZpdmpwGdZEz4pboUIhnh0338BM-WHBCWBzRGtT1CMiqECXhFtbRzNjASlG3jsRXwRSeq7_PvXZmShWHCnkgnYGnw&token_type=Bearer&expires_in=10&scope=openid%20profile%20api1&state=22bdd1faeb21414b8071c915e696fe7a&session_state=lXXBM8MVyOwEPMf_nuzDtafYVsHGmGAJTRfDpGgAmKg.deb0cdd03489a8d0782577e162ceb67f

前端会从 URL 中获取到 Token 信息,然后把用户信息和 Token 写入到上下文中,示例代码:

new Oidc.UserManager().signinRedirectCallback().then(function () {
    window.location = "/index.html";
}).catch(function (e) {
    console.error(e);
});

可以看到,登录成功后,是手动设置的跳转地址,那我们该如何调整到登录之前的地址呢?其实也很简单(但我尝试了好久)就是使用state传递地址。

登录示例代码:

function login() {
    mgr.signinRedirect({ state: window.location.href });
}

callback.html登录成功后,回调示例代码:

new Oidc.UserManager().signinRedirectCallback().then(function (user) {
    window.location = user.state;
}).catch(function (e) {
    console.error(e);
});

参考资料:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值