在客户端程序, 我们补充一键登出操作.

使用了idsv之后, 退出的操作需要删除本地cookie, 然后去请求认证服务器, 也删除认证服务器的cookie.

 

官网给的退出的代码

public async Task Logout()
        {
            await HttpContext.SignOutAsync("Cookies");
            await HttpContext.SignOutAsync("oidc");
        }

.net core Identity集成IdentityServer(3) 一键登出.net core Identity集成IdentityServer(3) 一键登出

现在一键登出, 会发现右侧图片的问题, 那是因为我们没有在认证服务器上实现登出的逻辑.

那么我们去参考(抄写)一下quickstartui的代码, 下面的代码是我的简化版本, 实现一键登出并且跳回去

[HttpGet]
        public async Task<ActionResult> Logout(string logoutid) { 

            if (User?.Identity.IsAuthenticated == true)
            {
                // delete local authentication cookie
                await HttpContext.SignOutAsync(); 
                //手工删除认证cookie(原理是设置某个cookie过期), cookie名在startup中配置好了.
                HttpContext.Response.Cookies.Delete("identityCookieJJL");
            }
            return Redirect(GetUrlAfterLogout(logoutid).Result);
        }

为什么需要手工删除验证服务器的cookie呢. 我也不清除, 不过目前客户端是登出了. 但是验证服务器还是登录状态,

查看验证服务器的cookie, 有如下

.net core Identity集成IdentityServer(3) 一键登出

这个cookiename是在startup中注册identity的时候指定的, 代码如下, 其实可以去系列文章的第一篇去查看

.net core Identity集成IdentityServer(3) 一键登出

 

这样就可以实现一键登出了.

 

实际上原本的登出代码如下

/// <summary>
        /// 退出回调用页面
        /// </summary>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Logout(LogoutInputModel model)
        {
            var vm = await _account.BuildLoggedOutViewModelAsync(model.LogoutId);
            var user = HttpContext.User;
            if (user?.Identity.IsAuthenticated == true)
            {
                //删除本地授权Cookies
                await HttpContext.SignOutAsync();
                await _events.RaiseAsync(new UserLogoutSuccessEvent(user.GetSubjectId(), user.GetName()));
            }

            // 检查是否需要在上游身份提供程序上触发签名
            if (vm.TriggerExternalSignout)
            {
                // 构建一个返回URL,以便上游提供者将重定向回
                // 在用户注销后给我们。这使我们能够
                // 完成单点签出处理。
                string url = Url.Action("Logout", new { logoutId = vm.LogoutId });
                // 这将触发重定向到外部提供者,以便签出
                return SignOut(new AuthenticationProperties { RedirectUri = url }, vm.ExternalAuthenticationScheme);
            }

            return View("LoggedOut", vm);
        }

还有不少东西需要研究

分类:

技术点:

相关文章: