可以在发起logout请求时,添加一个调用“prompt=none”参数的参数,如果logout成功且用户能够使用“prompt=none”参数,则应用程序会返回“HTTP 204 No Content”响应。如果该用户未找到或未能使用“prompt=none”参数,则应用程序会返回“HTTP 401 Unauthorized”响应。
具体实现可以参考下面的代码示例:
//添加prompt=none参数
string logoutUrl = new RequestUrl(await _interactionGenerator.GetLogoutContextAsync(logoutId)).CreateLogoutUrl(new IdentityServerAuthenticationOptions {
Authority = _environment.GetIdentityServerAuthority(),
AutomaticAuthenticate = false,
AutomaticChallenge = false,
RequireHttpsMetadata = true,
SaveToken = true,
ClientId = "implicit",
AuthenticationScheme = IdentityServerConstants.DefaultCookieAuthenticationScheme,
ResponseType = GalleryConstants.AuthenticationResponseType,
GetClaimsFromUserInfoEndpoint = true
}, additionalQueryParamaters: new Dictionary
//检查是否允许SignOutAsync if (await HttpContext.Authentication.GetAuthenticateInfoAsync("Cookies") == null) //如果没有登录,则直接重定向到/logout路径 { return new RedirectResult("~/logout" + HttpContext.Request.QueryString); }
var authenticationProps = new AuthenticationProperties { RedirectUri = redirectUrl }; await HttpContext.Authentication.SignOutAsync("Cookies", authenticationProps); //执行注销
//检查注销是否成功 var response = await _httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, logoutUrl)); if (response.IsSuccessStatusCode) //如果返回状态码为“No Content”,则表示注销成功 { return new RedirectResult(logoutUrl); } else if (response.StatusCode == HttpStatusCode.Unauthorized) //返回状态码为“Unauthorized”,则表示用户未登录或无法使用“prompt=none”参数 { Log.Error("Failed to sign out user due to missing or invalid login session", requestUrl: requestUrl);
return new RedirectResult(redirectUrl);
} else //其他情况下,抛出异常 { throw new Exception($"Failed to sign out user. StatusCode='{response
上一篇:ASP.NETCoreMVCNET5.0HTTPError500.0
下一篇:ASP.NETCoreMVC前端开发中,展示列表视图时,不断出现System.Collections.Generic.List的问题