How to signout from external provider while you are in callback page and the user want to cancel register

1.1k views Asked by At

How to signout from Saml external provider while you are in callback page and the user want to cancel register

Note: the user is not registered yet, he just enter external provider credential and redirect to my IDP to enter the additional data, I want to add action to be able to logout and cancel the registration process.

Logout Code

var vm = await BuildLoggedOutViewModelAsync(model.LogoutId);

        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.
            string url = Url.Action("Logout", "Account", new { Area = "Identity", logoutId = vm.LogoutId });

            // this triggers a redirect to the external provider for sign-out
            return SignOut(new AuthenticationProperties { RedirectUri = url }, vm.ExternalAuthenticationScheme);
        }


        return RedirectToPage("Login");
2

There are 2 answers

0
Ahmed Assaf On BEST ANSWER

The answer is just setting the user in the current context so, the saml2 can read the required data to make redirect

Request.HttpContext.User = info.Principal;
2
Anders Abel On

If I understand your question correctly, this is happening in the ExternalLoginCallback where you are logged in with the external identity, but not the main application identity.

To signout of the external identity use the SignOut call that you have in your example:

// this triggers a redirect to the external provider for sign-out
return SignOut(new AuthenticationProperties { RedirectUri = url },
  vm.ExternalAuthenticationScheme);

Usual rules for Saml2 applies if this just is a local logout or a federated logout which includes a redirect to the Idp.