Karen_P
Karen_P

Reputation: 11

Azure AD Single Sign Off Not Hitting Controller ASP.Net Core

I need to implement a single-sign-off from azure ad into my ASP.Net Core app. I have the following code, but whenever I click on the "Logout" it the webpage posts a GET but it never hits my controller to return the view. I have the Logout url in azure correct. Here is my startup:

            services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;

            // single-signout removing cookie
            options.ConsentCookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.None;
        });

        services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
            .AddAzureAD(options => Configuration.Bind("AzureAd", options));

        services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
        {
            options.Authority = options.Authority + "/v2.0/";
            options.TokenValidationParameters.ValidateIssuer = true;

            //login path
            options.CallbackPath = "/Account/LoginCompleted";

            options.RemoteSignOutPath = "/Account/Logout";

        });

Here is my controller:

    public IActionResult LoginCompleted()
    {
        ViewResult viewResult = View();

        return viewResult;
    }

    public IActionResult Logout()
    {
        return View();
    }

Upvotes: 1

Views: 517

Answers (2)

Karen_P
Karen_P

Reputation: 11

I found that the following works when put into the view:

 <a asp-area="AzureAD" asp-controller="Account" asp-action="SignOut">Sign out</a>

Upvotes: 0

juunas
juunas

Reputation: 58875

The remote sign out path is opened in an iframe and isn't meant to be something that shows a view. It should remove the user's login cookie, or in another way log out the user.

Here's my article on Single Sign Out: https://joonasw.net/view/aad-single-sign-out-in-asp-net-core

Your Logout action should return a SignOutResult. So e.g.

return SignOut(); //Sign out of default scheme

You can also specify the cookie scheme name if it is not the default.

Upvotes: 1

Related Questions