How do I correctly sign out of my Azure MVC app that uses oidc?

Viewed 22

If I manually go to this url: https://localhost:1234/signout-oidc

Then it signs out of my azure AD connected mvc application.

However going to this url just presents me with a blank white screen. I'm trying to do this on a 'log out' button on my MVC site, so ideally I would have it redirect. I might also want to do some custom logic so it would be good if I could put this into an action.

I've seen some suggestions like this:

await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties { RedirectUri = "/" });

However nothing happens when running these lines, I remain signed in.

Can anyone tell me what the correct way is to sign out the way that URL does within my application?

1 Answers

In the controller, simply put:

return SignOut("Cookies", "OpenIdConnect");

To control the URL, in the program.cs or startup.cs, put a handler in:

builder.Services.AddAuthentication(authOptions =>
{
    authOptions.DefaultScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
    .AddMicrosoftIdentityWebApp(options =>
    {
        builder.Configuration.Bind("AzureAd", options);
        options.Events.OnSignedOutCallbackRedirect += context =>
        {
            context.Response.Redirect("/"); // Redirect URL
            context.HandleResponse();

            return System.Threading.Tasks.Task.CompletedTask;
        };
    });
Related