Error logging out from Cognito in ASP.NET Core MVC

0

I'm struggling with the ASP.NET Core MVC app and the integration to Cognito. I am using the AWS SDK and the hosted UI option.

I have managed to get the sign-in functionality working but am now struggling with the sign-out functionality.

I am basing this on the AWS code at https://catalog.us-east-1.prod.workshops.aws/workshops/02696107-09ac-4313-a6cb-3798048b07d7/en-US/9-how-to-authenticate-users-in-my-application and the example code it supplies.

This has the signout code as the following:

    public IActionResult SignOut()
    {
        var callbackUrl = Url.Page("/", pageHandler: null, values: null, protocol: Request.Scheme);
        return SignOut(
            new AuthenticationProperties { RedirectUri = callbackUrl },
            CookieAuthenticationDefaults.AuthenticationScheme, OpenIdConnectDefaults.AuthenticationScheme
        );
    }

After tweaking it for my app, this ends up as:

    public IActionResult SignOut()
    {
        var baseUri = $"{Request.Scheme}://{Request.Host}";
 
        //var callbackUrl = Url.Page("/", pageHandler: null, values: null, protocol: Request.Scheme);

        return SignOut(
            new AuthenticationProperties { RedirectUri = baseUri },
            CookieAuthenticationDefaults.AuthenticationScheme, OpenIdConnectDefaults.AuthenticationScheme
        );
    }

When I run the app and attempt to sign-out, the sign-out seems to function correctly (in that the cookies are removed). However, the Cognito redirect fails with the following error:

Enter image description here

When debugging the code it can be seen that the redirect URI is set to "https://localhost:7085"

Within AWS the App Client is defined as follows. I have gone to town trying various options so the URLS are currently set as the following

Enter image description here

Any ideas about what I am doing wrong here?

1 Answer
0

Hey,

From the docs you need to use the logout endpoint passing your redirect url

https://mydomain.auth.us-east-1.amazoncognito.com/logout? client_id=ad398u21ijw3s9w3939& logout_uri=https://myclient/logout

Haven't tested this but this should get you going once you have replaced the values below with your values

[HttpGet]
public async Task<IActionResult> LogOff()
{
     await HttpContext.SignOutAsync("OpenIdConnect");
     await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);

     return Redirect($"https://mydomain.auth.us-east-1.amazoncognito.com/logout?client_id={ClientId}&logout_uri={Request.Scheme}://{Request.Host}");
}

Hope that helps

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions