Ejemplo n.º 1
0
        public async Task <IActionResult> Logout(LogoutModel model)
        {
            var logout = await InteractionService.GetLogoutContextAsync(model.LogoutId);

            var automaticRedirect = AuthorizationOptions.AutomaticRedirectionAfterSignOut && logout?.SignOutIFrameUrl != null;
            var signoutCallbacks  = TestStore
                                    .GetClients()
                                    .Where(client => client.LogoutUri != null)
                                    .Select(client => client.LogoutUri)
                                    .Where(logoutUrl =>
            {
                if (logout?.PostLogoutRedirectUri == null)
                {
                    return(true);
                }
                else if (logoutUrl == null)
                {
                    return(false);
                }
                else
                {
                    var logoutUri    = new Uri(logoutUrl);
                    var logoutDomain = logoutUri.IsDefaultPort ? $"{logoutUri.Scheme}://{logoutUri.Host}" : $"{logoutUri.Scheme}://{logoutUri.Host}:{logoutUri.Port}";

                    var postLogoutRedirectUri = new Uri(logout.PostLogoutRedirectUri);
                    var postLogoutDomain      = postLogoutRedirectUri.IsDefaultPort ? $"{postLogoutRedirectUri.Scheme}://{postLogoutRedirectUri.Scheme}" : $"{postLogoutRedirectUri.Scheme}://{postLogoutRedirectUri.Scheme}:{postLogoutRedirectUri.Port}";

                    return(!logoutDomain.Equals(postLogoutDomain, StringComparison.OrdinalIgnoreCase));
                }
            });

            var viewModel = new LoggedOutModel
            {
                AutomaticRedirectAfterSignOut = automaticRedirect,
                PostLogoutRedirectUri         = logout?.PostLogoutRedirectUri,
                ClientName       = logout?.ClientId,
                SignoutCallbacks = signoutCallbacks,
                //SignOutIframeUrl = logout?.SignOutIFrameUrl
            };

            var externalProvider = await GetExternalProviderAsync();

            if (externalProvider != null)
            {
                var logoutId = model.LogoutId ?? await InteractionService.CreateLogoutContextAsync();

                var url = Url.Action(nameof(Logout), new { logotId = model.LogoutId });

                try
                {
                    var logoutProperties = new AuthenticationProperties {
                        RedirectUri = url
                    };
                    await HttpContext.Authentication.SignOutAsync(externalProvider, logoutProperties);
                }
                catch (NotSupportedException)
                {
                    // hacks for external providers which dont have signout.
                }
                catch (InvalidOperationException)
                {
                    //hack for windows negotiate.
                }
            }

            // dlete the local authentication cooie
            await HttpContext.Authentication.SignOutAsync();

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