Cannot sign out the OpenIdConnect authentication of identityserver4 on ASP.NET Core 2 application
I can resolve my problem now.
1) Return SignOutResult will call endsession endpoint.
2) Change AJAX post to submit form.
public class AccountController : Controller
{
public IActionResult Signout()
{
return new SignOutResult(new[] { "oidc", "Cookies" });
}
}
<form action="/Account/Signout" id="signoutForm" method="post" novalidate="novalidate">
<ul class="nav navbar-nav navbar-right">
<li><a href="javascript:document.getElementById('signoutForm').submit()">Sign out</a></li>
</ul>
</form>
To allow the signing out to occur use the following Logout action:
public async Task Logout()
{
await HttpContext.SignOutAsync("Cookies");
await HttpContext.SignOutAsync("oidc");
}
This is exactly what the quickstart says to use (http://docs.identityserver.io/en/release/quickstarts/3_interactive_login.html). You (and I) have been too clever. I looked at the action in the tutorial and thought 'That's not complete, it doesn't return an action result, lets redirect back to my page'.
Actually what happens is HttpContext.SignOutAsync("oidc");
sets a default ActionResult (Which is to redirect to the OpenIdConnect provider to complete the sign-out). By specifying your own with return RedirectToAction("Index", "Home");
you override this, so the sign-out action never happens.
Redirect after logout
From this answer, the way you specify a redirect URL after the logout is completed is by using AuthenticationProperties
public async Task Logout()
{
await context.SignOutAsync("Cookies");
var prop = new AuthenticationProperties
{
RedirectUri = "/logout-complete"
};
// after signout this will redirect to your provided target
await context.SignOutAsync("oidc", prop);
}
On Net Core 2.0 change your code to use the enumerations CookieAuthenticationDefaults and OpenIdConnectDefaults
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(SetOpenIdConnectOptions);
private static void SetOpenIdConnectOptions(OpenIdConnectOptions options)
{
options.ClientId = "auAuthApp_implicit";
options.Authority = "http://localhost:55379/";
options.SignInScheme = "Cookies";
options.RequireHttpsMetadata = false;
options.SaveTokens = true;
options.ResponseType = "id_token token";
options.GetClaimsFromUserInfoEndpoint = true;
}
and...
public async Task<IActionResult> Logout()
{
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
return RedirectToAction("Index", "Home");
}