Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect to set logout page after external logout (openid Connect ) Asp net core 3.1

I'm trying to implement external login/logout for a webpage (asp net core 3.1) with OpenId Connect (oidc) as the login provder. Login is working fine, but logout redirection isn't working as intended. I'm sure there's an easy fix to it, but i'm fairly new to .NET development. Here's what I've tried so far without success:

The HTML for logout is as such:

<form class="form-inline" asp-area="Identity" asp-page="/Account/Logout">
    <button type="submit" class="nav-link btn btn-link text-light">Logg ut</button>
</form>

The Logout.cshtml.cs:

        //public async Task<IActionResult> OnPost(string returnUrl = null)
        //{
        //    await _signInManager.SignOutAsync();
        //    await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
        //    await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
        //    _logger.LogInformation("User logged out.");
        //    if (returnUrl != null)
        //    {
        //        return LocalRedirect(returnUrl);
        //    }
        //    else
        //    {
        //        return RedirectToPage();
        //    }

        //}

        public async Task<IActionResult> OnPost()
        {
            var user = HttpContext.User;
            if (user?.Identity.IsAuthenticated == true)
            {
                await _signInManager.SignOutAsync();
                await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
            }
            //await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
            var callbackUrl = Url.Page("/Account/Logout", pageHandler: null, values: null, protocol: Request.Scheme);
            return SignOut(new AuthenticationProperties { RedirectUri = callbackUrl }, CookieAuthenticationDefaults.AuthenticationScheme, OpenIdConnectDefaults.AuthenticationScheme);


        }

In startup.cs

                //options.SignedOutCallbackPath = "/Identity/Account/Logout";
                options.SignedOutRedirectUri = "/Identity/Account/Logout";
                //options.RemoteSignOutPath = "/Identity/Account/Logout";
                options.Events = new OpenIdConnectEvents
                {
                   //OnSignedOutCallbackRedirect = (c) =>
                   //{
                   //    c.Response.Redirect("/Identity/Account/Logout)");
                   //    c.HandleResponse();
                   //    return Task.CompletedTask;
                   //},

Redirect uri set at loginprovider: Redirect URI set at loginprovider

Closest I got to a redirect of sorts (The OnPost function is not called at all) was using RemoteSignOutPath, but that just returns a blank page at /Identity/Account/Logout.

Edit: To clarify, as it is now I am able to logout and the OnPost function is called, but at logout the user is redirected to the loginproviders own /loggedout page with info about the user being logged out of the application.

like image 814
Laende Avatar asked Nov 07 '22 04:11

Laende


2 Answers

The logout should not return any IActionResult, instead it should look like this:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task Logout()
    {
        await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
        await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
    }

If you want to set where to redirect to, then you pass that as a parameter to the SignOutAsync method, like this:

  var prop = new AuthenticationProperties()
    {
        RedirectUri = redirectUri
    };

then pass it to the SignOutAsync method like:

await context.SignOutAsync(OpenIdConnectDefaults, prop);

Give it a try!

like image 50
Tore Nestenius Avatar answered Nov 09 '22 22:11

Tore Nestenius


Please, use this simple sample:

  1. Update your Logout action in your controller like that:
[HttpGet]
public async Task<IActionResult> Logout()
{
     var user = HttpContext.User;
     if (user?.Identity.IsAuthenticated == true)
     {
          await _signInManager.SignOutAsync();
     }
    
     return View("Home");
}
  1. Update the html view like that:
<a class="dropdown-item" asp-action="Logout" asp-controller="Account">Logout</a>
like image 21
PWND Avatar answered Nov 09 '22 22:11

PWND