Ejemplo n.º 1
0
        public async Task <IActionResult> Logout(string logoutId)
        {
            //根据注销ID返回请求上下文
            var context = await _interaction.GetLogoutContextAsync(logoutId);

            //context?.ShowSignoutPrompt

            //
            var vm = new LogoutInputModel
            {
                //AutomaticRedirectAfterSignOut = AccountOptions.AutomaticRedirectAfterSignOut,
                PostLogoutRedirectUri = context?.PostLogoutRedirectUri,
                SignOutIframeUrl      = context?.SignOutIFrameUrl,
                ClientName            = context?.ClientName,
                ClientId = context?.ClientId,
                LogoutId = logoutId
            };

            //当前登录状态
            if (User?.Identity.IsAuthenticated == true)
            {
                var idp = User.FindFirst(JwtClaimTypes.IdentityProvider)?.Value;
                if (idp != null && idp != IdentityServer4.IdentityServerConstants.LocalIdentityProvider)
                {
                    //检查我们是否需要在上游身份提供者处触发签出
                    var providerSupportsSignout = await HttpContext.GetSchemeSupportsSignOutAsync(idp);

                    if (providerSupportsSignout)
                    {
                        //如没有当前的注销上下文,从当前登录的用户的信息创建
                        vm.LogoutId = await _interaction.CreateLogoutContextAsync();

                        string url = Url.Action("Logout", new { logoutId = vm.LogoutId });
                        return(SignOut(new AuthenticationProperties {
                            RedirectUri = url
                        }, idp));
                    }
                }

                // 删除本地认证cookie
                await HttpContext.SignOutAsync();

                //触发注销的事件
                await _events.RaiseAsync(new UserLogoutSuccessEvent(User.GetSubjectId(), User.GetDisplayName()));
            }
            return(View(vm));
        }
        public async Task <IActionResult> Logout(LogoutInputModel model)
        {
            // build a model so the logged out page knows what to display
            var vm = await BuildLoggedOutViewModelAsync(model.LogoutId);

            string url = Url.Action("Logout", new { logoutId = vm.LogoutId });

            var authProps = new AuthenticationProperties {
                RedirectUri = url
            };

            if (User?.Identity.IsAuthenticated == true)
            {
                // delete local authentication cookie
                await HttpContext.SignOutAsync();

                //   raise the logout event
                await _events.RaiseAsync(new UserLogoutSuccessEvent(User.GetSubjectId(), User.GetDisplayName()));
            }



            // check if we need to trigger sign-out at an upstream identity provider
            if (vm.TriggerExternalSignout)
            {
                // build a return URL so the upstream provider will redirect back
                // to us after the user has logged out. this allows us to then
                // complete our single sign-out processing.


                // this triggers a redirect to the external provider for sign-out


                return(SignOut(authProps, vm.ExternalAuthenticationScheme));
            }

            return(View("LoggedOut", vm));
        }